From 85630ec2332ceb5728c1ae664f3bff5a58267f49 Mon Sep 17 00:00:00 2001 From: Sebastien Clement Date: Thu, 28 Jan 2021 01:30:55 +0100 Subject: [PATCH] :hammer: Add Passport --- app.js | 27 +++- config/passport.js | 46 ++++++ db.sqlite | Bin 24576 -> 28672 bytes package-lock.json | 271 ++++++++++++++++++++++++++++++--- package.json | 6 + public/css/style.scss | 2 +- routes/index.js | 10 +- routes/login.js | 19 +++ sequelize/index.js | 3 +- sequelize/init-db.js | 3 + sequelize/models/user.model.js | 21 +++ views/login.ejs | 37 +++++ 12 files changed, 414 insertions(+), 31 deletions(-) create mode 100644 config/passport.js create mode 100644 routes/login.js create mode 100644 sequelize/models/user.model.js create mode 100644 views/login.ejs diff --git a/app.js b/app.js index b48e78e..8e03dd5 100644 --- a/app.js +++ b/app.js @@ -2,12 +2,16 @@ const createError = require('http-errors'); const express = require('express'); const path = require('path'); const cookieParser = require('cookie-parser'); +const bodyParser = require('body-parser') const logger = require('./config/winston'); const sassMiddleware = require('node-sass-middleware'); const expressWinston = require('express-winston'); const i18n = require('i18n'); const indexRouter = require('./routes/index'); +const loginRouter = require('./routes/login'); + +const passport = require('./config/passport'); const app = express(); @@ -39,17 +43,28 @@ app.use(expressWinston.logger({ app.use(express.json()); app.use(express.urlencoded({ extended: false })); +app.use(require('express-session')({ secret: 'keyboard cat', resave: false, saveUninitialized: false })); app.use(cookieParser()); -app.use(sassMiddleware({ - src: path.join(__dirname, 'public'), - dest: path.join(__dirname, 'public'), - indentedSyntax: true, // true = .sass and false = .scss - sourceMap: true -})); +app.use(bodyParser.urlencoded({ extended: true })); +// app.use(sassMiddleware({ +// src: path.join(__dirname, 'public'), +// dest: path.join(__dirname, 'public'), +// indentedSyntax: false, // true = .sass and false = .scss +// sourceMap: true, +// debug: false, +// response: false, +// log: function (severity, key, value) { logger.log(severity, `node-sass-middleware ${key} : ${value}`); } +// +// })); app.use(express.static(path.join(__dirname, 'public'))); app.use(i18n.init) +app.use(passport.initialize()); +app.use(passport.session()); + + app.use('/', indexRouter); +app.use('/', loginRouter); // Boootstrap JS Files app.use('/js/bootstrap.min.js', express.static(path.join(__dirname, '/node_modules/bootstrap/dist/js/bootstrap.min.js'))) diff --git a/config/passport.js b/config/passport.js new file mode 100644 index 0000000..2c47e86 --- /dev/null +++ b/config/passport.js @@ -0,0 +1,46 @@ +const passport = require('passport'); +const LocalStrategy = require('passport-local').Strategy; +const sequelize = require('../sequelize'); +const bcrypt = require('bcrypt'); +const logger = require('./winston') + + +passport.use(new LocalStrategy(function (username, password, done) { + sequelize.models.user.findOne({ where: { username: username } }) + .then(function (user) { + if (!user) { + return done(null, false, { + message: 'Invalid username or password' + }); + } + if (bcrypt.compareSync(password, user.password)) + return done(null, user); + else + return done(null, false, { + message: 'Invalid username or password' + }); + }) + .catch(function (err) { + logger.error("Error:", err); + return done(err, false, { + message: 'Something went wrong with your Signin' + }); + }) +})) + +passport.serializeUser(function(user, done) { + done(null, user.id); +}); + +passport.deserializeUser(function(id, done) { + sequelize.models.user.findOne({ where: { id: id } }) + .then(function (user) { + return done(null, user); + }) + .catch(function (err) { + logger.error("Error:", err); + return done(err, null); + }) +}); + +module.exports = passport; \ No newline at end of file diff --git a/db.sqlite b/db.sqlite index 2c5d1b581af89fb5e2d4568f0470f8558fb75daa..43b60773eb175a14fdf38bcba288bed6aac9f72d 100644 GIT binary patch delta 847 zcmZoTz}WDBae}nqV+IBWb|8iV#@iEhj3pm4=s6$e<>zN$<1%94+skvB+m_#s%ZOWO zv!VbK`{pY49ZcGdvFz;P;^K_Wk|l{rIjN<^sYS(Lg3UR|)iK0XA;i(i$5kN#C^mT^ zw;l^fZt?_9X(5GBKhMBWR~?0fg2dwD^8BKd$?LdGC*S8+)Yjx+6Soy-WXQ}*Nv$X@ z%*iZCjZZ8s$p`b}!KTL>f+_aRw%k9NIoR0Qzz*MR$G?Y{+sN3;$k58rOwYt}@<%;k z9wffuWEI&w{0jESX5d%gCl`-j!F9P*{0h?KGw~|e{8RodpBCKNrpD$9+6D$z1_nk3 zMuxfuhPp=P3dpjP1@xmxQ9ecA+7Vele$%0{yuc7<;orf)|DOLM|8f2un*|M4@JsNr zFe`HArKYCDXXa&=6mu{$i*tfvX7dI6|NPc)OUz76@mPY81zEz#U&+8<2{b;RzrK-$ zk)1)jk=4^PF(o%MPsJ!n#n3<{w8ALTJgwBJ*rKE~%Oa>WGTk82qSV{8+$}sc(Ivwr z+0|D+qS7!kx3WCDz|ps?(%;ZO99!6#8JXd8mXQKXnhWf%?+pCk89*tG6^NOEm}#?O OzGPcw+FfdloHZZU03IZgRR910 diff --git a/package-lock.json b/package-lock.json index eb27104..c5f2f29 100644 --- a/package-lock.json +++ b/package-lock.json @@ -139,6 +139,62 @@ "safe-buffer": "5.1.2" } }, + "bcrypt": { + "version": "5.0.0", + "resolved": "https://registry.npmjs.org/bcrypt/-/bcrypt-5.0.0.tgz", + "integrity": "sha512-jB0yCBl4W/kVHM2whjfyqnxTmOHkCX4kHEa5nYKSoGeYe8YrjTYTc87/6bwt1g8cmV0QrbhKriETg9jWtcREhg==", + "requires": { + "node-addon-api": "^3.0.0", + "node-pre-gyp": "0.15.0" + }, + "dependencies": { + "node-pre-gyp": { + "version": "0.15.0", + "resolved": "https://registry.npmjs.org/node-pre-gyp/-/node-pre-gyp-0.15.0.tgz", + "integrity": "sha512-7QcZa8/fpaU/BKenjcaeFF9hLz2+7S9AqyXFhlH/rilsQ/hPZKK32RtR5EQHJElgu+q5RfbJ34KriI79UWaorA==", + "requires": { + "detect-libc": "^1.0.2", + "mkdirp": "^0.5.3", + "needle": "^2.5.0", + "nopt": "^4.0.1", + "npm-packlist": "^1.1.6", + "npmlog": "^4.0.2", + "rc": "^1.2.7", + "rimraf": "^2.6.1", + "semver": "^5.3.0", + "tar": "^4.4.2" + } + }, + "nopt": { + "version": "4.0.3", + "resolved": "https://registry.npmjs.org/nopt/-/nopt-4.0.3.tgz", + "integrity": "sha512-CvaGwVMztSMJLOeXPrez7fyfObdZqNUK1cPAEzLHrTybIua9pMdmmPR5YwtfNftIOMv3DPUhFaxsZMNTQO20Kg==", + "requires": { + "abbrev": "1", + "osenv": "^0.1.4" + } + }, + "tar": { + "version": "4.4.13", + "resolved": "https://registry.npmjs.org/tar/-/tar-4.4.13.tgz", + "integrity": "sha512-w2VwSrBoHa5BsSyH+KxEqeQBAllHhccyMFVHtGtdMpF4W7IRWfZjFiQceJPChOeTsSDVUpER2T8FA93pr0L+QA==", + "requires": { + "chownr": "^1.1.1", + "fs-minipass": "^1.2.5", + "minipass": "^2.8.6", + "minizlib": "^1.2.1", + "mkdirp": "^0.5.0", + "safe-buffer": "^5.1.2", + "yallist": "^3.0.3" + } + }, + "yallist": { + "version": "3.1.1", + "resolved": "https://registry.npmjs.org/yallist/-/yallist-3.1.1.tgz", + "integrity": "sha512-a4UGQaWPH59mOXUYnAG2ewncQS4i4F43Tv3JoAM+s2VDAmS9NsK8GpDMLrCHPksFT7h3K6TOoUNn2pb7RoXx4g==" + } + } + }, "bcrypt-pbkdf": { "version": "1.0.2", "resolved": "https://registry.npmjs.org/bcrypt-pbkdf/-/bcrypt-pbkdf-1.0.2.tgz", @@ -156,20 +212,57 @@ } }, "body-parser": { - "version": "1.18.3", - "resolved": "https://registry.npmjs.org/body-parser/-/body-parser-1.18.3.tgz", - "integrity": "sha1-WykhmP/dVTs6DyDe0FkrlWlVyLQ=", + "version": "1.19.0", + "resolved": "https://registry.npmjs.org/body-parser/-/body-parser-1.19.0.tgz", + "integrity": "sha512-dhEPs72UPbDnAQJ9ZKMNTP6ptJaionhP5cBb541nXPlW60Jepo9RV/a4fX4XWW9CuFNK22krhrj1+rgzifNCsw==", "requires": { - "bytes": "3.0.0", + "bytes": "3.1.0", "content-type": "~1.0.4", "debug": "2.6.9", "depd": "~1.1.2", - "http-errors": "~1.6.3", - "iconv-lite": "0.4.23", + "http-errors": "1.7.2", + "iconv-lite": "0.4.24", "on-finished": "~2.3.0", - "qs": "6.5.2", - "raw-body": "2.3.3", - "type-is": "~1.6.16" + "qs": "6.7.0", + "raw-body": "2.4.0", + "type-is": "~1.6.17" + }, + "dependencies": { + "http-errors": { + "version": "1.7.2", + "resolved": "https://registry.npmjs.org/http-errors/-/http-errors-1.7.2.tgz", + "integrity": "sha512-uUQBt3H/cSIVfch6i1EuPNy/YsRSOUBXTVfZ+yR7Zjez3qjBz6i9+i4zjNaoqcoFVI4lQJ5plg63TvGfRSDCRg==", + "requires": { + "depd": "~1.1.2", + "inherits": "2.0.3", + "setprototypeof": "1.1.1", + "statuses": ">= 1.5.0 < 2", + "toidentifier": "1.0.0" + } + }, + "iconv-lite": { + "version": "0.4.24", + "resolved": "https://registry.npmjs.org/iconv-lite/-/iconv-lite-0.4.24.tgz", + "integrity": "sha512-v3MXnZAcvnywkTUEZomIActle7RXXeedOR31wwl7VlyoXO4Qi9arvSenNQWne1TcRwhCL1HwLI21bEqdpj8/rA==", + "requires": { + "safer-buffer": ">= 2.1.2 < 3" + } + }, + "qs": { + "version": "6.7.0", + "resolved": "https://registry.npmjs.org/qs/-/qs-6.7.0.tgz", + "integrity": "sha512-VCdBRNFTX1fyE7Nb6FYoURo/SPe62QCaAyzJvUjwRaIsc+NePBEniHlvxFmmX56+HZphIGtV0XeCirBtpDrTyQ==" + }, + "setprototypeof": { + "version": "1.1.1", + "resolved": "https://registry.npmjs.org/setprototypeof/-/setprototypeof-1.1.1.tgz", + "integrity": "sha512-JvdAWfbXeIGaZ9cILp38HntZSFSo3mWg6xGcJJsd+d4aRMOqauag1C63dJfDw7OaMYwEbHMOxEZ1lqVRYP2OAw==" + }, + "statuses": { + "version": "1.5.0", + "resolved": "https://registry.npmjs.org/statuses/-/statuses-1.5.0.tgz", + "integrity": "sha1-Fhx9rBd2Wf2YEfQ3cfqZOBR4Yow=" + } } }, "bootstrap": { @@ -187,9 +280,9 @@ } }, "bytes": { - "version": "3.0.0", - "resolved": "https://registry.npmjs.org/bytes/-/bytes-3.0.0.tgz", - "integrity": "sha1-0ygVQE1olpn4Wk6k+odV3ROpYEg=" + "version": "3.1.0", + "resolved": "https://registry.npmjs.org/bytes/-/bytes-3.1.0.tgz", + "integrity": "sha512-zauLjrfCG+xvoyaqLoV8bLVXXNGC4JqlxFCutSDWA6fJrTo2ZuvLYTqZ7aHBLZSMOopbzwv8f+wZcVzfVTI2Dg==" }, "camelcase": { "version": "2.1.1", @@ -330,6 +423,11 @@ "resolved": "https://registry.npmjs.org/concat-map/-/concat-map-0.0.1.tgz", "integrity": "sha1-2Klr13/Wjfd5OnMDajug1UBdR3s=" }, + "connect-ensure-login": { + "version": "0.1.1", + "resolved": "https://registry.npmjs.org/connect-ensure-login/-/connect-ensure-login-0.1.1.tgz", + "integrity": "sha1-F03MUSQ7nqwj+NmCFa62aU4uihI=" + }, "console-control-strings": { "version": "1.1.0", "resolved": "https://registry.npmjs.org/console-control-strings/-/console-control-strings-1.1.0.tgz", @@ -536,10 +634,70 @@ "vary": "~1.1.2" }, "dependencies": { + "body-parser": { + "version": "1.18.3", + "resolved": "https://registry.npmjs.org/body-parser/-/body-parser-1.18.3.tgz", + "integrity": "sha1-WykhmP/dVTs6DyDe0FkrlWlVyLQ=", + "requires": { + "bytes": "3.0.0", + "content-type": "~1.0.4", + "debug": "2.6.9", + "depd": "~1.1.2", + "http-errors": "~1.6.3", + "iconv-lite": "0.4.23", + "on-finished": "~2.3.0", + "qs": "6.5.2", + "raw-body": "2.3.3", + "type-is": "~1.6.16" + } + }, + "bytes": { + "version": "3.0.0", + "resolved": "https://registry.npmjs.org/bytes/-/bytes-3.0.0.tgz", + "integrity": "sha1-0ygVQE1olpn4Wk6k+odV3ROpYEg=" + }, "cookie": { "version": "0.3.1", "resolved": "https://registry.npmjs.org/cookie/-/cookie-0.3.1.tgz", "integrity": "sha1-5+Ch+e9DtMi6klxcWpboBtFoc7s=" + }, + "raw-body": { + "version": "2.3.3", + "resolved": "https://registry.npmjs.org/raw-body/-/raw-body-2.3.3.tgz", + "integrity": "sha512-9esiElv1BrZoI3rCDuOuKCBRbuApGGaDPQfjSflGxdy4oyzqghxu6klEkkVIvBje+FF0BX9coEv8KqW6X/7njw==", + "requires": { + "bytes": "3.0.0", + "http-errors": "1.6.3", + "iconv-lite": "0.4.23", + "unpipe": "1.0.0" + } + } + } + }, + "express-session": { + "version": "1.17.1", + "resolved": "https://registry.npmjs.org/express-session/-/express-session-1.17.1.tgz", + "integrity": "sha512-UbHwgqjxQZJiWRTMyhvWGvjBQduGCSBDhhZXYenziMFjxst5rMV+aJZ6hKPHZnPyHGsrqRICxtX8jtEbm/z36Q==", + "requires": { + "cookie": "0.4.0", + "cookie-signature": "1.0.6", + "debug": "2.6.9", + "depd": "~2.0.0", + "on-headers": "~1.0.2", + "parseurl": "~1.3.3", + "safe-buffer": "5.2.0", + "uid-safe": "~2.1.5" + }, + "dependencies": { + "depd": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/depd/-/depd-2.0.0.tgz", + "integrity": "sha512-g7nH6P6dyDioJogAAGprGpCtVImJhpPk/roCzdb3fIh61/s/nPsfR6onyMwkCAR/OlC3yBC0lESvUoQEAssIrw==" + }, + "safe-buffer": { + "version": "5.2.0", + "resolved": "https://registry.npmjs.org/safe-buffer/-/safe-buffer-5.2.0.tgz", + "integrity": "sha512-fZEwUGbVl7kouZs1jCdMLdt95hdIv0ZeHg6L7qPeciMZhZ+/gdesW4wgTARkrFWEpspjEATAzUGPG8N2jJiwbg==" } } }, @@ -1551,6 +1709,28 @@ "resolved": "https://registry.npmjs.org/parseurl/-/parseurl-1.3.3.tgz", "integrity": "sha512-CiyeOxFT/JZyN5m0z9PfXw4SCBJ6Sygz1Dpl0wqjlhDEGGBP1GnsUVEL0p63hoG1fcj3fHynXi9NYO4nWOL+qQ==" }, + "passport": { + "version": "0.4.1", + "resolved": "https://registry.npmjs.org/passport/-/passport-0.4.1.tgz", + "integrity": "sha512-IxXgZZs8d7uFSt3eqNjM9NQ3g3uQCW5avD8mRNoXV99Yig50vjuaez6dQK2qC0kVWPRTujxY0dWgGfT09adjYg==", + "requires": { + "passport-strategy": "1.x.x", + "pause": "0.0.1" + } + }, + "passport-local": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/passport-local/-/passport-local-1.0.0.tgz", + "integrity": "sha1-H+YyaMkudWBmJkN+O5BmYsFbpu4=", + "requires": { + "passport-strategy": "1.x.x" + } + }, + "passport-strategy": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/passport-strategy/-/passport-strategy-1.0.0.tgz", + "integrity": "sha1-tVOaqPwiWj0a0XlHbd8ja0QPUuQ=" + }, "path-exists": { "version": "2.1.0", "resolved": "https://registry.npmjs.org/path-exists/-/path-exists-2.1.0.tgz", @@ -1584,6 +1764,11 @@ "pinkie-promise": "^2.0.0" } }, + "pause": { + "version": "0.0.1", + "resolved": "https://registry.npmjs.org/pause/-/pause-0.0.1.tgz", + "integrity": "sha1-HUCLP9t2kjuVQ9lvtMnf1TXZy10=" + }, "performance-now": { "version": "2.1.0", "resolved": "https://registry.npmjs.org/performance-now/-/performance-now-2.1.0.tgz", @@ -1641,20 +1826,57 @@ "resolved": "https://registry.npmjs.org/qs/-/qs-6.5.2.tgz", "integrity": "sha512-N5ZAX4/LxJmF+7wN74pUD6qAh9/wnvdQcjq9TZjevvXzSUo7bfmw91saqMjzGS2xq91/odN2dW/WOl7qQHNDGA==" }, + "random-bytes": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/random-bytes/-/random-bytes-1.0.0.tgz", + "integrity": "sha1-T2ih3Arli9P7lYSMMDJNt11kNgs=" + }, "range-parser": { "version": "1.2.1", "resolved": "https://registry.npmjs.org/range-parser/-/range-parser-1.2.1.tgz", "integrity": "sha512-Hrgsx+orqoygnmhFbKaHE6c296J+HTAQXoxEF6gNupROmmGJRoyzfG3ccAveqCBrwr/2yxQ5BVd/GTl5agOwSg==" }, "raw-body": { - "version": "2.3.3", - "resolved": "https://registry.npmjs.org/raw-body/-/raw-body-2.3.3.tgz", - "integrity": "sha512-9esiElv1BrZoI3rCDuOuKCBRbuApGGaDPQfjSflGxdy4oyzqghxu6klEkkVIvBje+FF0BX9coEv8KqW6X/7njw==", + "version": "2.4.0", + "resolved": "https://registry.npmjs.org/raw-body/-/raw-body-2.4.0.tgz", + "integrity": "sha512-4Oz8DUIwdvoa5qMJelxipzi/iJIi40O5cGV1wNYp5hvZP8ZN0T+jiNkL0QepXs+EsQ9XJ8ipEDoiH70ySUJP3Q==", "requires": { - "bytes": "3.0.0", - "http-errors": "1.6.3", - "iconv-lite": "0.4.23", + "bytes": "3.1.0", + "http-errors": "1.7.2", + "iconv-lite": "0.4.24", "unpipe": "1.0.0" + }, + "dependencies": { + "http-errors": { + "version": "1.7.2", + "resolved": "https://registry.npmjs.org/http-errors/-/http-errors-1.7.2.tgz", + "integrity": "sha512-uUQBt3H/cSIVfch6i1EuPNy/YsRSOUBXTVfZ+yR7Zjez3qjBz6i9+i4zjNaoqcoFVI4lQJ5plg63TvGfRSDCRg==", + "requires": { + "depd": "~1.1.2", + "inherits": "2.0.3", + "setprototypeof": "1.1.1", + "statuses": ">= 1.5.0 < 2", + "toidentifier": "1.0.0" + } + }, + "iconv-lite": { + "version": "0.4.24", + "resolved": "https://registry.npmjs.org/iconv-lite/-/iconv-lite-0.4.24.tgz", + "integrity": "sha512-v3MXnZAcvnywkTUEZomIActle7RXXeedOR31wwl7VlyoXO4Qi9arvSenNQWne1TcRwhCL1HwLI21bEqdpj8/rA==", + "requires": { + "safer-buffer": ">= 2.1.2 < 3" + } + }, + "setprototypeof": { + "version": "1.1.1", + "resolved": "https://registry.npmjs.org/setprototypeof/-/setprototypeof-1.1.1.tgz", + "integrity": "sha512-JvdAWfbXeIGaZ9cILp38HntZSFSo3mWg6xGcJJsd+d4aRMOqauag1C63dJfDw7OaMYwEbHMOxEZ1lqVRYP2OAw==" + }, + "statuses": { + "version": "1.5.0", + "resolved": "https://registry.npmjs.org/statuses/-/statuses-1.5.0.tgz", + "integrity": "sha1-Fhx9rBd2Wf2YEfQ3cfqZOBR4Yow=" + } } }, "rc": { @@ -2099,6 +2321,11 @@ "resolved": "https://registry.npmjs.org/text-hex/-/text-hex-1.0.0.tgz", "integrity": "sha512-uuVGNWzgJ4yhRaNSiubPY7OjISw4sw4E5Uv0wbjp+OzcbmVU/rsT8ujgcXJhn9ypzsgr5vlzpPqP+MBBKcGvbg==" }, + "toidentifier": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/toidentifier/-/toidentifier-1.0.0.tgz", + "integrity": "sha512-yaOH/Pk/VEhBWWTlhI+qXxDFXlejDGcQipMlyxda9nthulaxLZUNcUqFxokp0vcYnvteJln5FNQDRrxj3YcbVw==" + }, "toposort-class": { "version": "1.0.1", "resolved": "https://registry.npmjs.org/toposort-class/-/toposort-class-1.0.1.tgz", @@ -2153,6 +2380,14 @@ "mime-types": "~2.1.24" } }, + "uid-safe": { + "version": "2.1.5", + "resolved": "https://registry.npmjs.org/uid-safe/-/uid-safe-2.1.5.tgz", + "integrity": "sha512-KPHm4VL5dDXKz01UuEd88Df+KzynaohSL9fBh096KWAxSKZQDI2uBrVqtvRM4rwrIrRRKsdLNML/lnaaVSRioA==", + "requires": { + "random-bytes": "~1.0.0" + } + }, "unpipe": { "version": "1.0.0", "resolved": "https://registry.npmjs.org/unpipe/-/unpipe-1.0.0.tgz", diff --git a/package.json b/package.json index dc733fe..440f8de 100644 --- a/package.json +++ b/package.json @@ -6,16 +6,22 @@ "start": "node ./bin/www" }, "dependencies": { + "bcrypt": "^5.0.0", + "body-parser": "^1.19.0", "bootstrap": "^5.0.0-beta1", + "connect-ensure-login": "^0.1.1", "cookie-parser": "~1.4.4", "debug": "~2.6.9", "ejs": "~2.6.1", "express": "~4.16.1", + "express-session": "^1.17.1", "express-winston": "^4.0.5", "http-errors": "~1.6.3", "i18n": "^0.13.2", "morgan": "~1.9.1", "node-sass-middleware": "0.11.0", + "passport": "^0.4.1", + "passport-local": "^1.0.0", "sequelize": "^6.5.0", "sqlite3": "^5.0.1", "winston": "^3.3.3" diff --git a/public/css/style.scss b/public/css/style.scss index 4ea26bb..393bec7 100644 --- a/public/css/style.scss +++ b/public/css/style.scss @@ -1,3 +1,3 @@ .navbar { background-color: #0091ea; -} \ No newline at end of file +} diff --git a/routes/index.js b/routes/index.js index b1d04d0..db772a2 100644 --- a/routes/index.js +++ b/routes/index.js @@ -3,12 +3,12 @@ var router = express.Router(); const sequelize = require('../sequelize') /* GET home page. */ -router.get('/', function(req, res, next) { - sequelize.models.preset_cut.findAll({order: ['name']}).then((preset_cut)=>{ - sequelize.models.preset_step_down_factor.findAll({order: ['name']}).then((step_down_factor)=>{ - res.render('index', { preset_cut: preset_cut, step_down_factor: step_down_factor }); +router.get('/', function (req, res, next) { + sequelize.models.preset_cut.findAll({ order: ['name'] }).then((preset_cut) => { + sequelize.models.preset_step_down_factor.findAll({ order: ['name'] }).then((step_down_factor) => { + res.render('index', { preset_cut: preset_cut, step_down_factor: step_down_factor }); + }) }) - }) }); diff --git a/routes/login.js b/routes/login.js new file mode 100644 index 0000000..e2422d6 --- /dev/null +++ b/routes/login.js @@ -0,0 +1,19 @@ +var express = require('express'); +var router = express.Router(); +const passport = require('../config/passport') + +/* GET home page. */ +router.get('/login', function (req, res, next) { + res.render('login'); +}); +router.post('/login', passport.authenticate('local', { failureRedirect: '/login' }), + function (req, res) { + res.redirect('/'); + }); + +router.get('/logout', + function (req, res) { + req.logout(); + res.redirect('/'); + }); +module.exports = router; diff --git a/sequelize/index.js b/sequelize/index.js index 02ba18a..78c79a9 100644 --- a/sequelize/index.js +++ b/sequelize/index.js @@ -11,7 +11,8 @@ const sequelize = new Sequelize({ const modelDefiners = [ require('./models/preset-cut.model'), require('./models/preset-step-down-factor.model'), - require('./models/need-init.model') + require('./models/need-init.model'), + require('./models/user.model') ]; for (const modelDefiner of modelDefiners) { diff --git a/sequelize/init-db.js b/sequelize/init-db.js index 27eb153..d24634d 100644 --- a/sequelize/init-db.js +++ b/sequelize/init-db.js @@ -1,5 +1,6 @@ const logger = require('../config/winston') const sequelize = require('./index') +const bcrypt = require('bcrypt') async function reset() { logger.warn('Reset database...'); @@ -103,7 +104,9 @@ async function reset() { k_more_6: 0.35 }, ]) + await sequelize.models.user.create({username: "admin" , password: bcrypt.hashSync("love_cnc", 10)}) await sequelize.models.need_init.create({name: 'init'}) + logger.info('...Done') } diff --git a/sequelize/models/user.model.js b/sequelize/models/user.model.js new file mode 100644 index 0000000..9022110 --- /dev/null +++ b/sequelize/models/user.model.js @@ -0,0 +1,21 @@ +const { DataTypes } = require('sequelize'); + +module.exports = (sequelize) => { + sequelize.define('user', { + id: { + allowNull: false, + autoIncrement: true, + primaryKey: true, + type: DataTypes.INTEGER + }, + username: { + allowNull: false, + type: DataTypes.STRING, + unique: true + }, + password: { + allowNull: false, + type: DataTypes.STRING, + } + }) +} \ No newline at end of file diff --git a/views/login.ejs b/views/login.ejs new file mode 100644 index 0000000..72f76bf --- /dev/null +++ b/views/login.ejs @@ -0,0 +1,37 @@ + + + + CNC Speed Calculator + + + + + + +
+
+
+ + +
+
+ + +
+
+ +
+
+
+ + + + + +