How can I tell to EJS if user is logged in?

CJP

I'm trying to check if a user is logged in to display specifc content. I have this:

app.get('/profile', isLoggedIn, (req, res) => {
    res.render('profile', {
        user: req.user, isLoggedIn: isLoggedIn()
    });
});


function isLoggedIn(req, res, next) {
if (req.isAuthenticated()) {
    return next(null, true);
}
res.redirect('/');
}

And as template:

<%if (isLoggedIn) { %>
 <div>Content 1</div>
<% } %>

But I get this error:

Cannot read property 'isAuthenticated' of undefined

What am I doing wrong?

Hardik Shah

isAuthenticated of undefined meaning req is undefined.

here you are doing mistake.

user: req.user, isLoggedIn: isLoggedIn()

You can not call middleware function like this. isLoggedIn()

What you can do is:

app.get('/profile', isLoggedIn, (req, res) => {
     res.render('profile', {
          user: req.user, isLoggedIn: req.isLogged
     });
});


function isLoggedIn(req, res, next) {
     if (req.isAuthenticated()) {
        req.isLogged = true
        return next();
     }
     res.redirect('/');
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How can I tell if a user is *not* logged in via Google Sign-In (OAuth2)?

From Dev

How can I tell if a user is *not* logged in via Google Sign-In (OAuth2)?

From Dev

How can I tell if somebody is logged in with Temp Profile?

From Dev

How to tell when I logged in?

From Dev

How can I check if a user is logged in in Symfony?

From Dev

How to tell if user logged in via Facebook

From Dev

How to tell if user logged in via Facebook

From Dev

How can I tell which user limit I am running into?

From Dev

PHP - How can i keep my user logged in?

From Dev

How can I test methods which needs user to be logged

From Dev

How can I send a notification in Laravel to a user that is not currently logged in?

From Dev

How can I get the username of the logged in user in Django?

From Dev

How can I view or hide some data depending of user logged in or not?

From Dev

How can I create a ParseUser from an already logged in Facebook user?

From Dev

How can I prevent access to an Angular page if the user IS logged in with AngularFire?

From Dev

How can I tell who is actively logged on (locally or remotely) a Windows 7 PC?

From Dev

How to tell when a user last logged in without using the command last?

From Dev

How to tell programmatically the last time a user logged in to a webmail account?

From Dev

How can I get the user_id from the user currently logged in?

From Dev

How can I get the user_id from the user currently logged in?

From Dev

How can I keep user keep user logged in RingCentral while using Authorization code flow?

From Dev

Laravel 5.2 Authentication - How can I show logged in user's name and the logout link in every page?

From Dev

How can I reliably link to a Gmail conversation given a thread ID if the user is logged into multiple accounts?

From Dev

How can I check if my user is already logged in (session), and go to specific ViewController?

From Dev

How can I use a global PHP file to keep track of whether or not the user is logged in?

From Dev

How can I get the specific fields of the currently logged-in user in MVC5?

From Dev

how can i list svn folders with the logged in user credential in jenkins for build parameters?

From Dev

How can i check if user is logged in from the MVC5 Layout file

From Dev

In Meteor, how can i identify connection / a user of a site on the server, regardless of whether or not they're logged in?

Related Related

  1. 1

    How can I tell if a user is *not* logged in via Google Sign-In (OAuth2)?

  2. 2

    How can I tell if a user is *not* logged in via Google Sign-In (OAuth2)?

  3. 3

    How can I tell if somebody is logged in with Temp Profile?

  4. 4

    How to tell when I logged in?

  5. 5

    How can I check if a user is logged in in Symfony?

  6. 6

    How to tell if user logged in via Facebook

  7. 7

    How to tell if user logged in via Facebook

  8. 8

    How can I tell which user limit I am running into?

  9. 9

    PHP - How can i keep my user logged in?

  10. 10

    How can I test methods which needs user to be logged

  11. 11

    How can I send a notification in Laravel to a user that is not currently logged in?

  12. 12

    How can I get the username of the logged in user in Django?

  13. 13

    How can I view or hide some data depending of user logged in or not?

  14. 14

    How can I create a ParseUser from an already logged in Facebook user?

  15. 15

    How can I prevent access to an Angular page if the user IS logged in with AngularFire?

  16. 16

    How can I tell who is actively logged on (locally or remotely) a Windows 7 PC?

  17. 17

    How to tell when a user last logged in without using the command last?

  18. 18

    How to tell programmatically the last time a user logged in to a webmail account?

  19. 19

    How can I get the user_id from the user currently logged in?

  20. 20

    How can I get the user_id from the user currently logged in?

  21. 21

    How can I keep user keep user logged in RingCentral while using Authorization code flow?

  22. 22

    Laravel 5.2 Authentication - How can I show logged in user's name and the logout link in every page?

  23. 23

    How can I reliably link to a Gmail conversation given a thread ID if the user is logged into multiple accounts?

  24. 24

    How can I check if my user is already logged in (session), and go to specific ViewController?

  25. 25

    How can I use a global PHP file to keep track of whether or not the user is logged in?

  26. 26

    How can I get the specific fields of the currently logged-in user in MVC5?

  27. 27

    how can i list svn folders with the logged in user credential in jenkins for build parameters?

  28. 28

    How can i check if user is logged in from the MVC5 Layout file

  29. 29

    In Meteor, how can i identify connection / a user of a site on the server, regardless of whether or not they're logged in?

HotTag

Archive