Closed vishal764 closed 4 years ago
Sorry, github issues are for bugs or problems with the passport-jwt module. I can't provide debugging or help implementing your application.
Consider opening a question on Stack Overflow or looking into the many available tutorials.
//this is my code var express = require("express"); var bodyParser = require("body-parser"); var jwt = require("jsonwebtoken"); var passport = require("passport"); var passportJWT = require("passport-jwt"); var { ExtractJwt } = passportJWT; var Strategy = passportJWT.Strategy;
var jwtSecret = "yoursecretkeyishere"; var params = { secretOrKey: jwtSecret, jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken(), };
var app = express();
app.use(function (req, res, next) { res.header("Access-Control_Allow-Origin", "*"); res.header( "Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept" ); res.header("Access-Control-Expose-Headers", "X-Auth-Token"); });
app.use(bodyParser.json()); passport.initialize();
let users = [ { id: "A9", email: "abc@15", password: "abc@123" }, { id: "A8", email: "abc@1", password: "abc@123" }, ];
var strategy = new Strategy(params, function (payload, done) { let id = users.findIndex((obj) => payload.id === obj.id); var user = users[+id] || null; if (user) { return done(null, { id: user.id, }); } else { return done(new Error("User not found"), null); } });
passport.use(strategy); const jwtExpirySeconds = 300;
app.post("/user", function (req, res, payload) { if (req.body.email && req.body.password) { var email = req.body.email; var password = req.body.password;
} else { res.sendStatus(401); } });