-
Notifications
You must be signed in to change notification settings - Fork 0
/
authenticate.js
31 lines (29 loc) · 951 Bytes
/
authenticate.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
const passport = require('passport');
const jwt = require('jsonwebtoken');
require('dotenv').config();
const dev = process.env.NODE_ENV !== "production";
exports.COOKIE_OPTIONS = {
/*to prevent client access */
httpOnly:true,
/*secure only for production */
secure: !dev,
signed:true,
maxAge: eval(process.env.REFRESH_TOKEN_EXPIRY) * 1000,
/*client and server have different domains */
sameSite:"none",
};
/*creates the JWT */
exports.getToken = user => {
return jwt.sign(user,process.env.JWT_SECRET, {
expiresIn: eval(process.env.SESSION_EXPIRY)
})
};
/*create jwt refresh token */
exports.getRefreshToken = user => {
const refreshToken = jwt.sign(user,process.env.REFRESH_TOKEN_SECRET, {
expiresIn: eval(process.env.REFRESH_TOKEN_EXPIRY),
})
return refreshToken;
};
/*called for every authenticated request */
exports.verifyUser = passport.authenticate('jwt', {session: false});