From 3161dd7ee95b6c7d70f8341298dd4fab84cda8b0 Mon Sep 17 00:00:00 2001 From: Adam Soltys Date: Fri, 2 Sep 2022 14:47:08 -0700 Subject: es6 migration --- lib/passport.js | 63 ++++++++++++++++++++++++++++++++++++++++----------------- 1 file changed, 44 insertions(+), 19 deletions(-) (limited to 'lib/passport.js') 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 + }) +}; -- cgit v1.2.3