mirror of
https://github.com/linuxserver/heimdalljs.git
synced 2026-02-20 05:12:24 +08:00
40 lines
830 B
JavaScript
40 lines
830 B
JavaScript
/**
|
|
* Passport for authentication
|
|
*/
|
|
const { Strategy } = require('passport-jwt')
|
|
const { User } = require('./models/index')
|
|
const config = require('./config/config')
|
|
|
|
module.exports = passport => {
|
|
passport.use(
|
|
new Strategy(
|
|
{
|
|
secretOrKey: config.jwtSecret,
|
|
jwtFromRequest(req) {
|
|
let token = null
|
|
|
|
// Pull the token from cookies, maybe support headers later?
|
|
if (req && req.cookies) {
|
|
token = req.cookies.jwt
|
|
}
|
|
|
|
return token
|
|
}
|
|
},
|
|
async (payload, done) => {
|
|
try {
|
|
const user = await User.findOne({
|
|
where: {
|
|
id: payload.id
|
|
}
|
|
})
|
|
|
|
return done(null, user)
|
|
} catch (err) {
|
|
return done(null, false)
|
|
}
|
|
}
|
|
)
|
|
)
|
|
}
|