@@ -40,30 +40,34 @@ app.use(function(req, res, next){
40
40
41
41
// dummy database
42
42
43
- var users = {
44
- user1 : { name : 'user1' } ,
45
- user2 : { name : 'user2 ' }
46
- } ;
43
+ var users = [
44
+ { firstName : 'Rene' , lastName :
'Mueller' , email : '[email protected] ' } ,
45
+ { firstName : 'Cecilia' , lastName :
'Stark' , email : '[email protected] '}
46
+ ] ;
47
47
48
48
// when you create a user, generate a salt
49
49
// and hash the password ('foobar' is the pass here)
50
50
51
51
hash ( { password : 'foobar' } , function ( err , pass , salt , hash ) {
52
52
if ( err ) throw err ;
53
53
// store the salt & hash in the "db"
54
- users . user1 . salt = salt ;
55
- users . user1 . hash = hash ;
56
- users . user2 . salt = salt ;
57
- users . user2 . hash = hash ;
54
+ users . forEach ( ( element ) => {
55
+ element . salt = salt ;
56
+ element . hash = hash ;
57
+ } ) ;
58
58
} ) ;
59
59
60
+ function findUserbyEmail ( email ) {
61
+ var u = users . find ( u => u . email === email ) ;
62
+ return u ;
63
+ }
60
64
61
65
// Authenticate using our plain-object database of doom!
62
66
63
- function authenticate ( name , pass , fn ) {
64
- if ( ! module . parent ) console . log ( 'authenticating %s:%s' , name , pass ) ;
65
- var user = users [ name ] ;
66
- // query the db for the given username
67
+ function authenticate ( email , pass , fn ) {
68
+ if ( ! module . parent ) console . log ( 'authenticating %s:%s' , email , pass ) ;
69
+ var user = findUserbyEmail ( email ) ;
70
+ // query the db for the given email
67
71
if ( ! user ) return fn ( null , null )
68
72
// apply the same algorithm to the POSTed password, applying
69
73
// the hash against the pass / salt, if there is a match we
@@ -106,7 +110,7 @@ app.get('/login', function(req, res){
106
110
} ) ;
107
111
108
112
app . post ( '/login' , function ( req , res , next ) {
109
- authenticate ( req . body . username , req . body . password , function ( err , user ) {
113
+ authenticate ( req . body . email , req . body . password , function ( err , user ) {
110
114
if ( err ) return next ( err )
111
115
if ( user ) {
112
116
// Regenerate session when signing in
@@ -117,17 +121,17 @@ app.post('/login', function (req, res, next) {
117
121
// in the session store to be retrieved,
118
122
// or in this case the entire user object
119
123
req . session . user = user ;
120
- req . session . success = 'Authenticated as ' + user . name
124
+ req . session . success = 'Authenticated as ' + user . firstName + '' + user . lastName
121
125
+ ' click to <a href="/logout">logout</a>. '
122
126
+ ' You may now access <a href="/restricted">/restricted</a>.' ;
123
127
//res.redirect('back');
124
- res . redirect ( returnTo || '/' ) ;
128
+ res . redirect ( returnTo || '/restricted ' ) ;
125
129
delete req . session . returnTo ;
126
130
} ) ;
127
131
} else {
128
132
req . session . error = 'Authentication failed, please check your '
129
- + ' username and password.'
130
- + ' (use "user1 " or "user " and password "foobar")' ;
133
+ + ' email and password.'
134
+ + ' (use "[email protected] " or "[email protected] " and password "foobar")' ;
131
135
res . redirect ( '/login' ) ;
132
136
}
133
137
} ) ;
0 commit comments