diff options
| author | Adam Soltys <[email protected]> | 2022-09-02 14:47:08 -0700 |
|---|---|---|
| committer | Adam Soltys <[email protected]> | 2022-09-02 14:47:14 -0700 |
| commit | 3161dd7ee95b6c7d70f8341298dd4fab84cda8b0 (patch) | |
| tree | 16cd6c55d5dc481331b1e8b640d179f193e7ea13 /lib/passport.js | |
| parent | 7a8d64833de8873afb92982b3f5bfff59b30c542 (diff) | |
| download | coinos-server-3161dd7ee95b6c7d70f8341298dd4fab84cda8b0.tar.gz coinos-server-3161dd7ee95b6c7d70f8341298dd4fab84cda8b0.zip | |
es6 migration
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 + }) +}; |
