-
Notifications
You must be signed in to change notification settings - Fork 0
/
passport.js
43 lines (40 loc) · 1.39 KB
/
passport.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
const passport = require('passport')
const JwtStrategy = require('passport-jwt').Strategy
const { ExtractJwt } = require('passport-jwt')
const LocalStrategy = require('passport-local').Strategy
const JWT_SECRET = require('./config').server_pub_secret
const User = require('./controllers/users')
const Op = require("sequelize").Op
passport.use(new JwtStrategy({
jwtFromRequest: ExtractJwt.fromHeader('authorization'),
secretOrKey: JWT_SECRET,
algorithms: ['RS256']
}, async (payload, done) => {
try {
const user = await User.users.findOne({ where: {[Op.and]: { username: payload.sub, is_active: true } }})
if (!user) {
return done(null, false)
}
done(null, user)
}
catch (error) {
done(error, false)
}
}))
passport.use(new LocalStrategy({
username: 'username'
}, async (username, password, done) => {
try {
const user = await User.users.findOne({ where:{[Op.and]: { username: username, is_active: true } } })
if (!user) {
return done(null, false, 'Invalid SOSKE-ID or password')
}
const isPasswordValid = await User.validatePassword(password, user.password)
if (!isPasswordValid) {
return done(null, false, 'Invalid SOSKE-ID or password')
}
done(null, user)
} catch (error) {
done(error, false, 'Invalid SOSKE-ID or password')
}
}))