node.js - How to configure and package passport as a one module? -
i test routes in advanced rest client, , code output 401, unauthorized. don't understand why happening. packaged authentication in 1 module. then, invoke in server file wagner-core(dependency injector):
wagner.invoke(require('./passport-init'),{ app: app })
passport.js:
'use strict' const bcrypt = require('bcryptjs') const passport = require('passport') const localstrategy = require('passport-local').strategy const session = require('express-session') module.exports = (user, app) => { passport.serializeuser((user, done) => { done(null, user._id) }) passport.deserializeuser((id, done) => { user.findone({ _id: id }).exec(done) }) passport.use('login', new localstrategy({ passreqtocallback: true }, (req, username, password, done) => { user.findone({ username: username }, (err, user) => { if (err) { return done(err) } if (!user) { return done(null, false, { message: 'invalid username' }) } if (!isvalidpassword(user, password)) { return done(null, false, { message: 'invalid password' }) } return done(null, user) }) })) app.use(session({ secret: process.env.session_secret || 'secret', resave: false, saveuninitialized: false })) app.use(passport.initialize()) app.use(passport.session()) app.post('/login', passport.authenticate('login'), (req, res) => { res.redirect('/users/' + req.user.username) }) } function isvalidpassword (user, password) { return bcrypt.comparesync(password, user.password) }
i figured out. routes, , isvalidpassword function.
heres amended code:
'use strict' const bcrypt = require('bcryptjs') const passport = require('passport') const localstrategy = require('passport-local').strategy const session = require('express-session') module.exports = (user, app) => { passport.serializeuser((user, done) => { done(null, user._id) }) passport.deserializeuser((id, done) => { user.findbyid(id, (err, user) => { done(err, user) }) }) passport.use('login', new localstrategy({ passreqtocallback: true }, (req, username, password, done) => { process.nexttick(() => { user.findone({ username: username }, (err, user) => { if (err) { return done(err) } if (!user) { return done(null, false, { message: 'invalid username' }) } if (!user.password) { bcrypt.comparesync(password, user.password) return done(null, false, { message: 'invalid password' }) } return done(null, user) }) }) })) passport.use('signup', new localstrategy({ passreqtocallback: true }, (req, username, password, done) => { process.nexttick(() => { user.findone({ username: username }, (err, user) => { if (err) { return done(err) } if (user) { return done(null, false, { message: 'user exists' }) } else { let newuser = new user() newuser.username = req.body.username newuser.password = createhash(req.body.password) newuser.save((err) => { if (err) throw err return done(null, newuser) }) } }) }) })) app.use(session({ secret: 'secret', resave: false, saveuninitialized: false })) app.use(passport.initialize()) app.use(passport.session()) app.get('/success', (req, res) => { res.send({ state: 'success', user: req.user ? req.user : null }) }) app.post('/login', passport.authenticate('login', { successredirect: '/success', failureredirect: '/fail' })) app.post('/signup', passport.authenticate('signup', { successredirect: '/success', failureredirect: '/fail' })) } function createhash (password) { return bcrypt.hashsync(password, bcrypt.gensaltsync(10), null) }
Comments
Post a Comment