Skip to content

Instantly share code, notes, and snippets.

@theptrk theptrk/app.js
Last active Jan 22, 2019

Embed
What would you like to do?
local authentication

Local authentication

  • Install dependencies
$ npm install --save body-parser connect-flash express-session bcryptjs passport passport-local
  • [ ]
app.use(session({ secret: "whatwhatwhatwhatwhat" }));
app.use(passport.initialize());
app.use(passport.session());
app.use(flash());
app.use(logger("dev"));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, "public")));
app.use(session({ secret: "ilovetodidlist" }));
// authman init needs to be invoked AFTER app.use(session)
authman.init(app);
const express = require("express");
const flash = require("connect-flash");
const bcrypt = require("bcryptjs");
const passport = require("passport");
const LocalStrategy = require("passport-local").Strategy;
const User = require("../models").User;
const router = express.Router();
passport.serializeUser((user, done) => done(null, user.id));
// used to deserialize the user
passport.deserializeUser((id, done) => {
User.findById(id, (err, user) => done(err, user));
});
router.get("/signup", (req, res) => {
res.render("signup", { message: req.flash("signupMessage") });
});
// Step 1: Sign up
const localSignUpName = "local-signup";
passport.use(
localSignUpName,
new LocalStrategy(
{
usernameField: "email",
passwordField: "password"
},
(email, rawPassword, done) =>
User.find({ where: { email } })
.then(user => {
if (user) {
console.log("user already exists");
return done(null, false);
}
const saltRounds = 12;
const salt = bcrypt.genSaltSync(saltRounds);
const password = bcrypt.hashSync(rawPassword, salt);
return User.create({ email, password })
.then(user => done(null, user))
.catch(done);
})
.catch(done)
)
);
router.post(
"/signup",
passport.authenticate(localSignUpName, {
successRedirect: "/profile",
failureRedirect: "/signup"
})
);
router.get("/login", (req, res) => {
res.render("login", { message: req.flash("loginMessage") });
});
const init = app => {
app.use(passport.initialize());
app.use(passport.session());
app.use(flash());
app.use("/", router);
};
module.exports = { init };
"use strict";
module.exports = {
up: (queryInterface, Sequelize) => {
return queryInterface.createTable("Users", {
id: {
allowNull: false,
autoIncrement: true,
primaryKey: true,
type: Sequelize.INTEGER
},
firstName: {
type: Sequelize.STRING
},
lastName: {
type: Sequelize.STRING
},
email: {
type: Sequelize.STRING,
allowNull: false
},
email_lowerCased: {
type: DataTypes.STRING,
allowNull: false,
unique: true
},
password: {
type: Sequelize.STRING,
allowNull: false
},
createdAt: {
allowNull: false,
type: Sequelize.DATE
},
updatedAt: {
allowNull: false,
type: Sequelize.DATE
}
});
},
down: (queryInterface, Sequelize) => {
return queryInterface.dropTable("Users");
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.