diff options
Diffstat (limited to 'lib/passport.js')
| -rw-r--r-- | lib/passport.js | 63 |
1 files changed, 44 insertions, 19 deletions
diff --git a/lib/passport.js b/lib/passport.js index 8da82b8e..16deef6e 100644 --- a/lib/passport.js +++ b/lib/passport.js @@ -1,6 +1,19 @@ -import Passport from 'passport'; -import jwt from 'passport-jwt'; -import { Op } from 'sequelize'; +import app from "../app.js"; +import Passport from "passport"; +import jwt from "passport-jwt"; +import { Op } from "@sequelize/core"; +import fastifyPassport from "@fastify/passport"; +import fastifySecureSession from '@fastify/secure-session' +import config from "../config/index.js"; +import fs from "fs"; +import path from "path"; + +app.register(fastifySecureSession, { + key: fs.readFileSync(config.jwt) +}); + +app.register(fastifyPassport.initialize()); +app.register(fastifyPassport.secureSession()); function cookieExtractor(req) { if (req && req.cookies) { @@ -9,7 +22,8 @@ function cookieExtractor(req) { return null; } -passport = Passport.use( +fastifyPassport.use( + "jwt", new jwt.Strategy( { jwtFromRequest: jwt.ExtractJwt.fromExtractors([ @@ -40,21 +54,32 @@ passport = Passport.use( ) ); -auth = passport.authenticate("jwt", { session: false }); - -optionalAuth = function(req, res, next) { - passport.authenticate("jwt", { session: false }, function(err, user, info) { - req.user = user; - next(); - })(req, res, next); +export const optionalAuth = { + preValidation: fastifyPassport.authenticate( + "jwt", + { session: false }, + function(err, user, info) { + req.user = user; + next(); + } + ) }; -adminAuth = ah((req, res, next) => { - passport.authenticate("jwt", { session: false }, function(err, user, info) { - if (!(user && user.admin)) return res.status(401).send("Unauthorized") - req.user = user; - next(); - })(req, res, next); -}); +export const adminAuth = { + preValidation: fastifyPassport.authenticate( + "jwt", + { session: false }, + function(err, user, info) { + if (!(user && user.admin)) return res.status(401).send("Unauthorized"); + req.user = user; + next(); + } + ) +}; -app.use(passport.initialize()); +export const auth = { + preValidation: fastifyPassport.authenticate("jwt", { + authInfo: false, + session: false + }) +}; |
