0

I am using passport.js for user authentication, user is successfully logging in. and now I try to change the navigation links i.e Login to logout after user successfully logged in.

for that I am using the following middleware in app.js file

  var express=require('express');
var path=require('path');
var bodyParser=require('body-parser');
var cookieParser=require('cookie-parser');
var passport=require('passport');
var LocalStrategy=require('passport-local').Strategy;
var expressSession=require('express-session');
var app=express();
require('./db');
    var userModel=require('./models/usermodel');
    var adminModel=require('./models/adminmodel');

app.use(function (req, res, next) {
  res.locals.login = req.isAuthenticated();
  next(); });


//Configuring Routes
var routes = require('./routes/index');
var register = require('./routes/register');
var login = require('./routes/login');
var admin = require('./routes/admin');
var addblog = require('./routes/addblog');
var admindashboard = require('./routes/admindashboard');
var adminviewblog = require('./routes/admin-view-blog');
var viewblog = require('./routes/view-blog');

// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');

// app uses configuration
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));

app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));
app.use(express.static(path.join(__dirname, 'routes/uploads')));
app.use(expressSession({ secret: 'keyboard catoard cat' }));
app.use(passport.initialize());
app.use(passport.session());


app.use('/', routes);
app.use('/register', register);
app.use('/login', login);
app.use('/admin', admin);
app.use('/addblog', addblog);
app.use('/admindashboard', admindashboard);
app.use('/admin-view-blog', adminviewblog);
app.use('/view-blog', viewblog);

//passport configuration

   /*=======passport configuration========*/
passport.use("userss", new LocalStrategy(userModel.authenticate()));
passport.use("adminn", new LocalStrategy(adminModel.authenticate()));
passport.serializeUser(function(user, done){
    done(null, user.id);
    });
passport.deserializeUser(function(id, done){
    userModel.findById(id, function(err, user){
        done(err, user);
        });

    });
    passport.deserializeUser(function(id, done){
    adminModel.findById(id, function(err, user){
        done(err, user);
        });

    });


//create server
app.listen(3000, function(){
    console.log('Server is running  localhost:3000');
    });

and in ejs template

<% if login %>
        <li><a href="/logout">Logout</a></li>
        <% else %> 
         <li><a href="/login">Login</a></li>
        <li><a href="/register">Register</a></li> 
        <% endif %>

But its not working. the page showing the following errors.

SyntaxError: Unexpected identifier in F:\saurabh_sharma\nodejs\blogsystem\views\index.ejs while compiling ejs
   at Function (native)
   at Object.Template.compile (F:\saurabh_sharma\nodejs\blogsystem\node_modules\ejs\lib\ejs.js:464:12)

I googled this problem but didn't find the solution.

If someone have idea about this please let me know.

Thanks.

Saurabh Sharma
  • 804
  • 6
  • 16
  • 42
  • I dont think you should have two definitions of `deserializeUser()` – Avantika Saini May 30 '16 at 11:02
  • ok...after removing extra deserializeUser(), still its not working – Saurabh Sharma May 30 '16 at 11:38
  • what is your code for passport strategies?? – Avantika Saini May 30 '16 at 11:57
  • this is the passport strategy i am using passport.use("userss", new LocalStrategy(userModel.authenticate())); passport.use("adminn", new LocalStrategy(adminModel.authenticate())); passport.serializeUser(function(user, done){ done(null, user.id); }); passport.deserializeUser(function(id, done){ userModel.findById(id, function(err, user){ done(err, user); }); }); passport.deserializeUser(function(id, done){ adminModel.findById(id, function(err, user){ done(err, user); }); }); – Saurabh Sharma May 30 '16 at 12:19

3 Answers3

0

Incorrect!

<% if(true){ %>
<h1>foo</h1>
<% } %>
<% else{ %>
<h1>bar</h1>
<% } %>

Correct

<% if(true){ %>
<h1>foo</h1>
<% } else{ %>  
<h1>bar</h1>
<% } %>

Please structure your conditionals correctly.

Reference: Can I use conditional statements with EJS templates (in JMVC)?

Community
  • 1
  • 1
Avantika Saini
  • 792
  • 4
  • 9
  • <%if (login == false){%>
  • Login
  • Register
  • <% } else { %>
  • Logout
  • <% } %> – Avantika Saini May 30 '16 at 10:20
  • I think there is something missed in middleware of authenticating the user. – Saurabh Sharma May 30 '16 at 10:25
  • does _not working_ means that both the links are visible on your page? Try to print the value of `login` after you login your user. If it is always `false` then there is some problem in your middleware – Avantika Saini May 30 '16 at 10:28
  • both conditions are not printing..only the if condition is printing in both action in while user logged in or not – Saurabh Sharma May 30 '16 at 10:35
  • it seems the problem of your middleware then. If you need help debugging it then please update your question with its code – Avantika Saini May 30 '16 at 10:39