-
Notifications
You must be signed in to change notification settings - Fork 80
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(login): add support for passport-local login
- Loading branch information
1 parent
7b54db2
commit 912524b
Showing
5 changed files
with
147 additions
and
35 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
{ | ||
// Use IntelliSense to learn about possible attributes. | ||
// Hover to view descriptions of existing attributes. | ||
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 | ||
"version": "0.2.0", | ||
"configurations": [ | ||
{ | ||
"type": "node", | ||
"request": "attach", | ||
"name": "Attach by Process ID", | ||
"processId": "${command:PickProcess}" | ||
}, | ||
] | ||
} |
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,50 +1,111 @@ | ||
import { Request, Response, NextFunction } from "express"; | ||
import { validationResult } from "express-validator"; | ||
import { User } from "../models/User"; | ||
import passport from 'passport'; | ||
import '../handlers/passport'; | ||
|
||
|
||
|
||
export const login = (req: Request, res: Response) => { | ||
res.render('login', { title: 'Login' }) | ||
res.render('login', { title: 'Login' }) | ||
} | ||
|
||
export const loginForm = async (req: Request, res: Response) => { | ||
try { | ||
const errors = validationResult(req); | ||
|
||
if (!errors.isEmpty()) { | ||
//@ts-ignore | ||
req.flash('error', errors.array().map(err => err.msg)); | ||
res.render('login', { | ||
title: 'Login', | ||
body: req.body, | ||
flashes: req.flash() | ||
}); | ||
} else { | ||
if (!req.body.email || !req.body.password) { | ||
req.flash('error', 'Something is wrong with your input') | ||
} | ||
await passport.authenticate('local', (err:any, user:any) => { | ||
if(err || !user) { | ||
req.flash("error", "Invalid Email address or Password") | ||
res.redirect('/login') | ||
return; | ||
|
||
} | ||
req.login(user, (err) => { | ||
if(err){ | ||
console.log('hello'); | ||
return req.flash('error', 'Authentication failed!'); | ||
} else{ | ||
req.flash("success", "Login Successful!"); | ||
res.redirect('/contact'); | ||
} | ||
|
||
return | ||
|
||
}) | ||
})(req, res) | ||
} | ||
|
||
} catch (e) { | ||
|
||
res.redirect('/register'); | ||
throw new Error(e) | ||
} | ||
|
||
} | ||
|
||
exports.isLoggedIn = (req: Request, res: Response, next: NextFunction) => { | ||
// check if the user is authenticated | ||
if (req.isAuthenticated()) { | ||
next(); // carry on to the login | ||
return; | ||
} else { | ||
req.flash('error', 'Oops, you must be logged in to do that!'); | ||
res.redirect('/login'); | ||
} | ||
} | ||
export const register = (req: Request, res: Response) => { | ||
res.render('register', { title: 'Register' }) | ||
} | ||
export const registerForm = async (req: Request, res: Response, next: NextFunction ) => { | ||
} | ||
|
||
export const registerForm = async (req: Request, res: Response, next: NextFunction) => { | ||
|
||
try { | ||
const errors = validationResult(req); | ||
|
||
if (!errors.isEmpty()) { | ||
//@ts-ignore | ||
req.flash('error', errors.array().map(err => err.msg)); | ||
res.render('register', { | ||
title: 'Register', | ||
body: req.body, | ||
flashes: req.flash() | ||
}); | ||
}else { | ||
const user = new User({ email: req.body.email }); | ||
await User.register(user, req.body.password, function(err, user){ | ||
|
||
if(err){ | ||
req.flash(`error`, `${err.message}`); | ||
res.redirect('/register') | ||
}else{ | ||
req.flash(`success`, `${req.body.email} has been registered! Please Login`); | ||
res.redirect('/login') | ||
} | ||
}) | ||
} | ||
|
||
const errors = validationResult(req); | ||
|
||
if (!errors.isEmpty()) { | ||
//@ts-ignore | ||
req.flash('error', errors.array().map(err => err.msg)); | ||
res.render('register', { | ||
title: 'Register', | ||
body: req.body, | ||
flashes: req.flash() | ||
}); | ||
} else { | ||
const user = new User({ email: req.body.email }); | ||
await User.register(user, req.body.password, function (err, user) { | ||
|
||
if (err) { | ||
req.flash(`error`, `${err.message}`); | ||
res.redirect('/register') | ||
} else { | ||
passport.authenticate('local', { | ||
session: true | ||
})(req, res, () => { | ||
req.flash(`success`, `${req.body.email} has been registered! Please Login`); | ||
res.redirect('/login') | ||
}) | ||
|
||
} | ||
}) | ||
} | ||
|
||
} catch (e) { | ||
res.redirect('/register'); | ||
throw new Error(e) | ||
|
||
res.redirect('/register'); | ||
throw new Error(e) | ||
} | ||
|
||
} | ||
|
||
|
||
} |
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,16 @@ | ||
import passport from 'passport'; | ||
|
||
|
||
import { User } from "../models/User"; | ||
import passportLocal from 'passport-local'; | ||
const LocalStrategy = passportLocal.Strategy; | ||
|
||
|
||
passport.use(new LocalStrategy({ | ||
usernameField: 'email', | ||
passwordField: 'password' | ||
}, | ||
User.authenticate())); | ||
|
||
passport.serializeUser(User.serializeUser()); | ||
passport.deserializeUser(User.deserializeUser()); |
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