LibreChat/api/strategies/jwtStrategy.js
2024-06-07 16:45:31 -04:00

28 lines
776 B
JavaScript

const { Strategy: JwtStrategy, ExtractJwt } = require('passport-jwt');
const { getUserById } = require('~/models');
const { logger } = require('~/config');
// JWT strategy
const jwtLogin = async () =>
new JwtStrategy(
{
jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken(),
secretOrKey: process.env.JWT_SECRET,
},
async (payload, done) => {
try {
const user = await getUserById(payload?.id, '-password -__v');
user.id = user._id.toString();
if (user) {
done(null, user);
} else {
logger.warn('[jwtLogin] JwtStrategy => no user found: ' + payload?.id);
done(null, false);
}
} catch (err) {
done(err, false);
}
},
);
module.exports = jwtLogin;