Create login page

main
sudoer777 2021-11-24 20:58:49 -07:00
parent 9c7241e7e6
commit a6fe6d6c72
3 changed files with 43 additions and 12 deletions

View File

@ -1,26 +1,30 @@
const database = require('./../database');
const passport = require('passport');
const passportLocal = require('passport-local');
const localStrategy = require('passport-local').Strategy;
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
FROM accounts.users
WHERE email = $1`;
database.executeQuery(query, [email])
database.executeQuery(query, [username])
.then(result => {
if(result.length > 0) {
const first = result[0];
const matches = bcrypt.compareSync(password, first[2]);
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
{
cb(null, false)
return cb(null, false)
}
} else {
cb(null, false)
return cb(null, false)
}
});
}));
@ -32,7 +36,7 @@ passport.serializeUser((user, done) => {
passport.deserializeUser((id, cb) => {
query = `SELECT user_id, email, admin
FROM accounts.users
WHERE id = $1`;
WHERE user_id = $1`;
database.executeQuery(query, [parseInt(id, 10)])
.then(result => {
cb(null, result[0]);
@ -50,4 +54,5 @@ async function createUser(email, password) {
await database.executeQuery(query, [email, hash]);
}
exports.createUser = createUser;
exports.createUser = createUser;
exports.passport = passport;

View File

@ -1,13 +1,19 @@
var express = require('express');
var router = express.Router();
const passport = require('passport');
const accounts = require('./../database/accounts/accounts');
const app = require('../app');
router.post('/login', passport.authenticate('local'), (req, res, next) => {
const { user } = req;
router.get('/login', (req, res, next) => {
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) => {

View 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