8000 Added local strategy for sign-in · mythLabs/api-server-node-express@719f80c · GitHub
[go: up one dir, main page]

Skip to content

Commit 719f80c

Browse files
committed
Added local strategy for sign-in
1 parent 47e2cf8 commit 719f80c

File tree

4 files changed

+99
-64
lines changed

4 files changed

+99
-64
lines changed

controllers/authentication.js

Lines changed: 38 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,50 @@
1-
const User = require('../models/user');
2-
const jwt = require('jwt-simple');
3-
const config = require('../config');
1+
const User = require("../models/user");
2+
const jwt = require("jwt-simple");
3+
const config = require("../config");
44

55
function tokenForUser(user) {
6-
const timestamp = new Date().getTime();
7-
return jwt.encode({ sub: user.id, iat: timestamp },config.secret)
6+
const timestamp = new Date().getTime();
7+
return jwt.encode({ sub: user.id, iat: timestamp }, config.secret);
88
}
99

10-
exports.signup = function(req,res,next) {
11-
const email = req.body.email;
12-
const password= req.body.password;
10+
exports.signup = function(req, res, next) {
11+
const email = req.body.email;
12+
const password = req.body.password;
1313

14-
if (!email || !password) {
15-
return res.status(422).send({error: 'You must provide Email and Password'});
16-
}
14+
if (!email || !password) {
15+
return res
16+
.status(422)
17+
.send({ error: "You must provide Email and Password" });
18+
}
1719

18-
//See if user with email already exists
19-
User.findOne({email: email},function(err,existingUser){
20-
if (err) { return next(err)}
20+
//See if user with email already exists
21+
User.findOne({ email: email }, function(err, existingUser) {
22+
if (err) {
23+
return next(err);
24+
}
2125

22-
if (existingUser) {
23-
return res.status(422 ED4F ).send({error: 'Email is already in use'});
24-
}
26+
if (existingUser) {
27+
return res.status(422).send({ error: "Email is already in use" });
28+
}
2529

26-
const user = new User({
27-
email: email,
28-
password: password
29-
});
30+
const user = new User({
31+
email: email,
32+
password: password
33+
});
3034

31-
user.save(function(err){
32-
if (err) { return next(err);}
35+
user.save(function(err) {
36+
if (err) {
37+
return next(err);
38+
}
3339

34-
res.json({token: tokenForUser(user)});
35-
});
40+
res.json({ token: tokenForUser(user) });
41+
});
42+
});
43+
};
3644

37-
})
45+
exports.signin = function(req, res, next) {
46+
//email,password are authed
47+
//return token
3848

39-
}
49+
res.send({ token: tokenForUser(req.user) });
50+
};

models/user.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,17 @@ userSchema.pre("save", function(next) {
2424
});
2525
});
2626

27+
// helper method to compare hashed and non hashed passwords
28+
userSchema.methods.comparePassword = function(canditatePassword, callback) {
29+
bcrypt.compare(canditatePassword, this.password, function(err, isMatch) {
30+
if (err) {
31+
return callback(err);
32+
}
33+
34+
callback(null, isMatch);
35+
});
36+
};
37+
2738
//Create model class
2839
const ModelClass = mongoose.model("user", userSchema);
2940
//export

router.js

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,19 @@
1-
const Authentication = require('./controllers/authentication');
2-
const passportService = require('./services/passport');
1+
const Authentication = require("./controllers/authentication");
2+
const passportService = require("./services/passport");
33
const passport = require("passport");
44

5-
const requireAuth = passport.authenticate('jwt',{session: false});
5+
const requireAuth = passport.authenticate("jwt", { session: false });
6+
const requireSignin = passport.authenticate("local", { session: false });
67

78
module.exports = function(app) {
89
app.get("/", function(req, res, next) {
9-
res.send(['a','b']);
10+
res.send(["a", "b"]);
1011
});
1112

12-
app.get("/data",requireAuth, function(req, res) {
13-
res.send({data:'You are the Data'});
14-
});
15-
16-
app.post('/signup',Authentication.signup)
13+
app.post("/signup", Authentication.signup);
14+
app.post("/signin", requireSignin, Authentication.signin);
1715

16+
app.get("/data", requireAuth, function(req, res) {
17+
res.send({ data: "You are the Data" });
18+
});
1819
};

services/passport.js

Lines changed: 40 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -6,23 +6,35 @@ const ExtractJwt = require("passport-jwt").ExtractJwt;
66
const LocalStrategy = require("passport-local");
77

88
// Create local Strategy
9-
const localOptions={usernameField: 'email'};
10-
const localStrategy = new LocalStrategy(localOptions, function(email, password, done){
11-
12-
// Find user with email
13-
User.findOne({email: email},
14-
function(err, user) {
15-
if (err) {
16-
return done(err, false);
17-
}
18-
19-
if (!user) {
20-
return done(null, false);
21-
}
22-
});
9+
const localOptions = { usernameField: "email" };
10+
const localLogin = new LocalStrategy(localOptions, function(
11+
email,
12+
password,
13+
done
14+
) {
15+
// Find user with email
16+
User.findOne({ email: email }, function(err, user) {
17+
if (err) {
18+
return done(err, false);
19+
}
20+
21+
if (!user) {
22+
return done(null, false);
23+
}
24+
2325
//Compare hashed password
24-
25-
})
26+
user.comparePassword(password, function(err, isMatch) {
27+
if (err) {
28+
return done(err);
29+
}
30+
if (!isMatch) {
31+
return done(null, false);
32+
}
33+
34+
return done(null, user);
35+
});
36+
});
37+
});
2638

2739
//Setup options for JWT Strategy
2840
const jwtOptions = {
@@ -32,19 +44,19 @@ const jwtOptions = {
3244

3345
//Create JWT Strategy
3446
const jwtLogin = new JwtStrategy(jwtOptions, function(payload, done) {
35-
User.findById(payload.sub,
36-
function(err, user) {
37-
if (err) {
38-
return done(err, false);
39-
}
47+
User.findById(payload.sub, function(err, user) {
48+
if (err) {
49+
return done(err, false);
50+
}
4051

41-
if (user) {
42-
return done(null, user);
43-
} else {
44-
return done(null, false);
45-
}
46-
});
52+
if (user) {
53+
return done(null, user);
54+
} else {
55+
return done(null, false);
56+
}
57+
});
4758
});
4859

49-
// Tell Passport to use it
60+
// Tell Passport to use strategies
5061
passport.use(jwtLogin);
62+
passport.use(localLogin);

0 commit comments

Comments
 (0)
0