passport / todos-express-password

Todo app using Express and Passport for sign in with username and password.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Doesn't work at all

mickeyvip opened this issue · comments

I am trying to use Passport (v0.3.2) and LocalStrategy, I have copied the example code as is and it does not work at all.

I am not getting any errors, but the page keeps loading login every time I hit "Submit".

Putting console.log() inside the function that should find the users has no effect, seems that the function is never called.

The headers:
image

I have this on my app

What am I doing wrong?
Same here, the verify function is never executed with this code:

require('dotenv').config()
const next = require('next')
const express = require('express')
const session = require('express-session')
const passport = require('passport')
const LocalStrategy = require('passport-local')
const uid = require('uid-safe')

const dev = process.env.NODE_ENV !== 'production'
const app = next({
  dev,
  dir: './src'
})

const nextHandle = app.getRequestHandler()

app.prepare().then(() => {
  const server = express()
  const sessionConfig = {
    secret: uid.sync(18),
    cookie: {
      maxAge: 86400 * 1000 // 24 hours in milliseconds
    },
    resave: false,
    saveUninitialized: true
  }
  server.use(session(sessionConfig))

  passport.use(new LocalStrategy(function(username, password, done) {
    console.log(`login ${username}:${password}`)
    if (username === process.env.USERNAME && password === process.env.PASSWORD)
      return done(null, {username})
    done(null, false)
  }))
  passport.serializeUser((user, done) => done(null, user))
  passport.deserializeUser((user, done) => done(null, user))
  server.use(passport.initialize())
  server.use(passport.session())

  const restrictAccess = (req, res, next) => {
    if (!req.isAuthenticated()) return res.redirect('/login')
    next()
  }

  server.post('/login', passport.authenticate('local', { successRedirect: '/', failureRedirect: '/login' }), (req, res) => {
    res.redirect('/')
  })
  server.get('*', nextHandle)

  server.listen(process.env.PORT)
})

For anyone else that runs into this issue, the body-parser package needs to be configured on your Express App.

Specifically adding the following line from the example resolved this issue for me:

app.use(require('body-parser').urlencoded({ extended: true }));

For anyone else that runs into this issue, the body-parser package needs to be configured on your Express App.

Specifically adding the following line from the example resolved this issue for me:

app.use(require('body-parser').urlencoded({ extended: true }));

this make no sense already shown in code......

this make no sense already shown in code......

Yes the use of the body-parser is in the example contained in this repo. If, however, you mistakenly skip this part of the example, you get the behavior described in this issue

Can someone link to the line in the repository? I can't find it

edit: you can use:

app.use(
    express.urlencoded({
        extended: true,
    })
);

instead of requireing body-parser

commented

Hi,
I know it's been a while, but does it still not work? Does anybody know if this issue was fixed? If yes, how?
Thanks in advance,