diff --git a/app.js b/app.js index c773280..0ad96f4 100644 --- a/app.js +++ b/app.js @@ -3,6 +3,11 @@ var express = require('express'); var path = require('path'); var cookieParser = require('cookie-parser'); var logger = require('morgan'); +var random = require('./database/accounts/random'); +const passport = require('passport'); +const session = require('express-session'); +const accounts = require('./database/accounts/accounts'); + var indexRouter = require('./routes/index'); var usersRouter = require('./routes/users'); @@ -11,6 +16,23 @@ var manageRouter = require('./routes/manage'); var app = express(); +// session setup +app.use( + session({ + secret: random.makeid(20), + resave: false, + saveUninitialized: true, + }) +); + +// passport setup +app.use(passport.initialize()); +app.use(passport.session()); + +//passport.use(accounts.createStrategy()); +//passport.serializeUser(accounts.serializeUser()); +//passport.deserializeUser(accounts.deserializeUser()); + // view engine setup app.set('views', path.join(__dirname, 'views')); app.set('view engine', 'pug'); diff --git a/database/accounts/accounts.js b/database/accounts/accounts.js new file mode 100644 index 0000000..67d277a --- /dev/null +++ b/database/accounts/accounts.js @@ -0,0 +1,37 @@ +const database = require('./../database'); +const passport = require('passport'); +const passportLocal = require('passport-local'); + +passport.use(new passportLocal.Strategy((email, password, cb) => { + query = `SELECT id, email, password, admin + FROM accounts.users + WHERE email = $1`; + const result = database.executeQuery(query, [email]); + if(result.length > 0) { + const first = result[0]; + bcrypt.compare(password, first[2], function(err, res) { + if(res) { + cb(null, { id: first[0], email: first[1], admin: first[3] }) + } + else + { + cb(null, false) + } + }) + } else { + cb(null, false) + } +})); + +passport.serializeUser((user, done) => { + done(null, user.id) +}) + +passport.deserializeUser((id, cb) => { + query = `SELECT id, email, admin + FROM accounts.users + WHERE id = $1`; + const result = database.executeQuery(query, [parseInt(id, 10)]); + + cb(null, result[0]); +}); \ No newline at end of file diff --git a/database/accounts/random.js b/database/accounts/random.js new file mode 100644 index 0000000..6d6a57d --- /dev/null +++ b/database/accounts/random.js @@ -0,0 +1,12 @@ +function makeid(length) { + var result = ''; + var characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'; + var charactersLength = characters.length; + for ( var i = 0; i < length; i++ ) { + result += characters.charAt(Math.floor(Math.random() * + charactersLength)); + } + return result; +} + +exports.makeid = makeid; \ No newline at end of file diff --git a/routes/accounts.js b/routes/accounts.js new file mode 100644 index 0000000..5a72c2f --- /dev/null +++ b/routes/accounts.js @@ -0,0 +1,11 @@ +const passport = require('passport'); +var router = express.Router(); +const app = require('../app'); + +router.post('/login', passport.authenticate('local'), (req, res, next) => { + const { user } = req; + + res.json(user); +}); + +module.exports = router; \ No newline at end of file