| 12345678910111213141516171819202122232425262728293031323334 |
- import { Strategy as JwtStrategy, ExtractJwt } from 'passport-jwt';
- import passport from 'passport';
- import {User} from "./routes/auth.js";
- import dotenv from 'dotenv';
- // Geheimschlüssel für JWT
- // aus einem ENV File laden
- dotenv.config();
- const SECRET_KEY = process.env.JWT_SECRET.toString();
- const opts = {
- jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken(),
- secretOrKey: SECRET_KEY
- };
- passport.use(
- new JwtStrategy(opts, async (jwt_payload, done) => {
- try {
- const user = await User.findById(jwt_payload.id);
- if (user) {
- return done(null, user); // Successfully authenticated user
- } else {
- return done(null, false); // No user found
- }
- } catch (err) {
- return done(err, false); // Authentication error
- }
- })
- );
- export { passport, SECRET_KEY };
|