forked from ShragaUser/spike-auth-middleware
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpassport.js
20 lines (14 loc) · 879 Bytes
/
passport.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
const passport = require("passport");
const { Strategy: JwtStrategy, ExtractJwt } = require("passport-jwt");
const addJwtStrategyToPassport = (secretOrKey, useBearerToken, verifyFunction, name = 'spike-auth-middleware-jwt') => {
const jwtFromRequest = useBearerToken ? ExtractJwt.fromAuthHeaderAsBearerToken() : ExtractJwt.fromHeader('authorization');
passport.use(name, new JwtStrategy({ secretOrKey, jwtFromRequest }, verifyFunction));
};
let jwtStrategyIndex = 0;
const getPassportAuthMiddleware = (secretOrKey, useBearerToken, verifyFunction) => {
// ensure unique strategy name
const strategyName = `spike-auth-middleware-jwt${jwtStrategyIndex++}`;
addJwtStrategyToPassport(secretOrKey, useBearerToken, verifyFunction, strategyName);
return passport.authenticate(strategyName, { session: false });
}
module.exports = getPassportAuthMiddleware;