mirror of
https://github.com/danny-avila/LibreChat.git
synced 2025-12-20 02:10:15 +01:00
32 lines
972 B
JavaScript
32 lines
972 B
JavaScript
const { SystemRoles } = require('librechat-data-provider');
|
|
const { User, logger } = require('@librechat/data-schemas');
|
|
const { Strategy: JwtStrategy, ExtractJwt } = require('passport-jwt');
|
|
|
|
// JWT strategy
|
|
const jwtLogin = () =>
|
|
new JwtStrategy(
|
|
{
|
|
jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken(),
|
|
secretOrKey: process.env.JWT_SECRET,
|
|
},
|
|
async (payload, done) => {
|
|
try {
|
|
const user = await User.getUserById(payload?.id, '-password -__v -totpSecret');
|
|
if (user) {
|
|
user.id = user._id.toString();
|
|
if (!user.role) {
|
|
user.role = SystemRoles.USER;
|
|
await User.updateUser(user.id, { role: user.role });
|
|
}
|
|
done(null, user);
|
|
} else {
|
|
logger.warn('[jwtLogin] JwtStrategy => no user found: ' + payload?.id);
|
|
done(null, false);
|
|
}
|
|
} catch (err) {
|
|
done(err, false);
|
|
}
|
|
},
|
|
);
|
|
|
|
module.exports = jwtLogin;
|