scotch-io / mean-machine-code

Code samples for the book: MEAN Machine
https://leanpub.com/mean-machine
MIT License
459 stars 228 forks source link

'username not found' in chapter 10 #39

Closed Lgofk closed 8 years ago

Lgofk commented 8 years ago

First, I writhe all the code myself, than I tryed to copy it from the book but the problem doesn't go away :(

apiRouter.post('/authenticate', function (req, res) {
    // find the user
    User.findOne({
        username: req.body.username
    }).select('name username password').exec(function (err, user) {

        if (err) throw err;

        // no user with that username was found
        if (!user) {
            res.json({
                success: false,
                message: 'Authentication failed. User not found.'
            });
        } else if (user) {

            // check if password matches
            var validPassword = user.comparePassword(req.body.password);
            if (!validPassword) {
                res.json({
                    success: false,
                    message: 'Authentication failed. Wrong password.'
                });
            } else {

                // if user is found and password is right
                // create a token
                var token = jwt.sign({
                    name: user.name,
                    username: user.username
                }, superSecret, {
                    expiresInMinutes: 1440 // expires in 24 hours
                });

                // return the information including token as JSON
                res.json({
                    success: true,
                    message: 'Enjoy your token!',
                    token: token
                });
            }
        }
    });
});

image

username and password are correct.

Also I found out that the findOne method returns 'null'.

FNGR2911 commented 8 years ago

how du you get it work? getting the some error all the time :(

chris-sev commented 8 years ago

What database are you using? Are you sure that the user you're trying to login with is in there?

Too many people abused the database provided in the book, so you'll need to spin up your own either locally or on mlab.com or modulus.io.

FNGR2911 commented 8 years ago

Hi @sevilayha: I got local mongodb working and checkt the database with Robomongo. The creation of the user worked without a problem but when I try to find him like in the tutorial I always get the user not found :(

erhv74 commented 8 years ago

that's looks like a deja-vu for me when i followed the examples. I think was a problem with:

params.username params.password

i changed to:

body.username body.password

something like that,

On Mon, Aug 1, 2016 at 11:48 PM, FNGR2911 notifications@github.com wrote:

Hi @sevilayha https://github.com/sevilayha: I got local mongodb working and checkt the database with Robomongo. The creation of the user worked without a problem but when I try to find him like in the tutorial I always get the user not found :(

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/scotch-io/mean-machine-code/issues/39#issuecomment-236805478, or mute the thread https://github.com/notifications/unsubscribe-auth/ABHNYI1-BfvwiG3n83Z31ojrVhpEacTnks5qbto-gaJpZM4Iez4q .