Create login page
This commit is contained in:
parent
9c7241e7e6
commit
a6fe6d6c72
3 changed files with 43 additions and 12 deletions
|
@ -1,26 +1,30 @@
|
||||||
const database = require('./../database');
|
const database = require('./../database');
|
||||||
const passport = require('passport');
|
const passport = require('passport');
|
||||||
const passportLocal = require('passport-local');
|
const localStrategy = require('passport-local').Strategy;
|
||||||
const bcrypt = require('bcrypt');
|
const bcrypt = require('bcrypt');
|
||||||
|
|
||||||
passport.use(new passportLocal.Strategy((email, password, cb) => {
|
|
||||||
|
passport.use(new localStrategy({
|
||||||
|
usernameField: 'email',
|
||||||
|
passwordField: 'password'},
|
||||||
|
(username, password, cb) => {
|
||||||
query = `SELECT user_id, email, password, admin
|
query = `SELECT user_id, email, password, admin
|
||||||
FROM accounts.users
|
FROM accounts.users
|
||||||
WHERE email = $1`;
|
WHERE email = $1`;
|
||||||
database.executeQuery(query, [email])
|
database.executeQuery(query, [username])
|
||||||
.then(result => {
|
.then(result => {
|
||||||
if(result.length > 0) {
|
if(result.length > 0) {
|
||||||
const first = result[0];
|
const first = result[0];
|
||||||
const matches = bcrypt.compareSync(password, first[2]);
|
const matches = bcrypt.compareSync(password, first[2]);
|
||||||
if(matches) {
|
if(matches) {
|
||||||
cb(null, { id: first[0], email: first[1], admin: first[3] })
|
return cb(null, { id: first[0], email: first[1], admin: first[3] })
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
cb(null, false)
|
return cb(null, false)
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
cb(null, false)
|
return cb(null, false)
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}));
|
}));
|
||||||
|
@ -32,7 +36,7 @@ passport.serializeUser((user, done) => {
|
||||||
passport.deserializeUser((id, cb) => {
|
passport.deserializeUser((id, cb) => {
|
||||||
query = `SELECT user_id, email, admin
|
query = `SELECT user_id, email, admin
|
||||||
FROM accounts.users
|
FROM accounts.users
|
||||||
WHERE id = $1`;
|
WHERE user_id = $1`;
|
||||||
database.executeQuery(query, [parseInt(id, 10)])
|
database.executeQuery(query, [parseInt(id, 10)])
|
||||||
.then(result => {
|
.then(result => {
|
||||||
cb(null, result[0]);
|
cb(null, result[0]);
|
||||||
|
@ -51,3 +55,4 @@ async function createUser(email, password) {
|
||||||
}
|
}
|
||||||
|
|
||||||
exports.createUser = createUser;
|
exports.createUser = createUser;
|
||||||
|
exports.passport = passport;
|
|
@ -1,13 +1,19 @@
|
||||||
var express = require('express');
|
var express = require('express');
|
||||||
var router = express.Router();
|
var router = express.Router();
|
||||||
const passport = require('passport');
|
|
||||||
const accounts = require('./../database/accounts/accounts');
|
const accounts = require('./../database/accounts/accounts');
|
||||||
const app = require('../app');
|
const app = require('../app');
|
||||||
|
|
||||||
router.post('/login', passport.authenticate('local'), (req, res, next) => {
|
router.get('/login', (req, res, next) => {
|
||||||
const { user } = req;
|
res.render('accounts/login', { title : "Login" });
|
||||||
|
});
|
||||||
|
|
||||||
res.json(user);
|
router.post('/login',
|
||||||
|
accounts.passport.authenticate('local', {
|
||||||
|
failureRedirect: '/fail',
|
||||||
|
successRedirect: '/success',
|
||||||
|
}),
|
||||||
|
(req, res, next) => {
|
||||||
|
console.log(req.user);
|
||||||
});
|
});
|
||||||
|
|
||||||
router.post('/register', (req, res, next) => {
|
router.post('/register', (req, res, next) => {
|
||||||
|
|
20
views/accounts/login.pug
Normal file
20
views/accounts/login.pug
Normal file
|
@ -0,0 +1,20 @@
|
||||||
|
extends ../layout
|
||||||
|
|
||||||
|
block stylesheets
|
||||||
|
link(rel='stylesheet', href='/stylesheets/submit.css')
|
||||||
|
link(rel='stylesheet', href='/stylesheets/form.css')
|
||||||
|
|
||||||
|
block content
|
||||||
|
div#mobile-view
|
||||||
|
h1 #{title}
|
||||||
|
form(action='/auth/login', method='POST')
|
||||||
|
span(class='form-section')
|
||||||
|
label Email
|
||||||
|
span(class='form-section-input')
|
||||||
|
input(type="email", name="email")
|
||||||
|
span(class='form-section')
|
||||||
|
label Password
|
||||||
|
span(class='form-section-input')
|
||||||
|
input(type="password", name="password")
|
||||||
|
span(class='form-section')
|
||||||
|
button#submit-button(type="submit") Submit
|
Reference in a new issue