fnakstad / angular-client-side-auth

One way to implement authentication/authorization in Angular applications
http://angular-client-side-auth.herokuapp.com/
MIT License
1.63k stars 346 forks source link

Adding database backing for LocalStrategy breaks the login #30

Closed vumaasha closed 11 years ago

vumaasha commented 11 years ago

I just modified the local strategy to support database backing, Users.find gets the user and the inside the callback, I moved the corresponding code. After I moved this, When i login, the home screen does not come. If i refresh the screen, then it home screen appears. I am not sure if it is a client side or server side issue. I am new to node and any suggestions on how to debug will also be very helpful.

The Users objects is sequelize orm mapping for the table that stores user info.

    localStrategy: new LocalStrategy(

        function(username, password, done) {

            Users.find({ where: {username: username} }).success(function(user){

                console.log('super')

                if(!user) {
                    done(null, false, { message: 'Incorrect username.' });
                }
                else if(user.password != password) {
                    done(null, false, { message: 'Incorrect username.' });
                }

                return done(null, user);

            }).error(function(err){
                console.log('error in LocalStrategy',err);
            });

        }
    )
fnakstad commented 11 years ago

Hi there! My first guess would be that you're not returning a user JSON object from the login action server-side. The client expects the server to tell it who the new user is when a login is performed, so you have to include the (non-sensitive, e.g. username + role) user data of the newly logged in user in the HTTP Response. If you inspect a login XHR in Chrome Dev Tools or Firebug you will see what the expected response should look like :)

Let me know if this works or not!

vumaasha commented 11 years ago

Perfect guess. It was the reason. Found that Auth.isLoggedIn() is responsible for checking the login status, after patiently reading your blog post one more time and this helped to fix the issue. Thanks a lot for your support.