Thursday 7 September 2017

How can I return JSON with passport ?

I'm trying passport for the first time. I wrote this in the route file:app.post('/signup', passport.authenticate('signup')); In the passport config file I wrote this:passport.use('signup', new LocalStrategy({ usernameField: 'email', passwordField: 'password' }, (email, password, done) => { User.findOne({'email': email}, (err, user) => { if(err) done(err); if(user) return done(null, false, {'signupMessage': 'That email is already taken.'}); else { const newUser = new User(); newUser.email = email; newUser.password = newUser.generateHash(password); newUser.save((err) => { if(err) throw err; return done(null, newUser); }); } }); })); I want to respond the request with the error message json if needed and if the user is not taken to respond with the newUser as a json. How can I do that ? Thanks.

Submitted September 07, 2017 at 04:52PM by DogoCap

No comments:

Post a Comment