|
1 | 1 | const User = require('../Models/User')
|
2 | 2 | const bcrypt = require('bcrypt')
|
3 |
| -// On any request req, req.session.user is now available where the userSession can be accesed |
4 |
| -// by User = require("../Models/User"); and then User.findOne to fetch the user from the database |
5 |
| - |
6 |
| -/** |
7 |
| - * Show the login page and check if the user is already logged in |
8 |
| - * @param {*} req |
9 |
| - * @param {*} res |
10 |
| - */ |
11 |
| -exports.login = (req, res) => { |
12 |
| - if(req.session.user) { |
13 |
| - res.redirect("/dashboard"); |
14 |
| - } else { |
15 |
| - res.render("auth/login"); |
16 |
| - } |
17 |
| -}; |
18 |
| - |
19 |
| - |
20 |
| - |
21 |
| -/** |
22 |
| - * Show the register page and check if the user is already logged in |
23 |
| - * @param {*} req |
24 |
| - * @param {*} res |
25 |
| - */ |
26 |
| -exports.register = (req, res) => { |
27 |
| - if(req.session.user) { |
28 |
| - res.redirect("/dashboard"); |
29 |
| - } else { |
30 |
| - res.render("auth/register"); |
31 |
| - } |
32 |
| -}; |
33 |
| - |
34 |
| - |
35 |
| -/** |
36 |
| - * Show the forgot password page |
37 |
| - * @param {*} req |
38 |
| - * @param {*} res |
39 |
| - */ |
40 |
| -exports.forgot = (req, res) => { |
41 |
| - if(req.session.user) { |
42 |
| - res.redirect("/dashboard"); |
43 |
| - } else { |
44 |
| - res.render("auth/forgot"); |
45 |
| - } |
46 |
| -}; |
47 |
| - |
48 |
| - |
49 |
| -/** |
50 |
| - * Authenticate the user and redirect to the dashboard |
51 |
| - * @param {*} req |
52 |
| - * @param {*} res |
53 |
| - * @returns |
54 |
| - */ |
55 |
| -exports.authenticate = async (req, res) => { |
56 |
| - const { username, password } = req.body; |
57 |
| - |
58 |
| - if (!username || !password) return res.status(400).send("Please fill all the fields"); |
59 |
| - |
60 |
| - User |
61 |
| - .findOne({ username }) |
62 |
| - .then((user) => { |
63 |
| - if (!user) return res.status(400).send("User does not exist"); |
64 |
| - |
65 |
| - bcrypt |
66 |
| - .compare(password, user.password) |
67 |
| - .then((isMatch) => { |
68 |
| - if (isMatch) { |
69 |
| - req.session.user = user; |
70 |
| - res.redirect("/project"); |
71 |
| - } else { |
72 |
| - return res.status(400).send("Incorrect password"); |
73 |
| - } |
| 3 | + |
| 4 | +class AuthenticationController { |
| 5 | + /** |
| 6 | + * Show the login page and check if the user is already logged in |
| 7 | + * @param {*} req |
| 8 | + * @param {*} res |
| 9 | + */ |
| 10 | + async showLogin (req, res) { |
| 11 | + if (req.session.user) { |
| 12 | + res.redirect('/dashboard') |
| 13 | + } else { |
| 14 | + res.render('auth/login') |
| 15 | + } |
| 16 | + }; |
| 17 | + |
| 18 | + /** |
| 19 | + * Show the register page and check if the user is already logged in |
| 20 | + * @param {*} req |
| 21 | + * @param {*} res |
| 22 | + */ |
| 23 | + async showRegister (req, res) { |
| 24 | + if (req.session.user) { |
| 25 | + res.redirect('/dashboard') |
| 26 | + } else { |
| 27 | + res.render('auth/register') |
| 28 | + } |
| 29 | + }; |
| 30 | + |
| 31 | + /** |
| 32 | + * Show the forgot password page and check if the user is already logged in |
| 33 | + * @param {*} req |
| 34 | + * @param {*} res |
| 35 | + */ |
| 36 | + async showForgot (req, res) { |
| 37 | + if (req.session.user) { |
| 38 | + res.redirect('/dashboard') |
| 39 | + } else { |
| 40 | + res.render('auth/forgot') |
| 41 | + } |
| 42 | + }; |
| 43 | + |
| 44 | + /** |
| 45 | + * Authenticate the user and redirect to the dashboard |
| 46 | + * @param {*} req |
| 47 | + * @param {*} res |
| 48 | + * @returns |
| 49 | + */ |
| 50 | + async authenticate (req, res) { |
| 51 | + const { username, password } = req.body |
| 52 | + |
| 53 | + if (!username || !password) return res.status(400).send('Please fill all the fields') |
| 54 | + |
| 55 | + User |
| 56 | + .findOne({ |
| 57 | + username |
| 58 | + }) |
| 59 | + .then((user) => { |
| 60 | + if (!user) return res.status(400).send('User does not exist') |
| 61 | + |
| 62 | + bcrypt |
| 63 | + .compare(password, user.password) |
| 64 | + .then((isMatch) => { |
| 65 | + if (isMatch) { |
| 66 | + req.session.user = user |
| 67 | + res.redirect('/project') |
| 68 | + } else { |
| 69 | + return res.status(400).send('Incorrect password') |
| 70 | + } |
| 71 | + }) |
| 72 | + .catch((err) => console.log(err)) |
| 73 | + }) |
| 74 | + }; |
| 75 | + |
| 76 | + /** |
| 77 | + * Create a new user and redirect to the dashboard |
| 78 | + * @param {*} req |
| 79 | + * @param {*} res |
| 80 | + * @returns |
| 81 | + */ |
| 82 | + async signup (req, res) { |
| 83 | + const { |
| 84 | + name, |
| 85 | + username, |
| 86 | + email, |
| 87 | + password, |
| 88 | + passwordConfirmation |
| 89 | + } = req.body |
| 90 | + |
| 91 | + if (!name || !username || !email || !password || !passwordConfirmation) { return res.status(400).send('Please fill all the fields') } |
| 92 | + |
| 93 | + if (password !== passwordConfirmation) { return res.status(400).send('Passwords do not match') } |
| 94 | + |
| 95 | + if (password.length < 8) { return res.status(400).send('Password must be at least 6 characters') } |
| 96 | + |
| 97 | + User.findOne({ |
| 98 | + username |
| 99 | + }).then((user) => { |
| 100 | + if (user) return res.status(400).send('User already exists') |
| 101 | + |
| 102 | + const newUser = new User({ name, username, email, password }) |
| 103 | + |
| 104 | + bcrypt.genSalt(10, (salt) => { |
| 105 | + bcrypt.hash(newUser.password, salt, (err, hash) => { |
| 106 | + if (err) console.log(err) |
| 107 | + newUser.password = hash |
| 108 | + newUser |
| 109 | + .save() |
| 110 | + .then((user) => { |
| 111 | + // req.session.user = user; |
| 112 | + // res.json(user); |
| 113 | + res.redirect('login') |
| 114 | + }) |
| 115 | + .catch((err) => console.log(err)) |
| 116 | + }) |
| 117 | + }) |
74 | 118 | })
|
75 |
| - .catch((err) => console.log(err)); |
76 |
| - }); |
77 |
| - }; |
78 |
| - |
79 |
| -/** |
80 |
| - * Create a new user and redirect to the dashboard |
81 |
| - * @param {*} req |
82 |
| - * @param {*} res |
83 |
| - * @returns |
84 |
| - */ |
85 |
| -exports.signup = (req, res) => { |
86 |
| - const { name, username, email, password, passwordConfirmation } = req.body; |
87 |
| - |
88 |
| - if (!name || !username || !email || !password || !passwordConfirmation) |
89 |
| - return res.status(400).send("Please fill all the fields"); |
90 |
| - |
91 |
| - if (password !== passwordConfirmation) |
92 |
| - return res.status(400).send("Passwords do not match"); |
93 |
| - |
94 |
| - if (password.length < 8) |
95 |
| - return res.status(400).send("Password must be at least 6 characters"); |
96 |
| - |
97 |
| - User.findOne({username}).then((user) => { |
98 |
| - if (user) return res.status(400).send("User already exists"); |
99 |
| - |
100 |
| - const newUser = new User({ |
101 |
| - name, |
102 |
| - username, |
103 |
| - email, |
104 |
| - password, |
105 |
| - }); |
106 |
| - |
107 |
| - bcrypt.genSalt(10, (err, salt) => { |
108 |
| - bcrypt.hash(newUser.password, salt, (err, hash) => { |
109 |
| - if (err) console.log(err); |
110 |
| - newUser.password = hash; |
111 |
| - newUser |
112 |
| - .save() |
113 |
| - .then((user) => { |
114 |
| - // req.session.user = user; |
115 |
| - // res.json(user); |
116 |
| - res.redirect("login"); |
117 |
| - }) |
118 |
| - .catch((err) => console.log(err)); |
119 |
| - }); |
120 |
| - }); |
121 |
| - }); |
122 |
| - }; |
123 |
| - |
124 |
| -/** |
125 |
| - * Logout the user and redirect to the login page |
126 |
| - * @param {*} req |
127 |
| - * @param {*} res |
128 |
| - */ |
129 |
| -exports.logout = (req, res) => { |
130 |
| - if(req.session) { |
131 |
| - req.session.destroy(() => { |
132 |
| - res.redirect("/login"); |
133 |
| - }); |
134 |
| - } else { |
135 |
| - res.redirect("/login"); |
136 |
| - } |
137 |
| -}; |
| 119 | + }; |
| 120 | + |
| 121 | + /** |
| 122 | + * Logout the user and redirect to the login page |
| 123 | + * @param {*} req |
| 124 | + * @param {*} res |
| 125 | + */ |
| 126 | + async logout (req, res) { |
| 127 | + if (req.session) { |
| 128 | + req.session.destroy(() => { |
| 129 | + res.redirect('/login') |
| 130 | + }) |
| 131 | + } else { |
| 132 | + res.redirect('/login') |
| 133 | + } |
| 134 | + }; |
| 135 | +} |
| 136 | + |
| 137 | +module.exports = new AuthenticationController() |
0 commit comments