aboutsummaryrefslogtreecommitdiff
path: root/src/server/authentication/controllers/user.ts
blob: 1ce82a911f6277896d8bb56f9ad02ba308d6c114 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
import { default as User, UserModel, AuthToken } from "../models/User";
import { Request, Response, NextFunction } from "express";
import * as passport from "passport";
import { IVerifyOptions } from "passport-local";
import "../config/passport";
const flash = require("express-flash");

/**
 * GET /
 * Whenever a user navigates to the root of Dash
 * (doesn't specify a sub-route), redirect to login.
 */
export let getEntry = (req: Request, res: Response) => {
    res.redirect("/login");
}

/**
 * GET /signup
 * Directs user to the signup page
 * modeled by signup.pug in views
 */
export let getSignup = (req: Request, res: Response) => {
    if (req.user) {
        let user = req.user;
        return res.redirect("/home");
    }
    res.render("signup.pug", {
        title: "Sign Up",
        errors: req.flash("Unable to facilitate sign up. Please try again.")
    });
};

/**
 * POST /signup
 * Create a new local account.
 */
export let postSignup = (req: Request, res: Response, next: NextFunction) => {
    req.assert("email", "Email is not valid").isEmail();
    req.assert("password", "Password must be at least 4 characters long").len({ min: 4 });
    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) {
        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,
        password,
    });
    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");
        }
        user.save((err) => {
            if (err) { return next(err); }
            req.logIn(user, (err) => {
                if (err) {
                    return next(err);
                }
                res.redirect("/");
            });
        });
    });

};


/**
 * GET /login
 * Login page.
 */
export let getLogin = (req: Request, res: Response) => {
    if (req.user) {
        return res.redirect("/home");
    }
    res.render("login.pug", {
        title: "Log In"
    });
};

/**
 * 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();
    req.assert("password", "Password cannot be blank").notEmpty();
    req.sanitize("email").normalizeEmail({ gmail_remove_dots: false });

    const errors = req.validationErrors();

    if (errors) {
        req.flash("errors", "Unable to login at this time. Please try again.");
        return res.redirect("/signup");
    }

    passport.authenticate("local", (err: Error, user: UserModel, info: IVerifyOptions) => {
        if (err) { return next(err); }
        if (!user) {
            return res.redirect("/signup");
        }
        req.logIn(user, (err) => {
            if (err) { return next(err); }
            req.flash("success", "Success! You are logged in.");
            res.redirect("/home");
        });
    })(req, res, next);
};

/**
 * 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');
}