From b1c9bc94e3ed008f9e72a9dc018b5fa342654b5e Mon Sep 17 00:00:00 2001 From: fabcodingzest <48706652+fabcodingzest@users.noreply.github.com> Date: Fri, 21 Aug 2020 19:57:23 +0530 Subject: [PATCH] Custom Sign In and Sign Up implemented using #46 --- app.js | 8 +++ middleware/auth.js | 2 +- routes/api/auth.js | 8 +++ routes/api/user.js | 69 +++++++++++++++++++++-- views/login.ejs | 5 +- views/partials/_messages.ejs | 103 +++++++++++++++++++++++++++++++++++ views/signup.ejs | 26 ++++++--- 7 files changed, 204 insertions(+), 17 deletions(-) create mode 100644 views/partials/_messages.ejs diff --git a/app.js b/app.js index 8c2735f..d43a27d 100644 --- a/app.js +++ b/app.js @@ -16,12 +16,14 @@ const session = require("express-session"); const mongoose = require("mongoose"); const MongoStore = require("connect-mongo")(session); const connectDB = require("./config/db"); +const flash = require('connect-flash'); // Load config dotenv.config({ path: "./config/config.env" }); // Passport config require("./config/passport")(passport); +require("./config/passportLocal")(passport); // DB Connected connectDB(); @@ -75,9 +77,15 @@ app.use( app.use(passport.initialize()); app.use(passport.session()); +// Connect Flash +app.use(flash()) + // Set Global variables app.use(function (req, res, next) { res.locals.user = req.user || null; + res.locals.success_msg = req.flash('success_msg'); + res.locals.error_msg = req.flash('error_msg'); + res.locals.error = req.flash('error'); next(); }); diff --git a/middleware/auth.js b/middleware/auth.js index a7fae58..bb38a58 100644 --- a/middleware/auth.js +++ b/middleware/auth.js @@ -5,7 +5,7 @@ module.exports = { if (req.isAuthenticated()) { return next(); } else { - req.flash('error_msg', 'Please log in to view that resource'); + req.flash('error_msg', 'Password or Email does not match'); res.redirect('/'); } }, diff --git a/routes/api/auth.js b/routes/api/auth.js index 971fdbd..f5a41a7 100644 --- a/routes/api/auth.js +++ b/routes/api/auth.js @@ -21,6 +21,14 @@ router.get( } ); +router.post('/signin', (req, res, next) => { + passport.authenticate('local', { + successRedirect: '/portfolio', + failureRedirect: '/', + failureFlash: true + })(req, res, next) +}) + // @desc Logout user // @route /auth/logout router.get("/logout", (req, res) => { diff --git a/routes/api/user.js b/routes/api/user.js index 4f83b16..adaa9c4 100644 --- a/routes/api/user.js +++ b/routes/api/user.js @@ -1,14 +1,73 @@ const express = require("express"); const router = express.Router(); +const passport = require("passport") +const bcrypt = require("bcryptjs"); +const {v4: uuidv4} = require("uuid"); +const { ensureGuest } = require("../../middleware/auth"); -router.get('/signup', (req, res) => { +// Load User Model +const User = require("../../models/User"); + +// Sign Up Page +router.get('/signup', ensureGuest, (req, res) => { res.status(200).render('signup', { layout: 'layouts/login' }) }) + +// Submit Sign Up Form router.post('/signup', (req, res) => { - - console.log('signup') + const { firstName, lastName, password1, password2, email } = req.body; + let errors = []; + + if (!firstName || !lastName || !password1 || !password2 || !email) { + errors.push({ msg: 'Please enter all fields' }); + } + if (password1 !== password2) { + errors.push({ msg: 'Passwords do not match' }) + } + if (password1.length < 6) { + errors.push({ msg: 'Password must be longer than 6 characters' }) + } + + if (errors.length > 0) { + res.render('signup', { layout: 'layouts/login', errors, firstName, lastName, password1, password2 }) + } else { + User.findOne({ email: email }).then((user) => { + if (user) { + errors.push({ msg: 'Email already exists' }) + res.render('signup', { layout: 'layouts/login', errors, firstName, lastName, password1, password2 }) + } else { + const newUser = new User({ + googleId: uuidv4(), + displayName: `${firstName} ${lastName}`, + firstName, + lastName, + email, + image: 'https://t3.ftcdn.net/jpg/00/64/67/52/240_F_64675209_7ve2XQANuzuHjMZXP3aIYIpsDKEbF5dD.jpg', + password: password1, + balance: 10000, + }) + + bcrypt.genSalt(10, (err, salt) => { + bcrypt.hash(newUser.password, salt, (err, hash) => { + if (err) throw err; + newUser.password = hash; + newUser.save().then(user => { + req.flash('success_msg', 'You are now registered and can log in') + res.status(200).redirect('/') + }).catch((err) => console.log(err)) + }) + }) + } + }) + } }) -router.post('/signin', (req, res) => { - + +router.post('/signin', (req, res, next) => { + passport.authenticate('local', { + successRedirect: '/portfolio', + failureRedirect: '/', + failureFlash: true, + })(req, res, next) }) + module.exports = router; \ No newline at end of file diff --git a/views/login.ejs b/views/login.ejs index 21d6dc5..e7f92af 100644 --- a/views/login.ejs +++ b/views/login.ejs @@ -10,6 +10,7 @@ src="/images/google.png">Sign in with Google + <%- include('./partials/_messages') %>
@@ -17,13 +18,13 @@
-
-
diff --git a/views/partials/_messages.ejs b/views/partials/_messages.ejs new file mode 100644 index 0000000..e260cb9 --- /dev/null +++ b/views/partials/_messages.ejs @@ -0,0 +1,103 @@ +<% if(typeof errors != "undefined") { %> +<% errors.forEach(function(error) { %> +
+
+
+ + + + + +
+

<%= error.msg %>

+
+
+ + + + +
+
+<% }); %> +<% } %> + +<% if(success_msg != "") { %> +
+
+
+ + + + + +
+

<%= success_msg %>

+
+
+ + + + +
+
+<% } %> + +<% if(error_msg != "") { %> +
+
+
+ + + + + +
+

<%= error_msg %>

+
+
+ + + + +
+
+<% } %> + +<% if(error != "") { %> +
+
+
+ + + + + +
+

<%= error %>

+
+
+ + + + +
+
+<% } %> \ No newline at end of file diff --git a/views/signup.ejs b/views/signup.ejs index 2ea3561..7434d8b 100644 --- a/views/signup.ejs +++ b/views/signup.ejs @@ -1,41 +1,49 @@

TradeByte

-
- Sign Up for  TradeByte +
+

Sign Up for  TradeByte

+ <%- include('./partials/_messages') %>
- + name="firstName" placeholder="First Name" style="transition: all 0.15s ease 0s;">
-
-
- +
+
+ +
+ class="bg-gray-900 text-white active:bg-gray-700 text-sm font-bold uppercase px-6 py-3 rounded shadow hover:shadow-lg outline-none focus:outline-none mr-1 mb-1 w-full" + type="submit" style="transition: all 0.15s ease 0s;">Sign Up

Go back to Login