I am trying to add the accounts to a chat app i'm working on but the passport strategy for signing up is not firing.
server.js:
"use strict"
const express = require('express'),
app = express(),
http = require('http').Server(app),
expressLayouts = require('express-ejs-layouts'),
mongoose = require('mongoose'),
passport = require('passport'),
flash = require('connect-flash'),
logger = require('morgan'),
cookieParser = require('cookie-parser'),
bodyParser = require('body-parser'),
session = require('express-session'),
configDB = require('./config/database.js')
app.set('port', process.env.PORT || 3000)
mongoose.connect(configDB.url)
require('./config/passport')(passport)
app.use(logger('dev'))
app.use(cookieParser())
app.use(bodyParser.urlencoded({ extended: true }))
app.use(bodyParser.json())
app.set('view engine', 'ejs')
app.set('views', __dirname + '/views')
app.set('view options', { layout: 'layout.ejs' })
app.use('/', express.static(__dirname + '/assets'))
app.use(expressLayouts)
app.use(session({
secret: (process.env.SECRET || 'drowssap'),
resave: true,
saveUninitialized: true
}))
app.use(passport.initialize())
app.use(passport.session())
app.use(flash())
require('./app/routes')(app, passport) //SET ROUTES
require('./app/sockets')(http)
http.listen(app.get('port'), () => {
console.log('listening on port 3000')
})
passport.js:
"use strict"
const LocalStrategy = require("passport-local").Strategy,
User = require("../app/models/user")
module.exports = (passport) => {
//============================================================================
//PASSPORT SERIALIZATION =====================================================
//============================================================================
passport.serializeUser((user, done) => {
done(null, user.id)
})
passport.deserializeUser((id, done) => {
User.findById(id, (err, user) => {
done(err, user)
})
})
//============================================================================
//LOCAL SIGNUP ===============================================================
//============================================================================
passport.use("local-signup", new LocalStrategy({
nameField: "name",
emailField: "email",
passwordField: "password",
passReqToCallback: true
}, (req, name, email, password, done) => {
process.nextTick(() => {
User.findOne({ $or: [{ "email": email }, { "name": name }] }, (err, user) => {
if(err) {
return done(err)
}
if(user) {
if(user.name = name) {
return done(null, false, req.flash("signupMessage", "This username is already taken."))
} else {
return done(null, false, req.flash("signupMessage", "This email is already taken."))
}
} else {
let newUser = new User()
newUser.name = name
newUser.email = email
newUser.password = newUser.generateHash(password)
newUser.save(err => {
if(err) {
throw err
}
return done(null, newUser)
})
}
})
})
}))
//============================================================================
//LOCAL LOGIN ================================================================
//============================================================================
passport.use("local-login", new LocalStrategy({
nameField: "name",
passwordField: "password",
passReqToCallback: true
}, (req, name, password, done) => {
User.findOne({ $or: [{ name: name }, { email: email }] }, (err, user) => {
if(err) {
return done(err)
}
if(!user) {
return done(null, false, req.flash("loginMessage", "User not found."))
}
if(!user.validPassword(password)) {
return done(null, false, req.flash("loginMessage", "Wrong password."))
}
return done(null, user)
})
}))
}
signup.ejs:
<link rel="stylesheet" href="/styles/auth.css" media="screen">
<% if(message.length > 0) { %>
<div class="alert alert-danger"><%= message %></div>
<% } %>
<form action="/signup" method="post">
<div class="form-group">
<label>Name</label>
<input type="text" class="form-control" name="name">
</div>
<div class="form-group">
<label>Email</label>
<input type="text" class="form-control" name="email">
</div>
<div class="form-group">
<label>Password</label>
<input type="password" class="form-control" name="password">
</div>
<button type="submit" class="btn btn-warning btn-lg">Signup</button>
</form>
<hr>
<p>Already have an account? <a href="/login">Login</a></p>
the server request handler looks like this: app.post('/signup', isNotLoggedIn, passport.authenticate('local-signup', { successRedirect: '/chat', failureRedirect: 'signup', failureFlash: true }))
The local signup strategy is not firing at all, I tried adding console.logs in every part of the strategy and not of them are logging anything. The logger shows the post request being made to the server and then simply refreshes the resources form the signup page.
Thank you in advance for the help!
via pringon
No comments:
Post a Comment