fancy
fancy

Reputation: 51383

How can I check if a user is already logged in? (everyauth, node.js)

Up until now I've been using client-side auth. I just set up server-side everyauth and it is working great but how can I do something like FB.getLoginStatus(what I did client-side) when the page loads?

If the user is already logged in I dont want to put them through the process again.

Thanks for explaining!

Upvotes: 3

Views: 5007

Answers (3)

Anmol Saraf
Anmol Saraf

Reputation: 15743

In case you are using Jade template, user's login status can be checked by following code inside a .jade file, for example - home.jade

if (!everyauth.loggedIn) // <-----
    a(href="/your_url_here/") log in with facebook
else
    p Welcome <username>

Upvotes: 0

Jesse Fulton
Jesse Fulton

Reputation: 2218

From the documentation

everyauth also provides convenience methods on the ServerRequest instance req. From any scope that has access to req, you get the following convenience getters and methods:

req.loggedIn - a Boolean getter that tells you if the request is by a logged in user

req.user - the User document associated with the session

req.logout() - clears the sesion of your auth data

Within Express Views:

If you are using express, everyauth comes with some useful dynamic helpers. To enable them:

var express = require('express')
, everyauth = require('everyauth') 
, app = express.createServer();
everyauth.helpExpress(app); 

Then, from within your views, you will have access to the following helpers methods attached to the helper, everyauth:

everyauth.loggedIn

Upvotes: 3

Benny Tjia
Benny Tjia

Reputation: 4883

I would do something like this on node.js:

var userlogin = function () {
       var req = this.request
          ,res = this.response
          ,login = req.body
          ,username = login.username
          ,password = login.password;

      if(username === 'tester' && password === '12345'){
         req.session.user_id = xxxxxx;
         res.redirect('/');
      }else{
        res.redirect('/login');
      }

    }
    var checkAuth = function(fn_auth, fn_unauth){
      var req = this.request
          ,res = this.response
          ,unauthCallback;

      if(req.session.user_id){
        fn_auth(); //execute fn_auth if user is logged in. callback can be set to redirect to root path '/'
      }else{
          fn_unauth(); //execute fn_unauth calback which bring user to login path '/login'
        } 
      }
    }

Basically just redirect when the user is already logged in. Hope that helps?

Upvotes: 0

Related Questions