Writing some code to authenticate users using passport, so far using passport-linkedin-oauth2, passport-google-oauth2, passport-facebook and passport-twitter.
OK, which ones I use isn't that important, but may be useful to know. What I am trying to do is to figure out a simple way to pass variables to the backend code to make decisions on. So for example. Currently, the system only makes its decission base on weather or not the authentication works. If you go to sign-up or login, and you press the google button, you will be logged in.
However, I want the code to be able to determine that the request comes from the signup (in which case a new user is a success, a existing user should send back a code and message stating "User already registered"). Same with login. Where "existing" user is a clear success, however, if you press "login" and its a new user, it will give a message back stating "User not registered", which allows the front end to present a nice button stating "Register user".
My problem is, whats a smooth way to pass this to the backend. The two routes for the call currently are:
// =====================================
// Google ROUTES =====================
// =====================================
// route for google authentication and login
router.get('/at/google', passport.authenticate('google', { scope : 'email' }));
// handle the callback after Google has authenticated the user
router.get('/cb/google',
passport.authenticate('google', {
successRedirect : '/',
failureRedirect : '/login',
failureFlash: true //This is where we send the failed logins
}));
Works perfectly, but they do call the same object, as I am really REALLY lazy, and wants a single object to handle all.
I tired setting a session value in the previous page (the signup page):
router.get('/login', function(req, res) {
req.session.passportOpType = "login";
res.render('login.ejs', { title: 'Betchanow - Social betting as it should be' , loginUrl: cfgWebPage.loginUrl, trackingID: cfgWebPage.googleTracking.trackingID, message: req.flash('loginMessage') });
});
router.get('/signup', function(req, res) {
req.session.passportOpType = "signup";
res.render('signup.ejs', { title: 'Betchanow - Social betting as it should be' , loginUrl: cfgWebPage.loginUrl, trackingID: cfgWebPage.googleTracking.trackingID, message: req.flash('signupMessage') });
});
And then pick that up on the google end of passport
// =========================================================================
// GOOGLE ==================================================================
// =========================================================================
passport.use(new GoogleStrategy({
clientID : configAuth.googleAuth.clientID,
clientSecret : configAuth.googleAuth.clientSecret,
callbackURL : configAuth.googleAuth.callbackURL,
passReqToCallback : true
},
function(request, accessToken, refreshToken, profile, done) {
// make the code asynchronous
// User.findOne won't fire until we have all our data back from Google
process.nextTick(function(req) {
console.log("This is a " + req.session.authtype)
However, the value is undefined at this stage. This may be because its the callback.
Not certain if the session storage is kept. My question now though is. How do I accomplish this in a straight forward manner, preferably independent on which passport type I use?