Reputation: 99
I want to have 2 different instances of passport Js for my app (user and admin - both with jwt authentification ). I read in the official documentation that the way to distinguish between them is to name them.
What am I missing? Am i missing an export or something ?
var Passport = require('passport').Passport,
passport_authorised = new Passport();
const JwtStrategy = require("passport-jwt").Strategy,
ExtractJwt = require("passport-jwt").ExtractJwt;
const opts ={}
require('dotenv').config();
const databaseServiceAdmins = require('../../services/database_service_admin')
opts.jwtFromRequest = ExtractJwt.fromAuthHeaderWithScheme('Bearer')
opts.secretOrKey = process.env.ACCESS_TOKEN_SECRET
passport_authorised.use("admin-rule",new JwtStrategy(opts,async function (jwt_payload, done) {
const doesUserExists = await databaseServiceAdmins.adminExists(jwt_payload.email)
if (doesUserExists.success === true && jwt_payload.access_rights === "privileged")
done(null, doesUserExists.item)
else
done(null, false)
}));
const express = require("express")
const router = express.Router()
const adminDatabaseService = require('../../services/database_service_admin')
const jwt = require("jsonwebtoken");
const {callback} = require("pg/lib/native/query");
const passport_authorised = require("passport");
require('../json_authorisation/passport_authorised')
const {approvePtoRequestByAdmin, dropOnePtoDay} = require("../../services/database_service_admin");
const bcrypt = require("bcrypt");
router.get("/all/not-approved",passport_authorised.authenticate("admin-rule",{session:false},callback),
async (req,res)=>{
const aux = await adminDatabaseService.getAllNotApprovedPtos()
return res.status(200).send(
aux
)
})
module.exports = router;
Error is:
Error: Unknown authentication strategy "admin-rule" at attempt (/home/radu/Desktop/licenta/backend/node_modules/passport/lib/middleware/authenticate.js:193:39) at authenticate (/home/radu/Desktop/licenta/backend/node_modules/passport/lib/middleware/authenticate.js:370:7) at Layer.handle [as handle_request] (/home/radu/Desktop/licenta/backend/node_modules/express/lib/router/layer.js:95:5) at next (/home/radu/Desktop/licenta/backend/node_modules/express/lib/router/route.js:137:13) at Route.dispatch (/home/radu/Desktop/licenta/backend/node_modules/express/lib/router/route.js:112:3) at Layer.handle [as handle_request] (/home/radu/Desktop/licenta/backend/node_modules/express/lib/router/layer.js:95:5) at /home/radu/Desktop/licenta/backend/node_modules/express/lib/router/index.js:281:22 at Function.process_params (/home/radu/Desktop/licenta/backend/node_modules/express/lib/router/index.js:341:12) at next (/home/radu/Desktop/licenta/backend/node_modules/express/lib/router/index.js:275:10) at Function.handle (/home/radu/Desktop/licenta/backend/node_modules/express/lib/router/index.js:174:3)
Upvotes: 1
Views: 592
Reputation: 99
Instead of having 2 JWT functionalities, one for admin and one for the unprivileged user, try to add in your JWT some roles (admin/user in our case). And sign the JWT with a secret key.
On a middleware when you decode JWT, validate it, and after check what role is in it. And allow/don't allow to pass.
Upvotes: 1