From 1eec9c7895dac0dddac4c714006dbc3cc7cb7f71 Mon Sep 17 00:00:00 2001 From: Sajeda Saju Date: Sun, 2 Apr 2023 07:32:36 +0600 Subject: [PATCH 1/6] login and register page added --- index.js | 44 ++++++++++++++++++++++++++++++ public/registerStyle.css | 52 ++++++++++++++++++++++++++++++++++++ views/pages/index.ejs | 1 + views/pages/login.ejs | 28 +++++++++++++++++++ views/pages/registration.ejs | 32 ++++++++++++++++++++++ 5 files changed, 157 insertions(+) create mode 100644 public/registerStyle.css create mode 100644 views/pages/login.ejs create mode 100644 views/pages/registration.ejs diff --git a/index.js b/index.js index 35260af..0d207cf 100644 --- a/index.js +++ b/index.js @@ -10,11 +10,54 @@ app.use(bodyParser.urlencoded({ extended: true })); app.set('view engine', 'ejs'); const todos = []; +const users=[]; +let error; + +app.get('/login', (req, res) => { + res.render('pages/login'); +}); + +app.post('/login', (req, res) => { + const data = req.body; + console.log('===',data) + + + res.redirect('/'); +}); +app.get('/registration', (req, res) => { + console.log({res,req}) + + res.render('pages/registration',{userName:'dd'}) +}); + +app.post('/registration', async (req, res) => { + const data = req.body; + const user={ + userName: data.userName, + email:data.userName, + password:data.password, + confirmPassword:data.confirmPassword + } + if(user.password===user.confirmPassword){ + console.log('hello') + users.push(user); + res.redirect('/login'); + } + else { + error='Password not matched'; + res.render('pages/registration'); + console.log('===',data) + } + + + +}); app.get('/', (req, res) => { res.render('pages/index', { todos }); }); + app.post('/', (req, res) => { const { todo, deleteIndex } = req.body; @@ -27,6 +70,7 @@ app.post('/', (req, res) => { res.redirect('/'); }); + app.listen(3000, () => { console.log('Server listening on port 3000'); }); diff --git a/public/registerStyle.css b/public/registerStyle.css new file mode 100644 index 0000000..47099a1 --- /dev/null +++ b/public/registerStyle.css @@ -0,0 +1,52 @@ +body{ + display: flex; + justify-content: center; + align-items: center; + min-height: 100vh; + font-family: 'Jost', sans-serif; + background: linear-gradient(to bottom, #ffffff, #fbfbfb, #f6f6fd); +} +.register-form{ + width: 30%; + background-color: rgba(255,255,255,0.13); + border: 2px solid rgba(255,255,255,0.1); + box-shadow: 0 0 30px rgb(8 7 16 / 19%); + padding: 50px 35px; + +} +.heading{ + text-align: center; +} + +input{ + width: 100%; + height: 20px; + background: #e0dede; + margin: 20px auto; + padding: 10px; + border: none; + outline: none; + border-radius: 5px; +} +button{ + width: 60%; + height: 40px; + margin: 10px auto; + justify-content: center; + display: block; + color: #fff; + background: #406253; + font-size: 1em; + font-weight: bold; + margin-top: 20px; + outline: none; + border: none; + border-radius: 5px; + transition: .2s ease-in; + cursor: pointer; +} +button:hover{ + background: #4e7664; +} + + diff --git a/views/pages/index.ejs b/views/pages/index.ejs index d755c6b..2b9b9df 100644 --- a/views/pages/index.ejs +++ b/views/pages/index.ejs @@ -20,6 +20,7 @@ + <% console.log('eww',todos) %> <% for (let i = 0; i < todos.length; i++) { %> <%= i + 1 %> diff --git a/views/pages/login.ejs b/views/pages/login.ejs new file mode 100644 index 0000000..e6daf6e --- /dev/null +++ b/views/pages/login.ejs @@ -0,0 +1,28 @@ + + + + + + + Registration + + + + +
+

LogIn

+ + + + + + + +
+ + + +
+ + \ No newline at end of file diff --git a/views/pages/registration.ejs b/views/pages/registration.ejs new file mode 100644 index 0000000..edb6474 --- /dev/null +++ b/views/pages/registration.ejs @@ -0,0 +1,32 @@ + + + + + + + Registration + + + + +
+

Registration

+ + + + + + + + + + + +
+ + + +
+ + \ No newline at end of file From 4587542e7dd392ce19c730ff11d5bf380a25c7fd Mon Sep 17 00:00:00 2001 From: "sajeda.akhter704@gmail.com" Date: Sun, 2 Apr 2023 14:24:16 +0600 Subject: [PATCH 2/6] session authentication add --- index.js | 107 ++++++++++++++++++----- package.json | 3 + yarn.lock | 238 ++++++++++++++++++++++++++++++++++++++++++++++++++- 3 files changed, 324 insertions(+), 24 deletions(-) diff --git a/index.js b/index.js index 0d207cf..26f1073 100644 --- a/index.js +++ b/index.js @@ -1,40 +1,57 @@ const express = require('express'); const bodyParser = require('body-parser'); +var cookieParser = require('cookie-parser') +var session = require('express-session') const app = express(); app.use(express.static('public')); app.use(bodyParser.urlencoded({ extended: true })); +app.use(cookieParser()) +app.use(session({ + secret: 'your-secret-key', + resave: false, + saveUninitialized: false +})) + app.set('view engine', 'ejs'); const todos = []; -const users=[]; +const users=[{ + userName: "userName", + email:"email", + password:"password", + confirmPassword:"password" +}]; let error; -app.get('/login', (req, res) => { - res.render('pages/login'); -}); - -app.post('/login', (req, res) => { - const data = req.body; - console.log('===',data) +function isLoggedIn(req,res,next) { + console.log('req.session islog',req.session); + console.log('req.session.user',req.session.user); + // if(req.session.user){ + // console.log('req.session.user',req.session.user); + // next() + // } + // else { + // error='user not found' + // next(error); + // } +} - res.redirect('/'); -}); app.get('/registration', (req, res) => { - console.log({res,req}) - res.render('pages/registration',{userName:'dd'}) + res.render('pages/registration') }); app.post('/registration', async (req, res) => { + const data = req.body; const user={ userName: data.userName, - email:data.userName, + email:data.email, password:data.password, confirmPassword:data.confirmPassword } @@ -53,24 +70,72 @@ app.post('/registration', async (req, res) => { }); + app.get('/', (req, res) => { - res.render('pages/index', { todos }); + console.log(req.session); + res.render('pages/index', {todos}); }); +app.use('/',function (err, req, res, next){ + console.log('err',err) + // res.redirect('/login') +}) -app.post('/', (req, res) => { - const { todo, deleteIndex } = req.body; +app.get('/login', (req, res) => { + res.render('pages/login'); +}); - if (deleteIndex !== undefined) { - todos.splice(deleteIndex, 1); - } else if (todo !== '') { - todos.push(todo); +app.post('/login', async (req, res) => { + try{ + const data = req.body; + console.log("req session",req.session) + const existingUser = users.filter((currentUser,index)=>currentUser.email == req.body.email && currentUser.password==req.body.password); + console.log("existingUser",existingUser); + if(existingUser.length > 0 && existingUser[0]){ + console.log("existingUser[0]",existingUser[0]); + req.session.user=existingUser[0]; + // res.redirect('/'); + } + console.log('eee') + + /* users.filter((currentUser,index)=>{ + if(currentUser.email===req.body.email && currentUser.password===req.body.password){ + console.log("currentUser",currentUser); + req.session.user=currentUser + console.log(" req.session.currentUser", req.session.currentUser); + //res.redirect('/'); + } + })*/ + res.redirect('/'); + } + catch (e) { + console.log('e',e); + throw e; } - res.redirect('/'); }); + + + + + +// +// +// app.post('/', (req, res) => { +// const { todo, deleteIndex } = req.body; +// +// if (deleteIndex !== undefined) { +// todos.splice(deleteIndex, 1); +// } else if (todo !== '') { +// todos.push(todo); +// } +// +// res.redirect('/'); +// }); + + app.listen(3000, () => { console.log('Server listening on port 3000'); }); diff --git a/package.json b/package.json index 3f44348..0bc2e10 100644 --- a/package.json +++ b/package.json @@ -7,8 +7,11 @@ "private": true, "dependencies": { "body-parser": "^1.20.2", + "cookie-parser": "^1.4.6", "ejs": "^3.1.9", "express": "^4.18.2", + "express-session": "^1.17.3", + "nodemon": "^2.0.22", "sqlite3": "^5.1.6" } } diff --git a/yarn.lock b/yarn.lock index ac75dbe..468f5ad 100644 --- a/yarn.lock +++ b/yarn.lock @@ -92,6 +92,14 @@ ansi-styles@^4.1.0: dependencies: color-convert "^2.0.1" +anymatch@~3.1.2: + version "3.1.3" + resolved "https://registry.yarnpkg.com/anymatch/-/anymatch-3.1.3.tgz#790c58b19ba1720a84205b57c618d5ad8524973e" + integrity sha512-KMReFUr0B4t+D+OBkjR3KYqvocp2XaSzO55UcB6mgQMd3KbcE+mWTyvVV7D/zsdEbNnV6acZUutkiHQXvTr1Rw== + dependencies: + normalize-path "^3.0.0" + picomatch "^2.0.4" + "aproba@^1.0.3 || ^2.0.0": version "2.0.0" resolved "https://registry.yarnpkg.com/aproba/-/aproba-2.0.0.tgz#52520b8ae5b569215b354efc0caa3fe1e45a8adc" @@ -128,6 +136,11 @@ balanced-match@^1.0.0: resolved "https://registry.yarnpkg.com/balanced-match/-/balanced-match-1.0.2.tgz#e83e3a7e3f300b34cb9d87f615fa0cbf357690ee" integrity sha512-3oSeUO0TMV67hN1AmbXsK4yaqU7tjiHlbxRDZOpH0KW9+CeX4bRAaX0Anxt0tx2MrpRpWwQaPwIlISEJhYU5Pw== +binary-extensions@^2.0.0: + version "2.2.0" + resolved "https://registry.yarnpkg.com/binary-extensions/-/binary-extensions-2.2.0.tgz#75f502eeaf9ffde42fc98829645be4ea76bd9e2d" + integrity sha512-jDctJ/IVQbZoJykoeHbhXpOlNBqGNcwXJKJog42E5HDPUwQTSdjCHdihjj0DlnheQ7blbT6dHOafNAiS8ooQKA== + body-parser@1.20.1: version "1.20.1" resolved "https://registry.yarnpkg.com/body-parser/-/body-parser-1.20.1.tgz#b1812a8912c195cd371a3ee5e66faa2338a5c668" @@ -179,6 +192,13 @@ brace-expansion@^2.0.1: dependencies: balanced-match "^1.0.0" +braces@~3.0.2: + version "3.0.2" + resolved "https://registry.yarnpkg.com/braces/-/braces-3.0.2.tgz#3454e1a462ee8d599e236df336cd9ea4f8afe107" + integrity sha512-b8um+L1RzM3WDSzvhm6gIz1yfTbBt6YTlcEKAvsmqCZZFw46z626lVj9j1yEPW33H5H+lBQpZMP1k8l+78Ha0A== + dependencies: + fill-range "^7.0.1" + bytes@3.1.2: version "3.1.2" resolved "https://registry.yarnpkg.com/bytes/-/bytes-3.1.2.tgz#8b0beeb98605adf1b128fa4386403c009e0221a5" @@ -224,6 +244,21 @@ chalk@^4.0.2: ansi-styles "^4.1.0" supports-color "^7.1.0" +chokidar@^3.5.2: + version "3.5.3" + resolved "https://registry.yarnpkg.com/chokidar/-/chokidar-3.5.3.tgz#1cf37c8707b932bd1af1ae22c0432e2acd1903bd" + integrity sha512-Dr3sfKRP6oTcjf2JmUmFJfeVMvXBdegxB0iVQ5eb2V10uFJUCAS8OByZdVAyVb8xXNz3GjjTgj9kLWsZTqE6kw== + dependencies: + anymatch "~3.1.2" + braces "~3.0.2" + glob-parent "~5.1.2" + is-binary-path "~2.1.0" + is-glob "~4.0.1" + normalize-path "~3.0.0" + readdirp "~3.6.0" + optionalDependencies: + fsevents "~2.3.2" + chownr@^2.0.0: version "2.0.0" resolved "https://registry.yarnpkg.com/chownr/-/chownr-2.0.0.tgz#15bfbe53d2eab4cf70f18a8cd68ebe5b3cb1dece" @@ -273,11 +308,29 @@ content-type@~1.0.4, content-type@~1.0.5: resolved "https://registry.yarnpkg.com/content-type/-/content-type-1.0.5.tgz#8b773162656d1d1086784c8f23a54ce6d73d7918" integrity sha512-nTjqfcBFEipKdXCv4YDQWCfmcLZKm81ldF0pAopTvyrFGVbcR6P/VAAd5G7N+0tTr8QqiU0tFadD6FK4NtJwOA== +cookie-parser@^1.4.6: + version "1.4.6" + resolved "https://registry.yarnpkg.com/cookie-parser/-/cookie-parser-1.4.6.tgz#3ac3a7d35a7a03bbc7e365073a26074824214594" + integrity sha512-z3IzaNjdwUC2olLIB5/ITd0/setiaFMLYiZJle7xg5Fe9KWAceil7xszYfHHBtDFYLSgJduS2Ty0P1uJdPDJeA== + dependencies: + cookie "0.4.1" + cookie-signature "1.0.6" + cookie-signature@1.0.6: version "1.0.6" resolved "https://registry.yarnpkg.com/cookie-signature/-/cookie-signature-1.0.6.tgz#e303a882b342cc3ee8ca513a79999734dab3ae2c" integrity sha512-QADzlaHc8icV8I7vbaJXJwod9HWYp8uCqf1xa4OfNu1T7JVxQIrUgOWtHdNDtPiywmFbiS12VjotIXLrKM3orQ== +cookie@0.4.1: + version "0.4.1" + resolved "https://registry.yarnpkg.com/cookie/-/cookie-0.4.1.tgz#afd713fe26ebd21ba95ceb61f9a8116e50a537d1" + integrity sha512-ZwrFkGJxUR3EIoXtO+yVE69Eb7KlixbaeAWfBQB9vVsNn/o+Yw69gBWSSDK825hQNdN+wF8zELf3dFNl/kxkUA== + +cookie@0.4.2: + version "0.4.2" + resolved "https://registry.yarnpkg.com/cookie/-/cookie-0.4.2.tgz#0e41f24de5ecf317947c82fc789e06a884824432" + integrity sha512-aSWTXFzaKWkvHO1Ny/s+ePFpvKsPnjc551iI41v3ny/ow6tBG5Vd+FuqGNhh1LxOmVzOlGUriIlOaokOvhaStA== + cookie@0.5.0: version "0.5.0" resolved "https://registry.yarnpkg.com/cookie/-/cookie-0.5.0.tgz#d1f5d71adec6558c58f389987c366aa47e994f8b" @@ -297,12 +350,19 @@ debug@4, debug@^4.1.0, debug@^4.3.3: dependencies: ms "2.1.2" +debug@^3.2.7: + version "3.2.7" + resolved "https://registry.yarnpkg.com/debug/-/debug-3.2.7.tgz#72580b7e9145fb39b6676f9c5e5fb100b934179a" + integrity sha512-CFjzYYAi4ThfiQvizrFQevTTXHtnCqWfe7x1AhgEscTz6ZbLbfoLRLPugTQyBth6f8ZERVUSyWHFD/7Wu4t1XQ== + dependencies: + ms "^2.1.1" + delegates@^1.0.0: version "1.0.0" resolved "https://registry.yarnpkg.com/delegates/-/delegates-1.0.0.tgz#84c6e159b81904fdca59a0ef44cd870d31250f9a" integrity sha512-bd2L678uiWATM6m5Z1VzNCErI3jiGzt6HGY8OVICs40JQq/HALfbyNJmp0UDakEY4pMMaN0Ly5om/B1VI/+xfQ== -depd@2.0.0, depd@^2.0.0: +depd@2.0.0, depd@^2.0.0, depd@~2.0.0: version "2.0.0" resolved "https://registry.yarnpkg.com/depd/-/depd-2.0.0.tgz#b696163cc757560d09cf22cc8fad1571b79e76df" integrity sha512-g7nH6P6dyDioJogAAGprGpCtVImJhpPk/roCzdb3fIh61/s/nPsfR6onyMwkCAR/OlC3yBC0lESvUoQEAssIrw== @@ -366,6 +426,20 @@ etag@~1.8.1: resolved "https://registry.yarnpkg.com/etag/-/etag-1.8.1.tgz#41ae2eeb65efa62268aebfea83ac7d79299b0887" integrity sha512-aIL5Fx7mawVa300al2BnEE4iNvo1qETxLrPI/o05L7z6go7fCw1J6EQmbK4FmJ2AS7kgVF/KEZWufBfdClMcPg== +express-session@^1.17.3: + version "1.17.3" + resolved "https://registry.yarnpkg.com/express-session/-/express-session-1.17.3.tgz#14b997a15ed43e5949cb1d073725675dd2777f36" + integrity sha512-4+otWXlShYlG1Ma+2Jnn+xgKUZTMJ5QD3YvfilX3AcocOAbIkVylSWEklzALe/+Pu4qV6TYBj5GwOBFfdKqLBw== + dependencies: + cookie "0.4.2" + 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.1" + uid-safe "~2.1.5" + express@^4.18.2: version "4.18.2" resolved "https://registry.yarnpkg.com/express/-/express-4.18.2.tgz#3fabe08296e930c796c19e3c516979386ba9fd59" @@ -410,6 +484,13 @@ filelist@^1.0.1: dependencies: minimatch "^5.0.1" +fill-range@^7.0.1: + version "7.0.1" + resolved "https://registry.yarnpkg.com/fill-range/-/fill-range-7.0.1.tgz#1919a6a7c75fe38b2c7c77e5198535da9acdda40" + integrity sha512-qOo9F+dMUmC2Lcb4BbVvnKJxTPjCm+RRpe4gDuGrzkL7mEVl/djYSu2OdQ2Pa302N4oqkSg9ir6jaLWJ2USVpQ== + dependencies: + to-regex-range "^5.0.1" + finalhandler@1.2.0: version "1.2.0" resolved "https://registry.yarnpkg.com/finalhandler/-/finalhandler-1.2.0.tgz#7d23fe5731b207b4640e4fcd00aec1f9207a7b32" @@ -445,6 +526,11 @@ fs.realpath@^1.0.0: resolved "https://registry.yarnpkg.com/fs.realpath/-/fs.realpath-1.0.0.tgz#1504ad2523158caa40db4a2787cb01411994ea4f" integrity sha512-OO0pH2lK6a0hZnAdau5ItzHPI6pUlvI7jMVnxUQRtw4owF2wk8lOSabtGDCTP4Ggrg2MbGnWO9X8K1t4+fGMDw== +fsevents@~2.3.2: + version "2.3.2" + resolved "https://registry.yarnpkg.com/fsevents/-/fsevents-2.3.2.tgz#8a526f78b8fdf4623b709e0b975c52c24c02fd1a" + integrity sha512-xiqMQR4xAeHTuB9uWm+fFRcIOgKBMiOBP+eXiyT7jsgVCq1bkVygt00oASowB7EdtpOHaaPgKt812P9ab+DDKA== + function-bind@^1.1.1: version "1.1.1" resolved "https://registry.yarnpkg.com/function-bind/-/function-bind-1.1.1.tgz#a56899d3ea3c9bab874bb9773b7c5ede92f4895d" @@ -488,6 +574,13 @@ get-intrinsic@^1.0.2: has "^1.0.3" has-symbols "^1.0.3" +glob-parent@~5.1.2: + version "5.1.2" + resolved "https://registry.yarnpkg.com/glob-parent/-/glob-parent-5.1.2.tgz#869832c58034fe68a4093c17dc15e8340d8401c4" + integrity sha512-AOIgSQCepiJYwP3ARnGx+5VnTu2HBYdzbGP45eLw1vr3zB3vZLeyed1sC9hnbcOc9/SrMyM5RPQrkGz4aS9Zow== + dependencies: + is-glob "^4.0.1" + glob@^7.1.3, glob@^7.1.4: version "7.2.3" resolved "https://registry.yarnpkg.com/glob/-/glob-7.2.3.tgz#b8df0fb802bbfa8e89bd1d938b4e16578ed44f2b" @@ -505,6 +598,11 @@ graceful-fs@^4.2.6: resolved "https://registry.yarnpkg.com/graceful-fs/-/graceful-fs-4.2.11.tgz#4183e4e8bf08bb6e05bbb2f7d2e0c8f712ca40e3" integrity sha512-RbJ5/jmFcNNCcDV5o9eTnBLJ/HszWV0P73bc+Ff4nS/rJj+YaS6IGyiOL0VoBYX+l1Wrl3k63h/KrH+nhJ0XvQ== +has-flag@^3.0.0: + version "3.0.0" + resolved "https://registry.yarnpkg.com/has-flag/-/has-flag-3.0.0.tgz#b5d454dc2199ae225699f3467e5a07f3b955bafd" + integrity sha512-sKJf1+ceQBr4SMkvQnBDNDtf4TXpVhVGateu0t918bl30FnbE2m4vNLX+VWe/dpjlb+HugGYzW7uQXH98HPEYw== + has-flag@^4.0.0: version "4.0.0" resolved "https://registry.yarnpkg.com/has-flag/-/has-flag-4.0.0.tgz#944771fd9c81c81265c4d6941860da06bb59479b" @@ -581,6 +679,11 @@ iconv-lite@^0.6.2: dependencies: safer-buffer ">= 2.1.2 < 3.0.0" +ignore-by-default@^1.0.1: + version "1.0.1" + resolved "https://registry.yarnpkg.com/ignore-by-default/-/ignore-by-default-1.0.1.tgz#48ca6d72f6c6a3af00a9ad4ae6876be3889e2b09" + integrity sha512-Ius2VYcGNk7T90CppJqcIkS5ooHUZyIQK+ClZfMfMNFEF9VSE73Fq+906u/CWu92x4gzZMWOwfFYckPObzdEbA== + imurmurhash@^0.1.4: version "0.1.4" resolved "https://registry.yarnpkg.com/imurmurhash/-/imurmurhash-0.1.4.tgz#9218b9b2b928a238b13dc4fb6b6d576f231453ea" @@ -619,16 +722,40 @@ ipaddr.js@1.9.1: resolved "https://registry.yarnpkg.com/ipaddr.js/-/ipaddr.js-1.9.1.tgz#bff38543eeb8984825079ff3a2a8e6cbd46781b3" integrity sha512-0KI/607xoxSToH7GjN1FfSbLoU0+btTicjsQSWQlh/hZykN8KpmMf7uYwPW3R+akZ6R/w18ZlXSHBYXiYUPO3g== +is-binary-path@~2.1.0: + version "2.1.0" + resolved "https://registry.yarnpkg.com/is-binary-path/-/is-binary-path-2.1.0.tgz#ea1f7f3b80f064236e83470f86c09c254fb45b09" + integrity sha512-ZMERYes6pDydyuGidse7OsHxtbI7WVeUEozgR/g7rd0xUimYNlvZRE/K2MgZTjWy725IfelLeVcEM97mmtRGXw== + dependencies: + binary-extensions "^2.0.0" + +is-extglob@^2.1.1: + version "2.1.1" + resolved "https://registry.yarnpkg.com/is-extglob/-/is-extglob-2.1.1.tgz#a88c02535791f02ed37c76a1b9ea9773c833f8c2" + integrity sha512-SbKbANkN603Vi4jEZv49LeVJMn4yGwsbzZworEoyEiutsN3nJYdbO36zfhGJ6QEDpOZIFkDtnq5JRxmvl3jsoQ== + is-fullwidth-code-point@^3.0.0: version "3.0.0" resolved "https://registry.yarnpkg.com/is-fullwidth-code-point/-/is-fullwidth-code-point-3.0.0.tgz#f116f8064fe90b3f7844a38997c0b75051269f1d" integrity sha512-zymm5+u+sCsSWyD9qNaejV3DFvhCKclKdizYaJUuHA83RLjb7nSuGnddCHGv0hk+KY7BMAlsWeK4Ueg6EV6XQg== +is-glob@^4.0.1, is-glob@~4.0.1: + version "4.0.3" + resolved "https://registry.yarnpkg.com/is-glob/-/is-glob-4.0.3.tgz#64f61e42cbbb2eec2071a9dac0b28ba1e65d5084" + integrity sha512-xelSayHH36ZgE7ZWhli7pW34hNbNl8Ojv5KVmkJD4hBdD3th8Tfk9vYasLM+mXWOZhFkgZfxhLSnrwRr4elSSg== + dependencies: + is-extglob "^2.1.1" + is-lambda@^1.0.1: version "1.0.1" resolved "https://registry.yarnpkg.com/is-lambda/-/is-lambda-1.0.1.tgz#3d9877899e6a53efc0160504cde15f82e6f061d5" integrity sha512-z7CMFGNrENq5iFB9Bqo64Xk6Y9sg+epq1myIcdHaGnbMTYOxvzsEtdYqQUylB7LxfkvgrrjP32T6Ywciio9UIQ== +is-number@^7.0.0: + version "7.0.0" + resolved "https://registry.yarnpkg.com/is-number/-/is-number-7.0.0.tgz#7535345b896734d5f80c4d06c50955527a14f12b" + integrity sha512-41Cifkg6e8TylSpdtTpeLVMqvSBEVzTttHvERD741+pnZ8ANv0004MRL43QKPDlK9cGvNp6NZWZUBlbGXYxxng== + isexe@^2.0.0: version "2.0.0" resolved "https://registry.yarnpkg.com/isexe/-/isexe-2.0.0.tgz#e8fbf374dc556ff8947a10dcb0572d633f2cfa10" @@ -712,7 +839,7 @@ mime@1.6.0: resolved "https://registry.yarnpkg.com/mime/-/mime-1.6.0.tgz#32cd9e5c64553bd58d19a568af452acff04981b1" integrity sha512-x0Vn8spI+wuJ1O6S7gnbaQg8Pxh4NNHb7KSINmEWKiPE4RKOplvijn+NkmYmmRgP68mc70j2EbeTFRsrswaQeg== -minimatch@^3.0.4, minimatch@^3.1.1: +minimatch@^3.0.4, minimatch@^3.1.1, minimatch@^3.1.2: version "3.1.2" resolved "https://registry.yarnpkg.com/minimatch/-/minimatch-3.1.2.tgz#19cd194bfd3e428f049a70817c038d89ab4be35b" integrity sha512-J7p63hRiAjw1NDEww1W7i37+ByIrOWO5XQQAzZ3VOcL0PNybwpfmV/N05zFAzwQ9USyEcX6t3UO+K5aqBQOIHw== @@ -800,7 +927,7 @@ ms@2.1.2: resolved "https://registry.yarnpkg.com/ms/-/ms-2.1.2.tgz#d09d1f357b443f493382a8eb3ccd183872ae6009" integrity sha512-sGkPx+VjMtmA6MX27oA4FBFELFCZZ4S4XqeGOXCv68tT+jb3vk/RyaKWP0PTKyWtmLSM0b+adUTEvbs1PEaH2w== -ms@2.1.3, ms@^2.0.0: +ms@2.1.3, ms@^2.0.0, ms@^2.1.1: version "2.1.3" resolved "https://registry.yarnpkg.com/ms/-/ms-2.1.3.tgz#574c8138ce1d2b5861f0b44579dbadd60c6615b2" integrity sha512-6FlzubTLZG3J2a/NVCAleEhjzq5oxgHyaCU9yYXvcLsvoVaHJq/s5xXI6/XXP6tz7R9xAOtHnSO/tXtF3WRTlA== @@ -838,6 +965,22 @@ node-gyp@8.x: tar "^6.1.2" which "^2.0.2" +nodemon@^2.0.22: + version "2.0.22" + resolved "https://registry.yarnpkg.com/nodemon/-/nodemon-2.0.22.tgz#182c45c3a78da486f673d6c1702e00728daf5258" + integrity sha512-B8YqaKMmyuCO7BowF1Z1/mkPqLk6cs/l63Ojtd6otKjMx47Dq1utxfRxcavH1I7VSaL8n5BUaoutadnsX3AAVQ== + dependencies: + chokidar "^3.5.2" + debug "^3.2.7" + ignore-by-default "^1.0.1" + minimatch "^3.1.2" + pstree.remy "^1.1.8" + semver "^5.7.1" + simple-update-notifier "^1.0.7" + supports-color "^5.5.0" + touch "^3.1.0" + undefsafe "^2.0.5" + nopt@^5.0.0: version "5.0.0" resolved "https://registry.yarnpkg.com/nopt/-/nopt-5.0.0.tgz#530942bb58a512fccafe53fe210f13a25355dc88" @@ -845,6 +988,18 @@ nopt@^5.0.0: dependencies: abbrev "1" +nopt@~1.0.10: + version "1.0.10" + resolved "https://registry.yarnpkg.com/nopt/-/nopt-1.0.10.tgz#6ddd21bd2a31417b92727dd585f8a6f37608ebee" + integrity sha512-NWmpvLSqUrgrAC9HCuxEvb+PSloHpqVu+FqcO4eeF2h5qYRhA7ev6KvelyQAKtegUbC6RypJnlEOhd8vloNKYg== + dependencies: + abbrev "1" + +normalize-path@^3.0.0, normalize-path@~3.0.0: + version "3.0.0" + resolved "https://registry.yarnpkg.com/normalize-path/-/normalize-path-3.0.0.tgz#0dcd69ff23a1c9b11fd0978316644a0388216a65" + integrity sha512-6eZs5Ls3WtCisHWp9S2GUy8dqkpGi4BVSz3GaqiE6ezub0512ESztXUwUB6C6IKbQkY2Pnb/mD4WYojCRwcwLA== + npmlog@^5.0.1: version "5.0.1" resolved "https://registry.yarnpkg.com/npmlog/-/npmlog-5.0.1.tgz#f06678e80e29419ad67ab964e0fa69959c1eb8b0" @@ -882,6 +1037,11 @@ on-finished@2.4.1: dependencies: ee-first "1.1.1" +on-headers@~1.0.2: + version "1.0.2" + resolved "https://registry.yarnpkg.com/on-headers/-/on-headers-1.0.2.tgz#772b0ae6aaa525c399e489adfad90c403eb3c28f" + integrity sha512-pZAE+FJLoyITytdqK0U5s+FIpjN0JP3OzFi/u8Rx+EV5/W+JTWGXG8xFzevE7AjBfDqHv/8vL8qQsIhHnqRkrA== + once@^1.3.0: version "1.4.0" resolved "https://registry.yarnpkg.com/once/-/once-1.4.0.tgz#583b1aa775961d4b113ac17d9c50baef9dd76bd1" @@ -911,6 +1071,11 @@ path-to-regexp@0.1.7: resolved "https://registry.yarnpkg.com/path-to-regexp/-/path-to-regexp-0.1.7.tgz#df604178005f522f15eb4490e7247a1bfaa67f8c" integrity sha512-5DFkuoqlv1uYQKxy8omFBeJPQcdoE07Kv2sferDCrAq1ohOU+MSDswDIbnx3YAM60qIOnYa53wBhXW0EbMonrQ== +picomatch@^2.0.4, picomatch@^2.2.1: + version "2.3.1" + resolved "https://registry.yarnpkg.com/picomatch/-/picomatch-2.3.1.tgz#3ba3833733646d9d3e4995946c1365a67fb07a42" + integrity sha512-JU3teHTNjmE2VCGFzuY8EXzCDVwEqB2a8fsIvwaStHhAWJEeVd1o1QD80CU6+ZdEXXSLbSsuLwJjkCBWqRQUVA== + promise-inflight@^1.0.1: version "1.0.1" resolved "https://registry.yarnpkg.com/promise-inflight/-/promise-inflight-1.0.1.tgz#98472870bf228132fcbdd868129bad12c3c029e3" @@ -932,6 +1097,11 @@ proxy-addr@~2.0.7: forwarded "0.2.0" ipaddr.js "1.9.1" +pstree.remy@^1.1.8: + version "1.1.8" + resolved "https://registry.yarnpkg.com/pstree.remy/-/pstree.remy-1.1.8.tgz#c242224f4a67c21f686839bbdb4ac282b8373d3a" + integrity sha512-77DZwxQmxKnu3aR542U+X8FypNzbfJ+C5XQDk3uWjWxn6151aIMGthWYRXTqT1E5oJvg+ljaa2OJi+VfvCOQ8w== + qs@6.11.0: version "6.11.0" resolved "https://registry.yarnpkg.com/qs/-/qs-6.11.0.tgz#fd0d963446f7a65e1367e01abd85429453f0c37a" @@ -939,6 +1109,11 @@ qs@6.11.0: dependencies: side-channel "^1.0.4" +random-bytes@~1.0.0: + version "1.0.0" + resolved "https://registry.yarnpkg.com/random-bytes/-/random-bytes-1.0.0.tgz#4f68a1dc0ae58bd3fb95848c30324db75d64360b" + integrity sha512-iv7LhNVO047HzYR3InF6pUcUsPQiHTM1Qal51DcGSuZFBil1aBBWG5eHPNek7bvILMaYJ/8RU1e8w1AMdHmLQQ== + range-parser@~1.2.1: version "1.2.1" resolved "https://registry.yarnpkg.com/range-parser/-/range-parser-1.2.1.tgz#3cf37023d199e1c24d1a55b84800c2f3e6468031" @@ -973,6 +1148,13 @@ readable-stream@^3.6.0: string_decoder "^1.1.1" util-deprecate "^1.0.1" +readdirp@~3.6.0: + version "3.6.0" + resolved "https://registry.yarnpkg.com/readdirp/-/readdirp-3.6.0.tgz#74a370bd857116e245b29cc97340cd431a02a6c7" + integrity sha512-hOS089on8RduqdbhvQ5Z37A0ESjsqz6qnRcffsMU3495FuTdqSm+7bhJ29JvIOsBDEEnan5DPu9t3To9VRlMzA== + dependencies: + picomatch "^2.2.1" + retry@^0.12.0: version "0.12.0" resolved "https://registry.yarnpkg.com/retry/-/retry-0.12.0.tgz#1b42a6266a21f07421d1b0b54b7dc167b01c013b" @@ -995,6 +1177,11 @@ safe-buffer@5.2.1, safe-buffer@~5.2.0: resolved "https://registry.yarnpkg.com/safer-buffer/-/safer-buffer-2.1.2.tgz#44fa161b0187b9549dd84bb91802f9bd8385cd6a" integrity sha512-YZo3K82SD7Riyi0E1EQPojLz7kpepnSQI9IyPbHHg1XXXevb5dJI7tpyN2ADxGcQbHG7vcyRHk0cbwqcQriUtg== +semver@^5.7.1: + version "5.7.1" + resolved "https://registry.yarnpkg.com/semver/-/semver-5.7.1.tgz#a954f931aeba508d307bbf069eff0c01c96116f7" + integrity sha512-sauaDf/PZdVgrLTNYHRtpXa1iRiKcaebiKQ1BJdpQlWH2lCvexQdX55snPFyK7QzpudqbCI0qXFfOasHdyNDGQ== + semver@^6.0.0: version "6.3.0" resolved "https://registry.yarnpkg.com/semver/-/semver-6.3.0.tgz#ee0a64c8af5e8ceea67687b133761e1becbd1d3d" @@ -1007,6 +1194,11 @@ semver@^7.3.5: dependencies: lru-cache "^6.0.0" +semver@~7.0.0: + version "7.0.0" + resolved "https://registry.yarnpkg.com/semver/-/semver-7.0.0.tgz#5f3ca35761e47e05b206c6daff2cf814f0316b8e" + integrity sha512-+GB6zVA9LWh6zovYQLALHwv5rb2PHGlJi3lfiqIHxR0uuwCgefcOJc59v9fv1w8GbStwxuuqqAjI9NMAOOgq1A== + send@0.18.0: version "0.18.0" resolved "https://registry.yarnpkg.com/send/-/send-0.18.0.tgz#670167cc654b05f5aa4a767f9113bb371bc706be" @@ -1060,6 +1252,13 @@ signal-exit@^3.0.0, signal-exit@^3.0.7: resolved "https://registry.yarnpkg.com/signal-exit/-/signal-exit-3.0.7.tgz#a9a1767f8af84155114eaabd73f99273c8f59ad9" integrity sha512-wnD2ZE+l+SPC/uoS0vXeE9L1+0wuaMqKlfz9AMUo38JsyLSBWSFcHR1Rri62LZc12vLr1gb3jl7iwQhgwpAbGQ== +simple-update-notifier@^1.0.7: + version "1.1.0" + resolved "https://registry.yarnpkg.com/simple-update-notifier/-/simple-update-notifier-1.1.0.tgz#67694c121de354af592b347cdba798463ed49c82" + integrity sha512-VpsrsJSUcJEseSbMHkrsrAVSdvVS5I96Qo1QAQ4FxQ9wXFcB+pjj7FB7/us9+GcgfW4ziHtYMc1J0PLczb55mg== + dependencies: + semver "~7.0.0" + smart-buffer@^4.2.0: version "4.2.0" resolved "https://registry.yarnpkg.com/smart-buffer/-/smart-buffer-4.2.0.tgz#6e1d71fa4f18c05f7d0ff216dd16a481d0e8d9ae" @@ -1128,6 +1327,13 @@ strip-ansi@^6.0.1: dependencies: ansi-regex "^5.0.1" +supports-color@^5.5.0: + version "5.5.0" + resolved "https://registry.yarnpkg.com/supports-color/-/supports-color-5.5.0.tgz#e2e69a44ac8772f78a1ec0b35b689df6530efc8f" + integrity sha512-QjVjwdXIt408MIiAqCX4oUKsgU2EqAGzs2Ppkm4aQYbjm+ZEWEcW4SfFNTr4uMNZma0ey4f5lgLrkB0aX0QMow== + dependencies: + has-flag "^3.0.0" + supports-color@^7.1.0: version "7.2.0" resolved "https://registry.yarnpkg.com/supports-color/-/supports-color-7.2.0.tgz#1b7dcdcb32b8138801b3e478ba6a51caa89648da" @@ -1147,11 +1353,25 @@ tar@^6.0.2, tar@^6.1.11, tar@^6.1.2: mkdirp "^1.0.3" yallist "^4.0.0" +to-regex-range@^5.0.1: + version "5.0.1" + resolved "https://registry.yarnpkg.com/to-regex-range/-/to-regex-range-5.0.1.tgz#1648c44aae7c8d988a326018ed72f5b4dd0392e4" + integrity sha512-65P7iz6X5yEr1cwcgvQxbbIw7Uk3gOy5dIdtZ4rDveLqhrdJP+Li/Hx6tyK0NEb+2GCyneCMJiGqrADCSNk8sQ== + dependencies: + is-number "^7.0.0" + toidentifier@1.0.1: version "1.0.1" resolved "https://registry.yarnpkg.com/toidentifier/-/toidentifier-1.0.1.tgz#3be34321a88a820ed1bd80dfaa33e479fbb8dd35" integrity sha512-o5sSPKEkg/DIQNmH43V0/uerLrpzVedkUh8tGNvaeXpfpuwjKenlSox/2O/BTlZUtEe+JG7s5YhEz608PlAHRA== +touch@^3.1.0: + version "3.1.0" + resolved "https://registry.yarnpkg.com/touch/-/touch-3.1.0.tgz#fe365f5f75ec9ed4e56825e0bb76d24ab74af83b" + integrity sha512-WBx8Uy5TLtOSRtIq+M03/sKDrXCLHxwDcquSP2c43Le03/9serjQBIztjRz6FkJez9D/hleyAXTBGLwwZUw9lA== + dependencies: + nopt "~1.0.10" + tr46@~0.0.3: version "0.0.3" resolved "https://registry.yarnpkg.com/tr46/-/tr46-0.0.3.tgz#8184fd347dac9cdc185992f3a6622e14b9d9ab6a" @@ -1165,6 +1385,18 @@ type-is@~1.6.18: media-typer "0.3.0" mime-types "~2.1.24" +uid-safe@~2.1.5: + version "2.1.5" + resolved "https://registry.yarnpkg.com/uid-safe/-/uid-safe-2.1.5.tgz#2b3d5c7240e8fc2e58f8aa269e5ee49c0857bd3a" + integrity sha512-KPHm4VL5dDXKz01UuEd88Df+KzynaohSL9fBh096KWAxSKZQDI2uBrVqtvRM4rwrIrRRKsdLNML/lnaaVSRioA== + dependencies: + random-bytes "~1.0.0" + +undefsafe@^2.0.5: + version "2.0.5" + resolved "https://registry.yarnpkg.com/undefsafe/-/undefsafe-2.0.5.tgz#38733b9327bdcd226db889fb723a6efd162e6e2c" + integrity sha512-WxONCrssBM8TSPRqN5EmsjVrsv4A8X12J4ArBiiayv3DyyG3ZlIg6yysuuSYdZsVz3TKcTg2fd//Ujd4CHV1iA== + unique-filename@^1.1.1: version "1.1.1" resolved "https://registry.yarnpkg.com/unique-filename/-/unique-filename-1.1.1.tgz#1d69769369ada0583103a1e6ae87681b56573230" From 2dd12f1050432d4b7bdd8e61e283528459f24af1 Mon Sep 17 00:00:00 2001 From: Sajeda Saju Date: Mon, 3 Apr 2023 02:23:44 +0600 Subject: [PATCH 3/6] authenticatioon done --- index.js | 77 ++++++++++++++++++---------------------------------- package.json | 4 +++ 2 files changed, 31 insertions(+), 50 deletions(-) diff --git a/index.js b/index.js index 26f1073..f2c99e9 100644 --- a/index.js +++ b/index.js @@ -10,6 +10,7 @@ app.use(express.static('public')); app.use(bodyParser.urlencoded({ extended: true })); app.use(cookieParser()) app.use(session({ + key:'user_id', secret: 'your-secret-key', resave: false, saveUninitialized: false @@ -28,16 +29,15 @@ const users=[{ let error; function isLoggedIn(req,res,next) { - console.log('req.session islog',req.session); - console.log('req.session.user',req.session.user); - // if(req.session.user){ - // console.log('req.session.user',req.session.user); - // next() - // } - // else { - // error='user not found' - // next(error); - // } + // console.log('req.session islog',req.session); + // console.log('req.session.user',req.session.user); + if(req.session.user && req.cookies.user_id){ + console.log('req.session.user',req.session.user); + next() + } + else { + res.redirect('/login') + } } @@ -56,9 +56,9 @@ app.post('/registration', async (req, res) => { confirmPassword:data.confirmPassword } if(user.password===user.confirmPassword){ - console.log('hello') users.push(user); res.redirect('/login'); + console.log('hello user',user) } else { error='Password not matched'; @@ -71,7 +71,7 @@ app.post('/registration', async (req, res) => { }); -app.get('/', (req, res) => { +app.get('/', isLoggedIn,(req, res) => { console.log(req.session); res.render('pages/index', {todos}); }); @@ -86,54 +86,31 @@ app.get('/login', (req, res) => { }); app.post('/login', async (req, res) => { - try{ - const data = req.body; - console.log("req session",req.session) - const existingUser = users.filter((currentUser,index)=>currentUser.email == req.body.email && currentUser.password==req.body.password); - console.log("existingUser",existingUser); - if(existingUser.length > 0 && existingUser[0]){ - console.log("existingUser[0]",existingUser[0]); - req.session.user=existingUser[0]; - // res.redirect('/'); - } - console.log('eee') - - /* users.filter((currentUser,index)=>{ - if(currentUser.email===req.body.email && currentUser.password===req.body.password){ - console.log("currentUser",currentUser); - req.session.user=currentUser - console.log(" req.session.currentUser", req.session.currentUser); - //res.redirect('/'); - } - })*/ + const existingUser = users.filter((currentUser,index)=>currentUser.email == req.body.email && currentUser.password==req.body.password); + + if(existingUser.length > 0 && existingUser[0]){ + req.session.user=existingUser[0]; res.redirect('/'); } - catch (e) { - console.log('e',e); - throw e; + else { + res.redirect('/login') } }); +app.post('/', (req, res) => { + const { todo, deleteIndex } = req.body; + if (deleteIndex !== undefined) { + todos.splice(deleteIndex, 1); + } else if (todo !== '') { + todos.push(todo); + } - - -// -// -// app.post('/', (req, res) => { -// const { todo, deleteIndex } = req.body; -// -// if (deleteIndex !== undefined) { -// todos.splice(deleteIndex, 1); -// } else if (todo !== '') { -// todos.push(todo); -// } -// -// res.redirect('/'); -// }); + res.redirect('/'); +}); app.listen(3000, () => { diff --git a/package.json b/package.json index 0bc2e10..94e817f 100644 --- a/package.json +++ b/package.json @@ -13,5 +13,9 @@ "express-session": "^1.17.3", "nodemon": "^2.0.22", "sqlite3": "^5.1.6" + }, + "scripts": { + "start": "nodemon -w ./ -e js,ejs,json index.js", + "test": "echo \"Error: no test specified\" && exit 1" } } From 71d04921b08e1eefa233c153a38846c54a7ce416 Mon Sep 17 00:00:00 2001 From: "sajeda.akhter704@gmail.com" Date: Mon, 10 Apr 2023 16:40:37 +0600 Subject: [PATCH 4/6] sequelize and sqlite added --- App/encyptDecryptPass/encryptPass.js | 17 ++++++ App/models/database.js | 16 ++++++ App/models/userModel.js | 23 ++++++++ index.js | 82 +++++++++++++++------------ package.json | 2 + users.sqlite | Bin 0 -> 12288 bytes 6 files changed, 105 insertions(+), 35 deletions(-) create mode 100644 App/encyptDecryptPass/encryptPass.js create mode 100644 App/models/database.js create mode 100644 App/models/userModel.js create mode 100644 users.sqlite diff --git a/App/encyptDecryptPass/encryptPass.js b/App/encyptDecryptPass/encryptPass.js new file mode 100644 index 0000000..3352d9a --- /dev/null +++ b/App/encyptDecryptPass/encryptPass.js @@ -0,0 +1,17 @@ +module.exports(pass)=>{ + const bcrypt = require("bcrypt"); +const saltRounds = 10; +const plainTextPassword1 = "DFGh5546*%^__90"; +const result=bcrypt + .genSalt(saltRounds) + .then(salt => { + console.log(`Salt: ${salt}`); + return bcrypt.hash(plainTextPassword1, salt); + }) + .then(hash => { + console.log(`Hash: ${hash}`); + // Store hash in your password DB. + }) + .catch(err => console.error(err.message)); + return result +} diff --git a/App/models/database.js b/App/models/database.js new file mode 100644 index 0000000..81ec8dc --- /dev/null +++ b/App/models/database.js @@ -0,0 +1,16 @@ +const { Sequelize,DataTypes } = require('sequelize'); + +const sequelize = new Sequelize('test-db', 'sajeda', 'password', { + dialect: 'sqlite', + host: './users.sqlite', + +}); + + const db = {}; + + db.Sequelize = Sequelize; + db.sequelize = sequelize; + +db.users = require("./userModel.js")(sequelize, Sequelize); + +module.exports = db; \ No newline at end of file diff --git a/App/models/userModel.js b/App/models/userModel.js new file mode 100644 index 0000000..688372e --- /dev/null +++ b/App/models/userModel.js @@ -0,0 +1,23 @@ +module.exports = (sequelize, Sequelize) => { + const User = sequelize.define("user", { + id: { + type: Sequelize.INTEGER, + autoIncrement: true, + primaryKey: true + }, + userName: { + type: Sequelize.STRING, + }, + email: { + type: Sequelize.STRING, + }, + password: { + type: Sequelize.STRING, + }, + confirmPassword:{ + type: Sequelize.STRING, + } + }); + + return User; +}; \ No newline at end of file diff --git a/index.js b/index.js index f2c99e9..8c2d10f 100644 --- a/index.js +++ b/index.js @@ -2,36 +2,34 @@ const express = require('express'); const bodyParser = require('body-parser'); var cookieParser = require('cookie-parser') var session = require('express-session') - const app = express(); +const db = require("./App/models/database"); +const bcrypt = require('bcryptjs'); app.use(express.static('public')); - app.use(bodyParser.urlencoded({ extended: true })); app.use(cookieParser()) +app.set('view engine', 'ejs'); + +const oneDay = 1000 * 60 * 60 * 24; + app.use(session({ key:'user_id', secret: 'your-secret-key', resave: false, + cookie: { + maxAge: oneDay,secure: true + }, saveUninitialized: false })) -app.set('view engine', 'ejs'); - const todos = []; -const users=[{ - userName: "userName", - email:"email", - password:"password", - confirmPassword:"password" -}]; let error; function isLoggedIn(req,res,next) { - // console.log('req.session islog',req.session); - // console.log('req.session.user',req.session.user); - if(req.session.user && req.cookies.user_id){ + console.log('isLoggedIn',req.session.id,req.cookies.user_id) + if(req.session.id){ console.log('req.session.user',req.session.user); next() } @@ -47,54 +45,60 @@ app.get('/registration', (req, res) => { }); app.post('/registration', async (req, res) => { - + const data = req.body; - const user={ + const user={ userName: data.userName, email:data.email, password:data.password, confirmPassword:data.confirmPassword } - if(user.password===user.confirmPassword){ - users.push(user); - res.redirect('/login'); - console.log('hello user',user) - } - else { - error='Password not matched'; - res.render('pages/registration'); - console.log('===',data) - } - + if(user.password===user.confirmPassword){ + db.users.create(data) + .then(()=>{ + res.redirect('/login') + }) + .catch(e=>console.log(e)) + } + else { + error='Password not matched'; + console.log('===',data) + } }); app.get('/', isLoggedIn,(req, res) => { - console.log(req.session); res.render('pages/index', {todos}); }); -app.use('/',function (err, req, res, next){ - console.log('err',err) - // res.redirect('/login') -}) - app.get('/login', (req, res) => { res.render('pages/login'); }); app.post('/login', async (req, res) => { - const existingUser = users.filter((currentUser,index)=>currentUser.email == req.body.email && currentUser.password==req.body.password); - if(existingUser.length > 0 && existingUser[0]){ - req.session.user=existingUser[0]; + try{ + const isExistUser = await db.users.findOne({ + where: { + email: req.body.email, + }, + raw:true, + }); + + + if(isExistUser){ + req.session.userId=isExistUser.id; res.redirect('/'); + } else { res.redirect('/login') } + } catch(err){ + res.redirect('/login') + } }); @@ -116,3 +120,11 @@ app.post('/', (req, res) => { app.listen(3000, () => { console.log('Server listening on port 3000'); }); + +db.sequelize.sync() + .then(() => { + console.log("Synced db."); + }) + .catch((err) => { + console.log("Failed to sync db: " + err.message); + }); diff --git a/package.json b/package.json index 94e817f..a493b2c 100644 --- a/package.json +++ b/package.json @@ -6,12 +6,14 @@ "license": "MIT", "private": true, "dependencies": { + "bcrypt": "^5.1.0", "body-parser": "^1.20.2", "cookie-parser": "^1.4.6", "ejs": "^3.1.9", "express": "^4.18.2", "express-session": "^1.17.3", "nodemon": "^2.0.22", + "sequelize": "^6.31.0", "sqlite3": "^5.1.6" }, "scripts": { diff --git a/users.sqlite b/users.sqlite new file mode 100644 index 0000000000000000000000000000000000000000..41e314e06a1608d75c07776e32d502e52bd87d26 GIT binary patch literal 12288 zcmeI2!EVzq7{{Ghp#tw&-X|gOFAI|!{u$lh6a^%8e~5fkrtldRv-|=91b_e#00KY&2mk>f00e*l z5C8%|;7%zgvdwu4I7iawR`YQA<#{S?ad7Is7S*B}Y*L+yFaANG*!0Y=P zd}j6iJ7K9(sR$qTW6yEu2Vs24OwDh{&)Rsc-Rrcm({!!JRtxX7oQBytz)!3LY(8^$ zohF~uu$t}$jwdwvue9-Vv$bQJtu;l{s$B5A4q%Z-)UlPYgdA4!XUdzc*J8 zd~f7;%uyRZ;_G#thJ~BEE^a=n*P}GfJ9Fh~bT$`PzN6x&;>%*G@U<{5l*KRNsdzvC zE&nn9fJ+cS00;m9AOHk_01yBIKmZ8*>jXMt4y~@1N{$nHCw|An-s$m(pKQF7@%EeO z{Bbnw%ZkdILKJnA&`p_OVlcJFD3xkN$LoYJLatZsd=3?PIL-x+t7EsAMwqHdRMk?P zX4U#i4&CM9IDsGdyam&4)L@Ds8MKz_mQ?NAlf1e-5>8u;$`aM*I3-kj`5GskX}YA; z<~Su(J2!FicqDS7Of@7;GtyU=P!E@Ks9Y_V9p~hDIEeaXNu|r0QeIS=@K+;xI;H9U z3H5OCul*IK5{YPXx_?4#+~Tz^#kCH8$8@?1VU$RQmY!}>Z7keurw~bz)9G@%Db>zS z>J;TRotSJyyF=F1)O99RJ2P?W@VM%vGomt8m9*4-Hmkl6a_C+qs`%w@j^7|eFASo7 z)wssAcmt Date: Tue, 11 Apr 2023 07:50:11 +0600 Subject: [PATCH 5/6] pass hash done --- App/encyptDecryptPass/encryptPass.js | 17 --- App/models/userModel.js | 15 ++- index.js | 176 ++++++++++++++------------- public/registerStyle.css | 12 ++ users.sqlite | Bin 12288 -> 16384 bytes views/pages/login.ejs | 8 +- views/pages/registration.ejs | 8 +- yarn.lock | 123 ++++++++++++++++++- 8 files changed, 251 insertions(+), 108 deletions(-) delete mode 100644 App/encyptDecryptPass/encryptPass.js diff --git a/App/encyptDecryptPass/encryptPass.js b/App/encyptDecryptPass/encryptPass.js deleted file mode 100644 index 3352d9a..0000000 --- a/App/encyptDecryptPass/encryptPass.js +++ /dev/null @@ -1,17 +0,0 @@ -module.exports(pass)=>{ - const bcrypt = require("bcrypt"); -const saltRounds = 10; -const plainTextPassword1 = "DFGh5546*%^__90"; -const result=bcrypt - .genSalt(saltRounds) - .then(salt => { - console.log(`Salt: ${salt}`); - return bcrypt.hash(plainTextPassword1, salt); - }) - .then(hash => { - console.log(`Hash: ${hash}`); - // Store hash in your password DB. - }) - .catch(err => console.error(err.message)); - return result -} diff --git a/App/models/userModel.js b/App/models/userModel.js index 688372e..baddb79 100644 --- a/App/models/userModel.js +++ b/App/models/userModel.js @@ -3,13 +3,26 @@ module.exports = (sequelize, Sequelize) => { id: { type: Sequelize.INTEGER, autoIncrement: true, - primaryKey: true + primaryKey: true, + unique: true }, userName: { type: Sequelize.STRING, + allowNull: false, + // validate: { + // len: [5, 10] + // } + }, email: { type: Sequelize.STRING, + allowNull: false, + unique: true, + validate: { + isEmail: true, + } + + }, password: { type: Sequelize.STRING, diff --git a/index.js b/index.js index 8c2d10f..ec30d5f 100644 --- a/index.js +++ b/index.js @@ -1,130 +1,132 @@ -const express = require('express'); -const bodyParser = require('body-parser'); -var cookieParser = require('cookie-parser') -var session = require('express-session') +const express = require("express"); +const bodyParser = require("body-parser"); +var cookieParser = require("cookie-parser"); +var session = require("express-session"); const app = express(); const db = require("./App/models/database"); -const bcrypt = require('bcryptjs'); +const bcrypt = require("bcrypt"); +const { log } = require("console"); -app.use(express.static('public')); +app.use(express.static("public")); app.use(bodyParser.urlencoded({ extended: true })); -app.use(cookieParser()) -app.set('view engine', 'ejs'); +app.use(cookieParser()); +app.set("view engine", "ejs"); const oneDay = 1000 * 60 * 60 * 24; -app.use(session({ - key:'user_id', - secret: 'your-secret-key', +app.use( + session({ + key: "user_id", + secret: "your-secret-key", resave: false, cookie: { - maxAge: oneDay,secure: true + maxAge: oneDay, }, - saveUninitialized: false -})) - + saveUninitialized: false, + }) +); const todos = []; let error; -function isLoggedIn(req,res,next) { - console.log('isLoggedIn',req.session.id,req.cookies.user_id) - if(req.session.id){ - console.log('req.session.user',req.session.user); - next() - } - else { - res.redirect('/login') - } - +function isLoggedIn(req, res, next) { + if (req.session.id && req.session.userId) { + next(); + } else { + res.redirect("/login"); + } } -app.get('/registration', (req, res) => { - - res.render('pages/registration') +app.get("/registration", (req, res) => { + res.render("pages/registration"); }); -app.post('/registration', async (req, res) => { - - const data = req.body; - const user={ - userName: data.userName, - email:data.email, - password:data.password, - confirmPassword:data.confirmPassword - } +app.post("/registration", async (req, res) => { + try { + const { userName, email, password, confirmPassword } = req.body; + let user; + if (password === confirmPassword) { + + bcrypt.hash(password, 10,async function (err, hash) { + user = { + userName: userName, + email: email, + password: hash, + }; + await db.users.create(user); + res.status(200); + res.redirect("/login"); + }); - if(user.password===user.confirmPassword){ - db.users.create(data) - .then(()=>{ - res.redirect('/login') - }) - .catch(e=>console.log(e)) - } - else { - error='Password not matched'; - console.log('===',data) - } + } + } catch (e) { + console.log('===============error=============',e) + res.status(500).json(e.message); + } }); - -app.get('/', isLoggedIn,(req, res) => { - res.render('pages/index', {todos}); +app.get("/", isLoggedIn, (req, res) => { + res.render("pages/index", { todos }); }); -app.get('/login', (req, res) => { - res.render('pages/login'); +app.get("/login", (req, res) => { + res.render("pages/login"); }); -app.post('/login', async (req, res) => { +app.post("/login", async (req, res) => { + const { email, password } = req.body; - try{ + try { const isExistUser = await db.users.findOne({ - where: { - email: req.body.email, - }, - raw:true, + where: { + email: email, + }, + raw: true, }); - - if(isExistUser){ - req.session.userId=isExistUser.id; - res.redirect('/'); - + if (isExistUser) { + bcrypt.compare(password, isExistUser.password, function (err, result) { + if (result === true) { + + res.status(200); + req.session.userId = isExistUser.id; + console.log(' req.session.userId=====', req.session.userId, req.session); + res.redirect("/"); + } + else { + res.status(404); + } + }); + } else { + res.redirect("/login"); } - else { - res.redirect('/login') - } - } catch(err){ - res.redirect('/login') - } - + } catch (err) { + res.redirect("/login"); + } }); +app.post("/", (req, res) => { + const { todo, deleteIndex } = req.body; + if (deleteIndex !== undefined) { + todos.splice(deleteIndex, 1); + } else if (todo !== "") { + todos.push(todo); + } -app.post('/', (req, res) => { - const { todo, deleteIndex } = req.body; - - if (deleteIndex !== undefined) { - todos.splice(deleteIndex, 1); - } else if (todo !== '') { - todos.push(todo); - } - - res.redirect('/'); + res.redirect("/"); }); - app.listen(3000, () => { - console.log('Server listening on port 3000'); + console.log("Server listening on port 3000"); }); -db.sequelize.sync() +db.sequelize + .sync({force:false}) .then(() => { console.log("Synced db."); }) - .catch((err) => { - console.log("Failed to sync db: " + err.message); - }); + .catch((err) => { + console.log("Failed to sync db: " + err.message); + }); diff --git a/public/registerStyle.css b/public/registerStyle.css index 47099a1..9f8f7f2 100644 --- a/public/registerStyle.css +++ b/public/registerStyle.css @@ -48,5 +48,17 @@ button{ button:hover{ background: #4e7664; } +.new{ + display: flex; + justify-content: space-between; + margin-top: 0; +} +.signUp{ + color: dodgerblue; + font-family: ui-rounded; + font-weight: 600; + text-decoration: underline; + cursor: pointer; +} diff --git a/users.sqlite b/users.sqlite index 41e314e06a1608d75c07776e32d502e52bd87d26..ca0a60da5d42c71904a9ac503a2cf2571b57ab08 100644 GIT binary patch delta 1231 zcmZ{k%}*LZ7{-@?qTsSxswCQ`+r&eOI?m4S0=r4m2+PN|0)hf!(kLIRtX-shfAjz} zJ+!wPjvjmHu_30%9-1^gH0inV-lXXt(4@yARRomH>`Z2V$@4z%yYEb6uhCGwU6`52 zFig{VFCX>sGHsm4!2WNXP-ANR1T(bp6XJV2YsPgijcTpQWKz8vYvF{nO8Wd;q%X9- zzD({(>6onSk>RzV!?PBAfLPYt!(_-O213r(YogL>8-g0*FZQjaCBDjL&A92INrlNr zNvV?0Dsn;EjTH--?tZtJmvZ@Cs=J(9oH+N0pHI^mM!YAU5=O%pL(wpcf5e~R_oseL zy_tHXKhwALUcFZLS@%N6PJWm)PrRGB+s^*d)|+b6Bt{U;F-#O=u|*VLq~M|`$u4)T zK!+nm)|=iqbURAvDp0Hr_u;BN16?UGYY#tpzUFp%qMo4Wr(@xK8bL&_KxT!4B%*o9 z@GJ#vkRq2Lle7Gp1W=%DOk%6Ihs01ZzBIj!Y?^1fvB`Edso?Hq*7BhBTBTu zLYKE(%2&%?H+LYe7q10kXuv^kgpDJ905{dMJsXL{MK!oAP~sl0y2Bop*_6{_O9!%6 z5D_2Oq!LK%#3@C|D6%@(cwy4&H2LhzipqRO$>*Gr)pI$P7_|7$F0B3IPr=H=7xvg5gSqzd9lrry delta 136 zcmZo@U~EX3AT7wpz`(!^#4x}(QO6i4sF(JV7bwKUw~>LLnQ!A}K?O&?$s75CSQ@Js z*(bB}ADdjqXRHKL!pnV=fqyxFEWa?{5x!DBQQob**}RH8*Lga5?744lY~0Sh`KGis e3mYT
- +
diff --git a/views/pages/registration.ejs b/views/pages/registration.ejs index edb6474..0bf0d32 100644 --- a/views/pages/registration.ejs +++ b/views/pages/registration.ejs @@ -24,7 +24,13 @@
- +
+ Already Have an Account + + + + +
diff --git a/yarn.lock b/yarn.lock index 468f5ad..6b41f48 100644 --- a/yarn.lock +++ b/yarn.lock @@ -7,7 +7,7 @@ resolved "https://registry.yarnpkg.com/@gar/promisify/-/promisify-1.1.3.tgz#555193ab2e3bb3b6adc3d551c9c030d9e860daf6" integrity sha512-k2Ty1JcVojjJFwrg/ThKi2ujJ7XNLYaFGNB/bWT9wGR+oSMJHMa5w+CUq6p/pVrKeNNgA7pCqEcjSnHVoqJQFw== -"@mapbox/node-pre-gyp@^1.0.0": +"@mapbox/node-pre-gyp@^1.0.0", "@mapbox/node-pre-gyp@^1.0.10": version "1.0.10" resolved "https://registry.yarnpkg.com/@mapbox/node-pre-gyp/-/node-pre-gyp-1.0.10.tgz#8e6735ccebbb1581e5a7e652244cadc8a844d03c" integrity sha512-4ySo4CjzStuprMwk35H5pPbkymjv1SF3jGLj6rAHp/xT/RF7TL7bd9CTm1xDY49K2qF7jmR/g7k+SkLETP6opA== @@ -43,6 +43,28 @@ resolved "https://registry.yarnpkg.com/@tootallnate/once/-/once-1.1.2.tgz#ccb91445360179a04e7fe6aff78c00ffc1eeaf82" integrity sha512-RbzJvlNzmRq5c3O09UipeuXno4tA1FE6ikOjxZK0tuxVv3412l64l5t1W5pj4+rJq9vpkm/kwiR07aZXnsKPxw== +"@types/debug@^4.1.7": + version "4.1.7" + resolved "https://registry.yarnpkg.com/@types/debug/-/debug-4.1.7.tgz#7cc0ea761509124709b8b2d1090d8f6c17aadb82" + integrity sha512-9AonUzyTjXXhEOa0DnqpzZi6VHlqKMswga9EXjpXnnqxwLtdvPPtlO8evrI5D9S6asFRCQ6v+wpiUKbw+vKqyg== + dependencies: + "@types/ms" "*" + +"@types/ms@*": + version "0.7.31" + resolved "https://registry.yarnpkg.com/@types/ms/-/ms-0.7.31.tgz#31b7ca6407128a3d2bbc27fe2d21b345397f6197" + integrity sha512-iiUgKzV9AuaEkZqkOLDIvlQiL6ltuZd9tGcW3gwpnX8JbuiuhFlEGmmFXEXkN50Cvq7Os88IY2v0dkDqXYWVgA== + +"@types/node@*": + version "18.15.11" + resolved "https://registry.yarnpkg.com/@types/node/-/node-18.15.11.tgz#b3b790f09cb1696cffcec605de025b088fa4225f" + integrity sha512-E5Kwq2n4SbMzQOn6wnmBjuK9ouqlURrcZDVfbo9ftDDTFt3nk7ZKK4GMOzoYgnpQJKcxwQw+lGaBvvlMo0qN/Q== + +"@types/validator@^13.7.1": + version "13.7.14" + resolved "https://registry.yarnpkg.com/@types/validator/-/validator-13.7.14.tgz#5512aef43ba353ea2fe2d0d8c7ce71c75c2ad9e6" + integrity sha512-J6OAed6rhN6zyqL9Of6ZMamhlsOEU/poBVvbHr/dKOYKTeuYYMlDkMv+b6UUV0o2i0tw73cgyv/97WTWaUl0/g== + abbrev@1: version "1.1.1" resolved "https://registry.yarnpkg.com/abbrev/-/abbrev-1.1.1.tgz#f8f2c887ad10bf67f634f005b6987fed3179aac8" @@ -136,6 +158,14 @@ balanced-match@^1.0.0: resolved "https://registry.yarnpkg.com/balanced-match/-/balanced-match-1.0.2.tgz#e83e3a7e3f300b34cb9d87f615fa0cbf357690ee" integrity sha512-3oSeUO0TMV67hN1AmbXsK4yaqU7tjiHlbxRDZOpH0KW9+CeX4bRAaX0Anxt0tx2MrpRpWwQaPwIlISEJhYU5Pw== +bcrypt@^5.1.0: + version "5.1.0" + resolved "https://registry.yarnpkg.com/bcrypt/-/bcrypt-5.1.0.tgz#bbb27665dbc400480a524d8991ac7434e8529e17" + integrity sha512-RHBS7HI5N5tEnGTmtR/pppX0mmDSBpQ4aCBsj7CEQfYXDcO74A8sIBYcJMuCsis2E81zDxeENYhv66oZwLiA+Q== + dependencies: + "@mapbox/node-pre-gyp" "^1.0.10" + node-addon-api "^5.0.0" + binary-extensions@^2.0.0: version "2.2.0" resolved "https://registry.yarnpkg.com/binary-extensions/-/binary-extensions-2.2.0.tgz#75f502eeaf9ffde42fc98829645be4ea76bd9e2d" @@ -377,6 +407,11 @@ detect-libc@^2.0.0: resolved "https://registry.yarnpkg.com/detect-libc/-/detect-libc-2.0.1.tgz#e1897aa88fa6ad197862937fbc0441ef352ee0cd" integrity sha512-463v3ZeIrcWtdgIg6vI6XUncguvr2TnGl4SzDXinkt9mSLpBJKXT3mW6xT3VQdDN11+WVs29pgvivTc4Lp8v+w== +dottie@^2.0.2: + version "2.0.3" + resolved "https://registry.yarnpkg.com/dottie/-/dottie-2.0.3.tgz#797a4f4c92a9a65499806be4051b9d9dcd5a5d77" + integrity sha512-4liA0PuRkZWQFQjwBypdxPfZaRWiv5tkhMXY2hzsa2pNf5s7U3m9cwUchfNKe8wZQxdGPQQzO6Rm2uGe0rvohQ== + ee-first@1.1.1: version "1.1.1" resolved "https://registry.yarnpkg.com/ee-first/-/ee-first-1.1.1.tgz#590c61156b0ae2f4f0255732a158b266bc56b21d" @@ -699,6 +734,11 @@ infer-owner@^1.0.4: resolved "https://registry.yarnpkg.com/infer-owner/-/infer-owner-1.0.4.tgz#c4cefcaa8e51051c2a40ba2ce8a3d27295af9467" integrity sha512-IClj+Xz94+d7irH5qRyfJonOdfTzuDaifE6ZPWfx0N0+/ATZCbuTPq2prFl526urkQd90WyUKIh1DfBQ2hMz9A== +inflection@^1.13.2: + version "1.13.4" + resolved "https://registry.yarnpkg.com/inflection/-/inflection-1.13.4.tgz#65aa696c4e2da6225b148d7a154c449366633a32" + integrity sha512-6I/HUDeYFfuNCVS3td055BaXBwKYuzw7K3ExVMStBowKo9oOAMJIXIHvdyR3iboTCp1b+1i5DSkIZTcwIktuDw== + inflight@^1.0.4: version "1.0.6" resolved "https://registry.yarnpkg.com/inflight/-/inflight-1.0.6.tgz#49bd6331d7d02d0c09bc910a1075ba8165b56df9" @@ -771,6 +811,11 @@ jake@^10.8.5: filelist "^1.0.1" minimatch "^3.0.4" +lodash@^4.17.21: + version "4.17.21" + resolved "https://registry.yarnpkg.com/lodash/-/lodash-4.17.21.tgz#679591c564c3bffaae8454cf0b3df370c3d6911c" + integrity sha512-v2kDEe57lecTulaDIuNTPy3Ry4gLGJ6Z1O3vE1krgXZNrsQ+LFTGHVxVjcXPs17LhbZVGedAJv8XZ1tvj5FvSg== + lru-cache@^6.0.0: version "6.0.0" resolved "https://registry.yarnpkg.com/lru-cache/-/lru-cache-6.0.0.tgz#6d6fe6570ebd96aaf90fcad1dafa3b2566db3a94" @@ -917,6 +962,18 @@ mkdirp@^1.0.3, mkdirp@^1.0.4: resolved "https://registry.yarnpkg.com/mkdirp/-/mkdirp-1.0.4.tgz#3eb5ed62622756d79a5f0e2a221dfebad75c2f7e" integrity sha512-vVqVZQyf3WLx2Shd0qJ9xuvqgAyKPLAiqITEtqW0oIUjzo3PePDd6fW9iFz30ef7Ysp/oiWqbhszeGWW2T6Gzw== +moment-timezone@^0.5.35: + version "0.5.43" + resolved "https://registry.yarnpkg.com/moment-timezone/-/moment-timezone-0.5.43.tgz#3dd7f3d0c67f78c23cd1906b9b2137a09b3c4790" + integrity sha512-72j3aNyuIsDxdF1i7CEgV2FfxM1r6aaqJyLB2vwb33mXYyoyLly+F1zbWqhA3/bVIoJ4szlUoMbUnVdid32NUQ== + dependencies: + moment "^2.29.4" + +moment@^2.29.1, moment@^2.29.4: + version "2.29.4" + resolved "https://registry.yarnpkg.com/moment/-/moment-2.29.4.tgz#3dbe052889fe7c1b2ed966fcb3a77328964ef108" + integrity sha512-5LC9SOxjSc2HF6vO2CyuTDNivEdoz2IvyJJGj6X8DJ0eFyfszE0QiEd+iXmBvUP3WHxSjFH/vIsA0EN00cgr8w== + ms@2.0.0: version "2.0.0" resolved "https://registry.yarnpkg.com/ms/-/ms-2.0.0.tgz#5608aeadfc00be6c2901df5f9861788de0d597c8" @@ -942,6 +999,11 @@ node-addon-api@^4.2.0: resolved "https://registry.yarnpkg.com/node-addon-api/-/node-addon-api-4.3.0.tgz#52a1a0b475193e0928e98e0426a0d1254782b77f" integrity sha512-73sE9+3UaLYYFmDsFZnqCInzPyh3MqIwZO9cw58yIqAZhONrrabrYyYe3TuIqtIiOuTXVhsGau8hcrhhwSsDIQ== +node-addon-api@^5.0.0: + version "5.1.0" + resolved "https://registry.yarnpkg.com/node-addon-api/-/node-addon-api-5.1.0.tgz#49da1ca055e109a23d537e9de43c09cca21eb762" + integrity sha512-eh0GgfEkpnoWDq+VY8OyvYhFEzBk6jIYbRKdIlyTiAXIVJ8PyBaKb0rp7oDtoddbdoHWhq8wwr+XZ81F1rpNdA== + node-fetch@^2.6.7: version "2.6.9" resolved "https://registry.yarnpkg.com/node-fetch/-/node-fetch-2.6.9.tgz#7c7f744b5cc6eb5fd404e0c7a9fec630a55657e6" @@ -1071,6 +1133,11 @@ path-to-regexp@0.1.7: resolved "https://registry.yarnpkg.com/path-to-regexp/-/path-to-regexp-0.1.7.tgz#df604178005f522f15eb4490e7247a1bfaa67f8c" integrity sha512-5DFkuoqlv1uYQKxy8omFBeJPQcdoE07Kv2sferDCrAq1ohOU+MSDswDIbnx3YAM60qIOnYa53wBhXW0EbMonrQ== +pg-connection-string@^2.5.0: + version "2.5.0" + resolved "https://registry.yarnpkg.com/pg-connection-string/-/pg-connection-string-2.5.0.tgz#538cadd0f7e603fc09a12590f3b8a452c2c0cf34" + integrity sha512-r5o/V/ORTA6TmUnyWZR9nCj1klXCO2CEKNRlVuJptZe85QuhFayC7WeMic7ndayT5IRIR0S0xFxFi2ousartlQ== + picomatch@^2.0.4, picomatch@^2.2.1: version "2.3.1" resolved "https://registry.yarnpkg.com/picomatch/-/picomatch-2.3.1.tgz#3ba3833733646d9d3e4995946c1365a67fb07a42" @@ -1155,6 +1222,11 @@ readdirp@~3.6.0: dependencies: picomatch "^2.2.1" +retry-as-promised@^7.0.3: + version "7.0.4" + resolved "https://registry.yarnpkg.com/retry-as-promised/-/retry-as-promised-7.0.4.tgz#9df73adaeea08cb2948b9d34990549dc13d800a2" + integrity sha512-XgmCoxKWkDofwH8WddD0w85ZfqYz+ZHlr5yo+3YUCfycWawU56T5ckWXsScsj5B8tqUcIG67DxXByo3VUgiAdA== + retry@^0.12.0: version "0.12.0" resolved "https://registry.yarnpkg.com/retry/-/retry-0.12.0.tgz#1b42a6266a21f07421d1b0b54b7dc167b01c013b" @@ -1218,6 +1290,33 @@ send@0.18.0: range-parser "~1.2.1" statuses "2.0.1" +sequelize-pool@^7.1.0: + version "7.1.0" + resolved "https://registry.yarnpkg.com/sequelize-pool/-/sequelize-pool-7.1.0.tgz#210b391af4002762f823188fd6ecfc7413020768" + integrity sha512-G9c0qlIWQSK29pR/5U2JF5dDQeqqHRragoyahj/Nx4KOOQ3CPPfzxnfqFPCSB7x5UgjOgnZ61nSxz+fjDpRlJg== + +sequelize@^6.31.0: + version "6.31.0" + resolved "https://registry.yarnpkg.com/sequelize/-/sequelize-6.31.0.tgz#4e2e724e604e0f42a9a481e31dba918d649ee306" + integrity sha512-nCPVtv+QydBmb3Us2jCNAr1Dx3gST83VZxxrUQn/JAVFCOrmYOgUaPUz5bevummyNf30zfHsZhIKYAOD3ULfTA== + dependencies: + "@types/debug" "^4.1.7" + "@types/validator" "^13.7.1" + debug "^4.3.3" + dottie "^2.0.2" + inflection "^1.13.2" + lodash "^4.17.21" + moment "^2.29.1" + moment-timezone "^0.5.35" + pg-connection-string "^2.5.0" + retry-as-promised "^7.0.3" + semver "^7.3.5" + sequelize-pool "^7.1.0" + toposort-class "^1.0.1" + uuid "^8.3.2" + validator "^13.7.0" + wkx "^0.5.0" + serve-static@1.15.0: version "1.15.0" resolved "https://registry.yarnpkg.com/serve-static/-/serve-static-1.15.0.tgz#faaef08cffe0a1a62f60cad0c4e513cff0ac9540" @@ -1365,6 +1464,11 @@ toidentifier@1.0.1: resolved "https://registry.yarnpkg.com/toidentifier/-/toidentifier-1.0.1.tgz#3be34321a88a820ed1bd80dfaa33e479fbb8dd35" integrity sha512-o5sSPKEkg/DIQNmH43V0/uerLrpzVedkUh8tGNvaeXpfpuwjKenlSox/2O/BTlZUtEe+JG7s5YhEz608PlAHRA== +toposort-class@^1.0.1: + version "1.0.1" + resolved "https://registry.yarnpkg.com/toposort-class/-/toposort-class-1.0.1.tgz#7ffd1f78c8be28c3ba45cd4e1a3f5ee193bd9988" + integrity sha512-OsLcGGbYF3rMjPUf8oKktyvCiUxSbqMMS39m33MAjLTC1DVIH6x3WSt63/M77ihI09+Sdfk1AXvfhCEeUmC7mg== + touch@^3.1.0: version "3.1.0" resolved "https://registry.yarnpkg.com/touch/-/touch-3.1.0.tgz#fe365f5f75ec9ed4e56825e0bb76d24ab74af83b" @@ -1426,6 +1530,16 @@ utils-merge@1.0.1: resolved "https://registry.yarnpkg.com/utils-merge/-/utils-merge-1.0.1.tgz#9f95710f50a267947b2ccc124741c1028427e713" integrity sha512-pMZTvIkT1d+TFGvDOqodOclx0QWkkgi6Tdoa8gC8ffGAAqz9pzPTZWAybbsHHoED/ztMtkv/VoYTYyShUn81hA== +uuid@^8.3.2: + version "8.3.2" + resolved "https://registry.yarnpkg.com/uuid/-/uuid-8.3.2.tgz#80d5b5ced271bb9af6c445f21a1a04c606cefbe2" + integrity sha512-+NYs2QeMWy+GWFOEm9xnn6HCDp0l7QBD7ml8zLUmJ+93Q5NF0NocErnwkTkXVFNiX3/fpC6afS8Dhb/gz7R7eg== + +validator@^13.7.0: + version "13.9.0" + resolved "https://registry.yarnpkg.com/validator/-/validator-13.9.0.tgz#33e7b85b604f3bbce9bb1a05d5c3e22e1c2ff855" + integrity sha512-B+dGG8U3fdtM0/aNK4/X8CXq/EcxU2WPrPEkJGslb47qyHsxmbggTWK0yEA4qnYVNF+nxNlN88o14hIcPmSIEA== + vary@~1.1.2: version "1.1.2" resolved "https://registry.yarnpkg.com/vary/-/vary-1.1.2.tgz#2299f02c6ded30d4a5961b0b9f74524a18f634fc" @@ -1458,6 +1572,13 @@ wide-align@^1.1.2, wide-align@^1.1.5: dependencies: string-width "^1.0.2 || 2 || 3 || 4" +wkx@^0.5.0: + version "0.5.0" + resolved "https://registry.yarnpkg.com/wkx/-/wkx-0.5.0.tgz#c6c37019acf40e517cc6b94657a25a3d4aa33e8c" + integrity sha512-Xng/d4Ichh8uN4l0FToV/258EjMGU9MGcA0HV2d9B/ZpZB3lqQm7nkOdZdm5GhKtLLhAE7PiVQwN4eN+2YJJUg== + dependencies: + "@types/node" "*" + wrappy@1: version "1.0.2" resolved "https://registry.yarnpkg.com/wrappy/-/wrappy-1.0.2.tgz#b5243d8f3ec1aa35f1364605bc0d1036e30ab69f" From ecdceb8452d7a56533dd26a0af99f16574f73ec7 Mon Sep 17 00:00:00 2001 From: "sajeda.akhter704@gmail.com" Date: Tue, 11 Apr 2023 10:22:16 +0600 Subject: [PATCH 6/6] 'sessionAuthenticationDone' --- App/models/userModel.js | 9 ++++---- index.js | 47 ++++++++++++++++++++++++---------------- users.sqlite | Bin 16384 -> 16384 bytes 3 files changed, 32 insertions(+), 24 deletions(-) diff --git a/App/models/userModel.js b/App/models/userModel.js index baddb79..760d117 100644 --- a/App/models/userModel.js +++ b/App/models/userModel.js @@ -4,14 +4,13 @@ module.exports = (sequelize, Sequelize) => { type: Sequelize.INTEGER, autoIncrement: true, primaryKey: true, - unique: true }, userName: { type: Sequelize.STRING, allowNull: false, - // validate: { - // len: [5, 10] - // } + validate: { + len: [3, 50] + } }, email: { @@ -22,10 +21,10 @@ module.exports = (sequelize, Sequelize) => { isEmail: true, } - }, password: { type: Sequelize.STRING, + }, confirmPassword:{ type: Sequelize.STRING, diff --git a/index.js b/index.js index ec30d5f..6a541cd 100644 --- a/index.js +++ b/index.js @@ -5,7 +5,6 @@ var session = require("express-session"); const app = express(); const db = require("./App/models/database"); const bcrypt = require("bcrypt"); -const { log } = require("console"); app.use(express.static("public")); app.use(bodyParser.urlencoded({ extended: true })); @@ -27,11 +26,24 @@ app.use( ); const todos = []; -let error; -function isLoggedIn(req, res, next) { +async function checkLoggedIn (req, res, next) { + console.log('checkLoggedIn',req.body) if (req.session.id && req.session.userId) { - next(); + const user = await db.users.findOne({ + where: { + id: req.session.userId, + }, + raw: true, + }); + if(user){ + next(); + } + else{ + res.redirect("/login"); + } + + } else { res.redirect("/login"); } @@ -44,29 +56,28 @@ app.get("/registration", (req, res) => { app.post("/registration", async (req, res) => { try { const { userName, email, password, confirmPassword } = req.body; - let user; + if (password === confirmPassword) { - - bcrypt.hash(password, 10,async function (err, hash) { - user = { + const hashedPassword = await bcrypt.hash(password, 10); + const user = { userName: userName, email: email, - password: hash, + password: hashedPassword, }; + await db.users.create(user); - res.status(200); - res.redirect("/login"); - }); - + res.status(200).redirect("/login"); + } + else { + throw new Error('password and confirmPassword not matched.'); } } catch (e) { - console.log('===============error=============',e) - res.status(500).json(e.message); + res.status(500); } }); -app.get("/", isLoggedIn, (req, res) => { +app.get("/", checkLoggedIn, (req, res) => { res.render("pages/index", { todos }); }); @@ -87,11 +98,9 @@ app.post("/login", async (req, res) => { if (isExistUser) { bcrypt.compare(password, isExistUser.password, function (err, result) { - if (result === true) { - + if (result) { res.status(200); req.session.userId = isExistUser.id; - console.log(' req.session.userId=====', req.session.userId, req.session); res.redirect("/"); } else { diff --git a/users.sqlite b/users.sqlite index ca0a60da5d42c71904a9ac503a2cf2571b57ab08..388136036fb31ac9c3570ea33d2d74cf920b0152 100644 GIT binary patch delta 1029 zcmZ|O&r;ez9Ki9!NN4CEsdhS!qZAI@9M|2Dgd~n*1))Ou6XZ`IC$UKgkTeAvO7IYP zg0}C_YY!bBp!c5o3?6#v*?XyW6hSU~`OfTocK5fV5k4B>pSSVgmz_8s{C@tG^m}|) z7XTN;z_=U%dZSIaeSSqEfhGE?-uNr2(X@Iwu8Ky462!=bd;^ecX z0e70Rx^!MQ^x=u6=DSQa*T{Z|3{*)fL}kR**nY#zIj~VK=P3$Okv&N7ArykNfGB~2 zfMMy-E`$PvH`QI5$Dvg(wsmv^>(yJTQombmjzI`57Q8UX)C#@xHHCBmPyvk{XH~fheH$|&6V0mY#HII`eCCy}5 zl50z`nNAG^f4>P`n?9KfeUehF~_=+t!%P)u5pWTZ9>G5YSJ-v*-6?P(ti zEUdLyI)MtZsPrWxR+O3v*w)R5K*R|&4;aXXz|1WEf98mApKt3LhG95)jgi-rb@+!2 zcyVGAw|i>0=Xp9!9`X_+FUe2jH}WU>d$JyClJ0Y27oYn{o&a%#FP-7l9pZ4V-k+S< WCJr9P`?j)0@ZQ;N|D`9lpZ70qL>!g? delta 829 zcma)5O-{l<6rTE9Q7M&R19rjI7$(!1X=`U?;)X6r+_{p_2&hEF(6DZg0O1NIt_fH0 z03O0CxOYZcX=`JA=}TVv<@>((=FNnm6o%6DM5@j7OR2W#yeLIb0zi3HZj^HItvFd8 zmEI4k{eB-KOd5zb3{*$wjPlQb)SfSNjYPFoKSPKi)ZCMS!ozhrEz7f+X+vxYUJ%tk zJUQlhJTiWi5eJ&46M6iNws&PvIjvM&cW^&?5_xeq^d27J?PzFVLPcT}qy{1kJJ6;y z-ynmwcm89L(#MP-h)iRxA0*E+1Glzipq5qDb;og}N5+Uz1RXQK-z?hN%8^UW&lwm& zY~&ws7Hw|Es}kL%J2MYA3=u{MG?ASn^&7pGWKh+_ z)_rf#>v}!0@@LQYMJ9zUW?H<*c}LgusD_l7k;x(8A>P2}Qmua|v#UaJUz+&6v9A@r Jq1Er{@fSuuv|<1N