jaredhanson / passport

Simple, unobtrusive authentication for Node.js.
https://www.passportjs.org?utm_source=github&utm_medium=referral&utm_campaign=passport&utm_content=about
MIT License
22.93k stars 1.24k forks source link

req.isAuthenticated() is false after login #702

Closed nullRefErr closed 5 years ago

nullRefErr commented 5 years ago

Hello I switched to Passportjs to authenticate and I created simple Express server. I defined routes and local-strategy for passport but I'm always getting false as a output from req.isAuthenticated()

Edit: Changed the user object to users.

I'm using MacOs 10.14 Mojave and Node version is 8.11.4

Here is the code;

const express = require('express');
const morgan = require('morgan');
const helmet = require('helmet');
const path = require('path');
const override = require('method-override');
const bodyParser = require('body-parser');
const session = require('express-session');
const cors = require('cors');
const passport = require('passport');
const LocalStrategy = require('passport-local').Strategy;
const cookieParser = require('cookie-parser');

const app = express();
app.use(bodyParser.urlencoded({
  extended: true,
}));
app.use(bodyParser.json());
app.use(morgan('dev'));
app.use(helmet());
app.use(override());
app.use(cookieParser());
app.use(session({
  secret: 'mysupersecretpassword.i.',
  resave: false,
  saveUninitialized: true,
  cookie: { secure: true },
}));
app.use(cors());
app.use(passport.initialize());
app.use(passport.session());

const users = [
  {id: '2f24vvg', username: 'admin', password: '123'}
]

// configure passport.js to use the local strategy
passport.use('local-login', new LocalStrategy(
  { usernameField: 'username' },
  (username, password, done) => {
    console.log('Inside local strategy callback')
    // here is where you make a call to the database
    // to find the user based on their username or email address
    // for now, we'll just pretend we found that it was users[0]
    const user = users[0] 
    if(username === user.username && password === user.password) {
      console.log('Local strategy returned true')
      return done(null, user)
    }
  }
));

// tell passport how to serialize the user
passport.serializeUser((user, done) => {
  console.log('Inside serializeUser callback. User id is save to the session file store here')
  done(null, user.id);
});

app.get('/', function(req, res) {
  console.log(req.isAuthenticated());
  res.send('home index');
});

app.get('/err', function(req, res) {
  console.log(req.isAuthenticated());
  res.send('err err');
});

app.post('/login', passport.authenticate('local-login', {
  successRedirect: '/',
  failureRedirect: '/err',
}));

app.listen(3000, function() {
  console.log('server started');
});
nullRefErr commented 5 years ago

I found the solution, I did not know that we can use authenticate method as a middleware and then can call another fucntion. So I made these changes and it worked. From:

 app.post('/login', passport.authenticate('local-login', {
  successRedirect: '/',
  failureRedirect: '/err',
}));

To:

app.post('/login', passport.authenticate('local-login'), function(req, res) {
  console.log(req.isAuthenticated());
});

Issue can now be closed.