mirror of
https://github.com/danny-avila/LibreChat.git
synced 2025-12-17 08:50:15 +01:00
30 lines
761 B
JavaScript
30 lines
761 B
JavaScript
|
|
const passport = require('passport');
|
||
|
|
const { Strategy: JwtStrategy, ExtractJwt } = require('passport-jwt');
|
||
|
|
const User = require('../models/User');
|
||
|
|
|
||
|
|
const isProduction = process.env.NODE_ENV === 'production';
|
||
|
|
const secretOrKey = isProduction ? process.env.JWT_SECRET_PROD : process.env.JWT_SECRET_DEV;
|
||
|
|
|
||
|
|
// JWT strategy
|
||
|
|
const jwtLogin = new JwtStrategy(
|
||
|
|
{
|
||
|
|
jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken(),
|
||
|
|
secretOrKey
|
||
|
|
},
|
||
|
|
async (payload, done) => {
|
||
|
|
try {
|
||
|
|
const user = await User.findById(payload.id);
|
||
|
|
if (user) {
|
||
|
|
done(null, user);
|
||
|
|
} else {
|
||
|
|
console.log('JwtStrategy => no user found');
|
||
|
|
done(null, false);
|
||
|
|
}
|
||
|
|
} catch (err) {
|
||
|
|
done(err, false);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
);
|
||
|
|
|
||
|
|
passport.use(jwtLogin);
|