forked from iampavangandhi/TradeByte
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Custom Sign In and Sign Up implemented using iampavangandhi#46
- Loading branch information
1 parent
c9a6635
commit b1c9bc9
Showing
7 changed files
with
204 additions
and
17 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
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,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; |
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,103 @@ | ||
<% if(typeof errors != "undefined") { %> | ||
<% errors.forEach(function(error) { %> | ||
<div | ||
class="w-full flex items-center justify-between bg-yellow-200 text-yellow-700 border border-yellow-600 p-2 rounded-lg mt-1"> | ||
<div class="item flex items-center"> | ||
<div slot="avatar"> | ||
<svg xmlns="http://www.w3.org/2000/svg" width="100%" height="100%" fill="none" viewBox="0 0 24 24" | ||
stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" | ||
class="feather feather-alert-octagon w-5 h-5 mx-2"> | ||
<polygon points="7.86 2 16.14 2 22 7.86 22 16.14 16.14 22 7.86 22 2 16.14 2 7.86 7.86 2"></polygon> | ||
<line x1="12" y1="8" x2="12" y2="12"></line> | ||
<line x1="12" y1="16" x2="12.01" y2="16"></line> | ||
</svg> | ||
</div> | ||
<p class=""><%= error.msg %> </p> | ||
</div> | ||
<div onClick="this.parentNode.remove()"> | ||
<svg xmlns="http://www.w3.org/2000/svg" width="100%" height="100%" fill="none" viewBox="0 0 24 24" | ||
stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" | ||
class="feather feather-x cursor-pointer hover:text-yellow-500 rounded-full w-5 h-5 ml-2"> | ||
<line x1="18" y1="6" x2="6" y2="18"></line> | ||
<line x1="6" y1="6" x2="18" y2="18"></line> | ||
</svg> | ||
</div> | ||
</div> | ||
<% }); %> | ||
<% } %> | ||
|
||
<% if(success_msg != "") { %> | ||
<div | ||
class="w-full flex items-center justify-between bg-green-200 text-green-700 border border-green-600 p-2 rounded-lg mt-1"> | ||
<div class="item flex items-center"> | ||
<div slot="avatar"> | ||
<svg xmlns="http://www.w3.org/2000/svg" width="100%" height="100%" fill="none" viewBox="0 0 24 24" | ||
stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" | ||
class="feather feather-alert-octagon w-5 h-5 mx-2"> | ||
<polygon points="7.86 2 16.14 2 22 7.86 22 16.14 16.14 22 7.86 22 2 16.14 2 7.86 7.86 2"></polygon> | ||
<line x1="12" y1="8" x2="12" y2="12"></line> | ||
<line x1="12" y1="16" x2="12.01" y2="16"></line> | ||
</svg> | ||
</div> | ||
<p class=""><%= success_msg %> </p> | ||
</div> | ||
<div onClick="this.parentNode.remove()"> | ||
<svg xmlns="http://www.w3.org/2000/svg" width="100%" height="100%" fill="none" viewBox="0 0 24 24" | ||
stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" | ||
class="feather feather-x cursor-pointer hover:text-green-500 rounded-full w-5 h-5 ml-2"> | ||
<line x1="18" y1="6" x2="6" y2="18"></line> | ||
<line x1="6" y1="6" x2="18" y2="18"></line> | ||
</svg> | ||
</div> | ||
</div> | ||
<% } %> | ||
|
||
<% if(error_msg != "") { %> | ||
<div class="w-full flex items-center justify-between bg-red-200 text-red-700 border border-red-600 p-2 rounded-lg mt-1"> | ||
<div class="item flex items-center"> | ||
<div slot="avatar"> | ||
<svg xmlns="http://www.w3.org/2000/svg" width="100%" height="100%" fill="none" viewBox="0 0 24 24" | ||
stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" | ||
class="feather feather-alert-octagon w-5 h-5 mx-2"> | ||
<polygon points="7.86 2 16.14 2 22 7.86 22 16.14 16.14 22 7.86 22 2 16.14 2 7.86 7.86 2"></polygon> | ||
<line x1="12" y1="8" x2="12" y2="12"></line> | ||
<line x1="12" y1="16" x2="12.01" y2="16"></line> | ||
</svg> | ||
</div> | ||
<p class=""><%= error_msg %> </p> | ||
</div> | ||
<div onClick="this.parentNode.remove()"> | ||
<svg xmlns="http://www.w3.org/2000/svg" width="100%" height="100%" fill="none" viewBox="0 0 24 24" | ||
stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" | ||
class="feather feather-x cursor-pointer hover:text-red-500 rounded-full w-5 h-5 ml-2"> | ||
<line x1="18" y1="6" x2="6" y2="18"></line> | ||
<line x1="6" y1="6" x2="18" y2="18"></line> | ||
</svg> | ||
</div> | ||
</div> | ||
<% } %> | ||
|
||
<% if(error != "") { %> | ||
<div class="w-full flex items-center justify-between bg-red-200 text-red-700 border border-red-600 p-2 rounded-lg mt-1"> | ||
<div class="item flex items-center"> | ||
<div slot="avatar"> | ||
<svg xmlns="http://www.w3.org/2000/svg" width="100%" height="100%" fill="none" viewBox="0 0 24 24" | ||
stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" | ||
class="feather feather-alert-octagon w-5 h-5 mx-2"> | ||
<polygon points="7.86 2 16.14 2 22 7.86 22 16.14 16.14 22 7.86 22 2 16.14 2 7.86 7.86 2"></polygon> | ||
<line x1="12" y1="8" x2="12" y2="12"></line> | ||
<line x1="12" y1="16" x2="12.01" y2="16"></line> | ||
</svg> | ||
</div> | ||
<p class=""><%= error %> </p> | ||
</div> | ||
<div onClick="this.parentNode.remove()"> | ||
<svg xmlns="http://www.w3.org/2000/svg" width="100%" height="100%" fill="none" viewBox="0 0 24 24" | ||
stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" | ||
class="feather feather-x cursor-pointer hover:text-red-500 rounded-full w-5 h-5 ml-2"> | ||
<line x1="18" y1="6" x2="6" y2="18"></line> | ||
<line x1="6" y1="6" x2="18" y2="18"></line> | ||
</svg> | ||
</div> | ||
</div> | ||
<% } %> |
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