diff options
Diffstat (limited to 'src/server/authentication/controllers/user.ts')
-rw-r--r-- | src/server/authentication/controllers/user.ts | 76 |
1 files changed, 59 insertions, 17 deletions
diff --git a/src/server/authentication/controllers/user.ts b/src/server/authentication/controllers/user.ts index f74ff9039..a496959d1 100644 --- a/src/server/authentication/controllers/user.ts +++ b/src/server/authentication/controllers/user.ts @@ -9,15 +9,30 @@ import * as session from "express-session"; import * as pug from 'pug'; /** + * GET / + * Whenever a user navigates to the root of Dash + * (doesn't specify a sub-route), redirect to login. + * If the user is already signed in, it will effectively + * automatically redirect them to /home instead + */ +export let getEntry = (req: Request, res: Response) => { + res.redirect("/login"); +} + +/** * GET /signup - * Signup page. + * Directs user to the signup page + * modeled by signup.pug in views */ export let getSignup = (req: Request, res: Response) => { if (req.user) { - return res.redirect("/"); + let user = req.user; + return res.redirect("/home"); } res.render("signup.pug", { - title: "Sign Up" + title: "Sign Up", + user: req.user, + errors: req.flash("Unable to facilitate sign up. Please try again.") }); }; @@ -31,21 +46,33 @@ export let postSignup = (req: Request, res: Response, next: NextFunction) => { req.assert("confirmPassword", "Passwords do not match").equals(req.body.password); req.sanitize("email").normalizeEmail({ gmail_remove_dots: false }); + req.flash("Working on something!!!"); + const errors = req.validationErrors(); if (errors) { - req.flash("errors", "Unable to facilitate sign up. Please try again."); + res.render("signup.pug", { + title: "Sign Up", + errors: req.flash("Unable to facilitate sign up. Please try again.") + }); return res.redirect("/signup"); } + const email = req.body.email; + const password = req.body.password; + const user = new User({ - email: req.body.email, - password: req.body.password + email, + password, + userDoc: "document here" }); - User.findOne({ email: req.body.email }, (err, existingUser) => { + User.findOne({ email }, (err, existingUser) => { if (err) { return next(err); } if (existingUser) { + if (existingUser) { + // existingUser.update({ $set: { email: please_work } }, (err, res) => { }); + } req.flash("errors", "Account with that email address already exists."); return res.redirect("/signup"); } @@ -59,6 +86,7 @@ export let postSignup = (req: Request, res: Response, next: NextFunction) => { }); }); }); + }; @@ -68,17 +96,18 @@ export let postSignup = (req: Request, res: Response, next: NextFunction) => { */ export let getLogin = (req: Request, res: Response) => { if (req.user) { - return res.redirect("/"); + return res.redirect("/home"); } - res.send("<p>dear lord please render</p>"); - // res.render("account/login", { - // title: "Login" - // }); + res.render("login.pug", { + title: "Log In", + user: req.user + }); }; /** * POST /login * Sign in using email and password. + * On failure, redirect to login page */ export let postLogin = (req: Request, res: Response, next: NextFunction) => { req.assert("email", "Email is not valid").isEmail(); @@ -89,19 +118,32 @@ export let postLogin = (req: Request, res: Response, next: NextFunction) => { if (errors) { req.flash("errors", "Unable to login at this time. Please try again."); - return res.redirect("/login"); + return res.redirect("/signup"); } passport.authenticate("local", (err: Error, user: UserModel, info: IVerifyOptions) => { if (err) { return next(err); } if (!user) { - req.flash("errors", info.message); - return res.redirect("/login"); + return res.redirect("/signup"); } req.logIn(user, (err) => { if (err) { return next(err); } req.flash("success", "Success! You are logged in."); - res.redirect("/"); + res.redirect("/home"); }); })(req, res, next); -};
\ No newline at end of file +}; + +/** + * GET /logout + * Invokes the logout function on the request + * and destroys the user's current session. + */ +export let getLogout = (req: Request, res: Response) => { + req.logout(); + const sess = req.session; + if (sess) { + sess.destroy((err) => { if (err) { console.log(err); } }); + } + res.redirect('/login'); +}
\ No newline at end of file |