Skip to content

Commit 62c18c2

Browse files
Creating a to tokens manager middleware with passportjs
1 parent 2f9059e commit 62c18c2

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

src/midlewares/passport.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { Strategy, StrategyOptions, ExtractJwt } from 'passport-jwt'
2+
import config from '../config/config'
3+
import User from '../models/User'
4+
5+
const opts: StrategyOptions = {
6+
jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken(),
7+
secretOrKey: config.secretKey
8+
}
9+
10+
export default new Strategy(opts, (payload,done)=>{
11+
try {
12+
const user = User.findById(payload.id)
13+
if (user) {
14+
return done(null,user)
15+
}
16+
return done(null,false)
17+
} catch (error) {
18+
console.log(error)
19+
}
20+
})

0 commit comments

Comments
 (0)