phonegap ajax user authentication with nodejs-expresss-mongo-passportjs

Alex Source

I have already built a basic node.js user authentication system based on node.js, express.js, passport-local.

I store my username and passwords in a mysql database and I use mongo for persistent storage for the sessions. I now want to move the user registration and login to phonegap.

From the tutorials I have found online, the only way that seems work is is AJAX user authentication. However I have two questions:

  1. How do I rewrite the express routes to respond JSON since passport.js relies on redirects?

    // process the signup form'/register', passport.authenticate('local-signup', {
            successRedirect : '/home', 
            failureRedirect : '/register', 
            failureFlash : true // allow flash messages

    // process the login form'/login', passport.authenticate('local', {
        successRedirect : '/home', 
        failureRedirect : '/login', 
        failureFlash : true // allow flash messages

    and in my strategies I have :
    passport.use('local-signup', new LocalStrategy({ usernameField : 'email', passwordField : 'password', passReqToCallback : true }, function(req, email, password, done) { ... rest of the code that queries the db

    also for login
    //Configure passport Local Strategy for login passport.use(new LocalStrategy( function(username, password, done) { var query = 'select * from users where email = '+ connection.escape(username); connection.query(query, function (err, user) { if (err) { return done(err); ... rest of code }

2.Will the AJAX authentication in PhoneGap work by sending a post to '/login' and therefore creating a new active session in the express server?

3.How do I handle state in the client. In a normal webapp you use redirects for ie. failed login attempts, logout, etc. In an AJAX authentication how do you handle that? Do you return an status code, return new markup, update part of the view?



answered 4 years ago Alex #1

I will close this question as I did some research and my original problem was from my lack of understanding of how phonegap apps are architected. I wasn't aware that I need to follow the single page app architecture vs the traditional web page model.

comments powered by Disqus