'Passport authentication get the name of incoming route
Currently I have the following configuration for passport. Here, each login request is coming from /login/students. The getUserById function finds the user from a STUDENT table stored in database. But login requests can also come from /login/instructors, and in that case I would like to find users from INSTRUCTORS table. But currently there is no way to check the incoming route's name. How would I know that from which route my login request is coming from? Any help appreciated :)
const initialize = (passport) => {
passport.use(new LocalStrategy({ usernameField: 'username', passwordField: 'password' }, authenticateUser));
passport.serializeUser((user, done) => done(null, user.ID));
passport.deserializeUser(async (id, done) => {
const user = await getUserById(id);
done(null, user);
});
};
Code for login router:
router
.route('/login/students')
.post(
checkAuth.not, //login only if user is not in current session
passport.authenticate('local', {
successRedirect: '/home/students',
failureRedirect: '/login/students',
})
);
Solution 1:[1]
Looks like I found out the solution. Just had to set passReqToCallback to true.
passport.use(new LocalStrategy({ usernameField: 'username', passwordField: 'password' }, authenticateUser));
Then from authencateUser, I could access the req param like this:
const authenticateUser = async (req, username, password, done) => {
console.log(req.url); //prints from which port this login request came
//blah blah blah do da di do
};
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | stackneverflow |
