Voting App project, passport library and rendering of template

I have in my file of routes index.js the different routes for the requests. So:

function(app,passeport){
      function isLoggedIn (req, res, next) {
	        if (req.isAuthenticated()) {
		    return next();
		 } else {
		res.redirect('/login');			
	  }
  }
    .......
     app.route('/login')
	       .get(function (req, res) {
		           res.render("login")
	         });
   .........
   //more routes
}`

I tried to substitute in the public views the HTML files for PUG TEMPLATES.
So, instead the HTML

res.sendFile(path + '/public/login.html'); // View with a HTML

The PUG template

res.render("login") // Render with a pug template

No problem for that, the pug template works and render the HTML
The problem is with the req.Authenticated() method of the js passport library in the isLoggedIn express middleware function. It returns false when using the template (with the HTML returns true). So, I cannot access the rest of the routes as they are conditional to req.Authenticate() true

I use the isLoggedIn function as a middleware for a profile route (or any authenticated page route for that matter) and separate the function itself out. So for what you have above would look something like this:

function(app,passport){

    .......
     app.route('/login')
	       .get(function (req, res) {
		           res.render("login")
	         });
   .........
     app.route('/profile')
	       .get(isLoggedIn,function (req, res) {
		           res.render("User has successfully authenticated ")
	         });
   ...........
   //more routes
}`

and the isLoggedIn function

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

In other words an unsuccesful login is handled by the isLoggedIn function as it will not execute the next() call back and hence will not go to the profile page.

This is the initial reference I used for the passport authentication model and modified / customized for my own needs from there:

Yes, that is what I did and it works perfect when in the public views folder I have html files and the response in the routes are res.sendFile()

app/routes/index.js
      function(app,passeport){
                function isLoggedIn (req,res,next){ ...}

                app.route('/login')
	                    .get(function (req, res) {
		                          res.sendFile(path + '/public/login.html');
	                    });
                app.route('/profile')
                               .get(isLoggedIn,function(req,res){
                                       res.sendFile(path+'/public/profile.html')
                                 }
                  }

public
----login.html
----profile.html

The problem comes when instead of the html files I have .pug templates after setting the app for pug:

app.js
  app.set('views',__dirname+'/views')
  app.set('view engine','pug')

app/routes/index.js
      function(app,passeport){
           app.route('/login')
	               .get(function (req, res) {
		                  res.render('login')
	                    });
                app.route('/profile')
                               .get(isLoggedIn,function(req,res){
                                       res.render('profile')
                                 }
       }
public
-----login.pug
-----profile.pug

I can render the login file, but not the profile one because the req.Authenticate() method of the isLoggedIn function returns false.

Looks to me like an issue with your view engine rendering and not with the .Authenticate() method, I never use pug or other view engines , but out of curiosity what does your profile.pug file look like ? and what parameters are you passing to it ? per the docs, don’t you need to pass another object to res.render() as well?

https://expressjs.com/en/guide/using-template-engines.html

Perhaps somebody more versed on view engines can help out.

I checked the routes.
It has been a confusion. When referencing the route of the profile file from the login file instead of referencing ‘/auth/github’
I have been referencing ‘’/profile’.
In ‘login.pug’

li.nav-item
            a.nav-link.active(href='/auth/github') Sign In

An not

 li.nav-item
            a.nav-link.active(href='/profile') Sign In