SHOW:
|
|
- or go back to the newest paste.
1 | - | // config/passport.js |
1 | + | |
2 | var LocalStrategy = require('passport-local').Strategy | |
3 | - | // ========================================================================= |
3 | + | |
4 | - | // LOCAL LOGIN ============================================================= |
4 | + | |
5 | - | // ========================================================================= |
5 | + | |
6 | - | // we are using named strategies since we have one for login and one for signup |
6 | + | |
7 | - | // by default, if there was no name, it would just be called 'local' |
7 | + | |
8 | - | // config/passport.js |
8 | + | |
9 | // expose this function to our app using module.exports | |
10 | module.exports = function (passport) { | |
11 | ||
12 | // ========================================================================= | |
13 | // passport session setup ================================================== | |
14 | // ========================================================================= | |
15 | // required for persistent login sessions | |
16 | // passport needs ability to serialize and unserialize users out of session | |
17 | ||
18 | // used to serialize the user for the session | |
19 | passport.serializeUser(function (user, done) { | |
20 | done(null, user.id); | |
21 | }); | |
22 | ||
23 | // used to deserialize the user | |
24 | passport.deserializeUser(function (id, done) { | |
25 | const connection = mysql.createConnection(dbConfig) | |
26 | connection.connect(function(err) { | |
27 | if (err) return done(err) | |
28 | connection.query('SELECT * FROM users WHERE id = ?', [id], function (err, results) { | |
29 | if (err) return done(err) | |
30 | connection.end() | |
31 | done(null, results.length === 1 ? results[0] : null); | |
32 | }); | |
33 | }) | |
34 | }); | |
35 | ||
36 | passport.use('local-login', new LocalStrategy({ | |
37 | usernameField : 'username', | |
38 | passwordField : 'password', | |
39 | passReqToCallback: true // allows us to pass back the entire request to the callback | |
40 | }, | |
41 | function (req, username, password, done) { // callback with username and password from our form | |
42 | req.getConnection(function(err, connection) { | |
43 | if (err) { | |
44 | console.log('Mysql connection has not been established ', err) | |
45 | return done(err); | |
46 | } | |
47 | ||
48 | connection.query('SELECT * FROM users WHERE username = ?', [username], function(err, results) { | |
49 | if (err) { | |
50 | console.log('Sql query error ' + err) | |
51 | return done(err); | |
52 | } | |
53 | ||
54 | if(!results || results.length !== 1) { | |
55 | console.log('User not found ', username) | |
56 | return done(null, false, req.flash('loginMessage', 'Invalid credentials #1')) | |
57 | ||
58 | } | |
59 | ||
60 | bcrypt.compare(password, results[0].password, function(err, passwordsMatch) { | |
61 | if (err || !passwordsMatch) { | |
62 | console.log('Password validation error ', err, passwordsMatch) | |
63 | return done(null, false, req.flash('loginMessage', 'Invalid credentials')); | |
64 | } | |
65 | // all is well, return successful user | |
66 | return done(null, results[0]); | |
67 | }) | |
68 | }); | |
69 | ||
70 | }); | |
71 | })) | |
72 | ||
73 | } |