Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Doesn't work at all #9

Open
mickeyvip opened this issue Jul 28, 2017 · 7 comments
Open

Doesn't work at all #9

mickeyvip opened this issue Jul 28, 2017 · 7 comments

Comments

@mickeyvip
Copy link

mickeyvip commented Jul 28, 2017

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

@norfair00
Copy link

I have this on my app

@nikonakoneko
Copy link

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)
})

@mostertb
Copy link

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 }));

@miraclesumail
Copy link

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......

@mostertb
Copy link

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

@carlinmack
Copy link

carlinmack commented Oct 21, 2021

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

@alicecommits
Copy link

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,

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants