diff --git a/database/accounts/accounts.js b/database/accounts/accounts.js index 5ae6cc9..6f000c9 100644 --- a/database/accounts/accounts.js +++ b/database/accounts/accounts.js @@ -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; \ No newline at end of file +exports.createUser = createUser; +exports.passport = passport; \ No newline at end of file diff --git a/routes/auth.js b/routes/auth.js index 38426ea..026728c 100644 --- a/routes/auth.js +++ b/routes/auth.js @@ -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) => { diff --git a/views/accounts/login.pug b/views/accounts/login.pug new file mode 100644 index 0000000..fb58202 --- /dev/null +++ b/views/accounts/login.pug @@ -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 \ No newline at end of file