I have no idea where to begin, I've used passport for years now and have never encountered an issue with this simplistic and useful library... until now. Firstly, I have two strategies: 'client-register' and 'client-login'.
});
router.post('/login', passport.authenticate('client-login'), (req, res) => {
let user = req.user;
if(!user) {
res.sendStatus(401);
return;
} else {
res.send(user).status(200);
}
});
`
^ for the above, I've tried using passport.authenticate('client-register') as both a middleware component in the express-endpoint like how it is used in 'client-login', got the same results.
Seriously? What gives? I have no idea why it's not deserializing my registration cookie but it is for consecutive logins.... a cookie is save upon registration but it is never utilized in a credentials: 'include' api call.... WTF!
I have no idea where to begin, I've used passport for years now and have never encountered an issue with this simplistic and useful library... until now. Firstly, I have two strategies: 'client-register' and 'client-login'.
passport.use('client-register', new LocalStrategy({ usernameField: 'email', passwordField: 'password', passReqToCallback: true }, async (req, email, password, done) => { const user = await Client.findOne({ where: { email: email }}); if(user) { done(null, false, { message: 'This email is already registered.'}); } else { let newUser = await Client.create({ email: email, password: password, firstName: req.body.first_name, lastName: req.body.last_name }); done(null, newUser); } }));
`passport.use('client-login', new LocalStrategy({ usernameField: 'email', passwordField: 'password', passReqToCallback: true }, async (req, email, password, done) => { const user = await Client.findOne({ where: { email: email }}); if(user) { let isValid = await user.validPassword(password);
}));`
my serializations: Keep in mind, I have duplicates of the above strategies for separate account types.
`passport.serializeUser((user, done) => { if (user instanceof Client) { done(null, { id: user.id, type: 'Client'}); } else { done(null, { id: user.id, type: 'Vendor'}) } });
passport.deserializeUser(async (user, done) => { if(user.type === 'Client') { Client.findByPk(user.id).then(client => { done(null, client); }).catch(err => { if(err) { done(err); } }); } else { Vendor.findByPk(user.id).then(vendor => { done(null, vendor); }).catch(err => { if(err) { done(err); } }); } });`
Here are my login / register endpoints using the passport.authenticate function. `router.post('/register', (req, res, next) => { passport.authenticate('client-register', (err, user, info) => { if(err) { next(err); }
}); router.post('/login', passport.authenticate('client-login'), (req, res) => { let user = req.user; if(!user) { res.sendStatus(401); return; } else { res.send(user).status(200); } }); ` ^ for the above, I've tried using passport.authenticate('client-register') as both a middleware component in the express-endpoint like how it is used in 'client-login', got the same results.
Here is my configuration:
`const sessionMiddleware = session({ name: 'user_sid', secret: process.env.SESSION_SECRET, resave: true, saveUninitialized: true });
//Express Config app.use(express.urlencoded({ extended: true})); app.use(express.json()); app.use(cors({ credentials: true, origin: 'http://localhost:3000' })); app.use(flash()); app.use(sessionMiddleware);
//PassPort app.use(passport.initialize()); app.use(passport.session());`
Seriously? What gives? I have no idea why it's not deserializing my registration cookie but it is for consecutive logins.... a cookie is save upon registration but it is never utilized in a credentials: 'include' api call.... WTF!