uknj
uknj

Reputation: 89

Forward a PassportJS to a form authentication to add email, password etc

So I have followed this tutorial. I finished the whole series and the application is working fine. However I would like for users who signup using a social network eg. Facebook etc. to be redirected to a seperate form where they can fill their name(s) email etc. Couple of questions.

  1. Would I have to change the database schema to allow 'global'(is this the correct term to use?) values for email and name etc. from the facebook/twitter specific stuff. Like so:

    var userSchema = mongoose.Schema({
    
    email            : String,
    name             : String
    password         : String,
    
    facebook         : {
        id           : String,
        token        : String,
    },
    
    twitter          : {
        id           : String,
        token        : String,
        displayName  : String,
        username     : String,
    },
    
    google           : {
        id           : String,
        token        : String,
    }
    
    });
    

What then would be the correct route? I currently have:

        // TWITTER ROUTES
// route for facebook authentication and login
app.get('/auth/twitter', passport.authenticate('twitter'));

// handle the callback after twitter has authenticated the user
app.get('/auth/twitter/callback',
passport.authenticate('twitter', { failureRedirect: '/signup' }),
function(req, res) {
// The user has authenticated with Twitter.  Now check to see if the profile
// is "complete".  If not, send them down a flow to fill out more details.
if (req.user.isCompleteProfile()) {
  res.redirect('/profile');
} else {
  res.redirect('/complete-profile');
}});

app.get('/complete-profile', function(req, res) {
    res.render('profile-form', { user: req.user });
});

app.post('/update-profile', function(req, res) {
    // Grab the missing info from the form and update the profile.
    res.redirect('/home');
});

// route middleware to make sure a user is logged in
function isLoggedIn(req, res, next) {

// if user is authenticated in the session, carry on
if (req.isAuthenticated ())
    return next();

// if they arent redirect them to the home page
res.redirect('/');
}

I took it from this StackOverflow.

How would I define the function isCompleteProfile? Currently I have something like this:

function isCompleteProfile(req, res, next) {

// if user has completed profile, carry on
if (req.isComplete ())
    return next();

// if they arent redirect to complete-profile
res.redirect('/complete-profile');
}

The first link (to the tutorial) has some suggestions by the author on how to implement this, in the comment section below the article if anyone needs it. However I have mainly been looking at the SO link.

Obviously it is not working (I just copied the earlier function for isLoggedIn. Creating views should (hopefully) not be too difficult. So if anyone can shed some light on how I should solve this issue it will be greatly appreciated.

Thanks Uknj

PostScript: Is there also potentially an alternative method? Where I do not use a separate function in the route and just redirect them to another page directly? Not sure how I would do this either, so help would be appreciated.

Upvotes: 1

Views: 129

Answers (1)

Peter Robinson
Peter Robinson

Reputation: 383

See my clarification back on Using Everyauth/Passport.js to authenticate with Twitter whilst asking for username/email/password for an explanation as to how to set up the isCompleteProfile test.

Upvotes: 1

Related Questions