-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Custom Sign In and Sign Up Implemented using passport.js package. Fixes #46 (Task 1)
- Loading branch information
Showing
15 changed files
with
377 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
const LocalStrategy = require('passport-local').Strategy; | ||
const bcrypt = require('bcryptjs'); | ||
|
||
// Load User model | ||
const User = require('../models/User'); | ||
|
||
module.exports = function (passport) { | ||
passport.use( | ||
new LocalStrategy({ usernameField: 'email' }, (email, password, done) => { | ||
// Match User | ||
User.findOne({ | ||
email: email | ||
}).then(user => { | ||
if (!user) { | ||
return done(null, false, { message: 'That email is not registered' }) | ||
} | ||
|
||
// Match user Password | ||
bcrypt.compare(password, user.password, (err, isMatch) => { | ||
if (err) throw err; | ||
if (isMatch) { | ||
done(null, user); | ||
} else { | ||
return done(null, false, { message: 'That password is incorrect' }) | ||
} | ||
}) | ||
}) | ||
}) | ||
) | ||
|
||
passport.serializeUser((user, done) => { | ||
done(null, user.id); | ||
}); | ||
|
||
passport.deserializeUser((id, done) => { | ||
User.findById(id, (err, user) => { | ||
done(err, user); | ||
}); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
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"); | ||
|
||
// Load User Model | ||
const User = require("../../models/User"); | ||
|
||
// @desc Sign Up Page | ||
// @route GET /user/signup | ||
// @access Public | ||
router.get('/signup', ensureGuest, (req, res) => { | ||
res.status(200).render('signup', { layout: 'layouts/login' }) | ||
}) | ||
|
||
// @desc Submit Sign Up Form | ||
// @route GET /user/signup | ||
router.post('/signup', (req, res) => { | ||
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)) | ||
}) | ||
}) | ||
} | ||
}) | ||
} | ||
}) | ||
|
||
// @desc Submit Sign In Form | ||
// @route GET /user/signin | ||
router.post('/signin', (req, res, next) => { | ||
passport.authenticate('local', { | ||
successRedirect: '/portfolio', | ||
failureRedirect: '/', | ||
failureFlash: true, | ||
})(req, res, next) | ||
}) | ||
|
||
module.exports = router; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,43 @@ | ||
<h3>**Login/Landing Page**</h3> | ||
|
||
<br /> | ||
|
||
<a href="/auth/google" class="" style="margin-top: 5px;"> | ||
<i class="fab fa-google"></i> Login With Google | ||
</a> | ||
<section class="p-1 min-w"> | ||
<h2 class="text-blue-200 text-center text-4xl lg:text-6xl font-semibold">TradeByte</h2> | ||
<div class="flex flex-col mb-6 shadow-lg rounded-lg bg-gray-300"> | ||
<div class="rounded-t mb-0 px-6 pt-6 pb-2"> | ||
<div class="btn-wrapper text-center"> | ||
<a href="/auth/google" class="w-full"> | ||
<button | ||
class="bg-white active:bg-gray-100 text-gray-800 font-normal px-4 py-2 rounded outline-none focus:outline-none mr-1 mb-1 uppercase shadow hover:shadow-md inline-flex items-center font-bold text-xs justify-center w-full" | ||
type="button" style="transition: all 0.15s ease 0s;"><img alt="..." class="w-5 mr-1" | ||
src="/images/google.png">Sign in with Google</button> | ||
</a> | ||
</div> | ||
<%- include('./partials/_messages') %> | ||
<hr class="mt-6 border-b-1 border-gray-400"> | ||
</div> | ||
<div class="flex-auto px-4 lg:px-10 pb-10 pt-0"> | ||
<div class="text-gray-500 text-center mb-3 font-bold"><small>Or sign in with credentials</small></div> | ||
<form action="/user/signin" method="POST"> | ||
<div class="relative w-full mb-3"> | ||
<label class="block uppercase text-gray-700 text-xs font-bold mb-2" for="grid-password">Email</label> | ||
<input type="email" name="email" | ||
class="px-3 py-3 placeholder-gray-400 text-gray-700 bg-white rounded text-sm shadow focus:outline-none focus:shadow-outline w-full" | ||
placeholder="Email" style="transition: all 0.15s ease 0s;"> | ||
</div> | ||
<div class="relative w-full mb-3"> | ||
<label class="block uppercase text-gray-700 text-xs font-bold mb-2" for="grid-password">Password</label> | ||
<input type="password" name="password" | ||
class="px-3 py-3 placeholder-gray-400 text-gray-700 bg-white rounded text-sm shadow focus:outline-none focus:shadow-outline w-full" | ||
placeholder="Password" style="transition: all 0.15s ease 0s;"> | ||
</div> | ||
<div class="text-center mt-6"> | ||
<button | ||
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 In</button> | ||
</div> | ||
</form> | ||
<a href="/user/signup"><button | ||
class="bg-red-900 text-white active:bg-red-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="button" style="transition: all 0.15s ease 0s;">Sign Up</button> | ||
</a> | ||
</div> | ||
</div> | ||
</section> |
Oops, something went wrong.