|
1 |
| -const express = require("express") // CommonJS import style! |
| 1 | +const express = require("express"); // CommonJS import style! |
2 | 2 |
|
3 | 3 | // mongoose models for MongoDB data manipulation
|
4 |
| -const mongoose = require("mongoose") |
5 |
| -const User = require("../models/User.js") |
| 4 | +const mongoose = require("mongoose"); |
| 5 | +const User = require("../models/User.js"); |
6 | 6 |
|
7 | 7 | // a method that constains code to handle authentication-specific routes
|
8 | 8 | const authenticationRouter = () => {
|
9 | 9 | // create a new router that we can customize
|
10 |
| - const router = express.Router() |
| 10 | + const router = express.Router(); |
11 | 11 |
|
12 | 12 | // a route to handle user signup requests to /auth/signup
|
13 |
| - router.post("/signup", async (req, res) => { |
| 13 | + router.post("/signup", async (req, res, next) => { |
14 | 14 | // console.log(`Incoming signup data: ${JSON.stringify(req.body, null, 0)}`)
|
15 | 15 | // grab the username and password from the POST body
|
16 |
| - const username = req.body.username |
17 |
| - const password = req.body.password |
| 16 | + const username = req.body.username; |
| 17 | + const password = req.body.password; |
18 | 18 |
|
19 | 19 | if (!username || !password) {
|
20 | 20 | // no username or password received in the POST body... send an error
|
21 | 21 | res.status(401).json({
|
22 | 22 | success: false,
|
23 | 23 | message: `No username or password supplied.`,
|
24 |
| - }) |
| 24 | + }); |
| 25 | + next(); |
25 | 26 | }
|
26 | 27 |
|
27 | 28 | // try to create a new user
|
28 | 29 | try {
|
29 |
| - const user = await new User({ username, password }).save() |
| 30 | + const user = await new User({ username, password }).save(); |
30 | 31 | // user saved successfully... send a success response
|
31 |
| - console.error(`New user: ${user}`) |
32 |
| - const token = user.generateJWT() // generate a signed token |
| 32 | + console.error(`New user: ${user}`); |
| 33 | + const token = user.generateJWT(); // generate a signed token |
33 | 34 | res.json({
|
34 | 35 | success: true,
|
35 | 36 | message: "User saved successfully.",
|
36 | 37 | token: token,
|
37 | 38 | username: user.username,
|
38 |
| - }) // send the token to the client to store |
| 39 | + }); // send the token to the client to store |
| 40 | + next(); |
39 | 41 | } catch (err) {
|
40 | 42 | // error saving user to database... send an error response
|
41 |
| - console.error(`Failed to save user: ${err}`) |
| 43 | + console.error(`Failed to save user: ${err}`); |
42 | 44 | res.status(500).json({
|
43 | 45 | success: false,
|
44 | 46 | message: "Error saving user to database.",
|
45 | 47 | error: err,
|
46 |
| - }) |
| 48 | + }); |
| 49 | + next(); |
47 | 50 | }
|
48 |
| - }) |
| 51 | + }); |
49 | 52 |
|
50 | 53 | // a route to handle login attempts requested to /auth/login
|
51 |
| - router.post("/login", async function (req, res) { |
| 54 | + router.post("/login", async function (req, res, next) { |
52 | 55 | // grab the name and password that were submitted as POST body data
|
53 |
| - const username = req.body.username |
54 |
| - const password = req.body.password |
| 56 | + const username = req.body.username; |
| 57 | + const password = req.body.password; |
55 | 58 | // console.log(`${username}, ${password}`)
|
56 | 59 |
|
57 | 60 | if (!username || !password) {
|
58 | 61 | // no username or password received in the POST body... send an error
|
59 | 62 | res
|
60 | 63 | .status(401)
|
61 |
| - .json({ success: false, message: `No username or password supplied.` }) |
| 64 | + .json({ success: false, message: `No username or password supplied.` }); |
| 65 | + next(); |
62 | 66 | }
|
63 | 67 |
|
64 | 68 | // find this user in the database
|
65 | 69 | try {
|
66 |
| - const user = await User.findOne({ username: username }).exec() |
| 70 | + const user = await User.findOne({ username: username }).exec(); |
67 | 71 | // check if user was found
|
68 | 72 | if (!user) {
|
69 |
| - console.error(`User not found.`) |
70 |
| - return res.status(401).json({ |
| 73 | + console.error(`User not found.`); |
| 74 | + res.status(401).json({ |
71 | 75 | success: false,
|
72 | 76 | message: "User not found in database.",
|
73 |
| - }) |
| 77 | + }); |
| 78 | + next(); |
74 | 79 | }
|
75 | 80 | // if user exists, check if password is correct
|
76 | 81 | else if (!user.validPassword(password)) {
|
77 |
| - console.error(`Incorrect password.`) |
78 |
| - return res.status(401).json({ |
| 82 | + console.error(`Incorrect password.`); |
| 83 | + res.status(401).json({ |
79 | 84 | success: false,
|
80 | 85 | message: "Incorrect password.",
|
81 |
| - }) |
| 86 | + }); |
| 87 | + next(); |
82 | 88 | }
|
83 | 89 | // user found and password is correct... send a success response
|
84 |
| - console.log("User logged in successfully.") |
85 |
| - const token = user.generateJWT() // generate a signed token |
| 90 | + console.log("User logged in successfully."); |
| 91 | + const token = user.generateJWT(); // generate a signed token |
86 | 92 | res.json({
|
87 | 93 | success: true,
|
88 | 94 | message: "User logged in successfully.",
|
89 | 95 | token: token,
|
90 | 96 | username: user.username,
|
91 |
| - }) // send the token to the client to store |
| 97 | + }); // send the token to the client to store |
| 98 | + next(); |
92 | 99 | } catch (err) {
|
93 | 100 | // check error
|
94 |
| - console.error(`Error looking up user: ${err}`) |
95 |
| - return res.status(500).json({ |
| 101 | + console.error(`Error looking up user: ${err}`); |
| 102 | + res.status(500).json({ |
96 | 103 | success: false,
|
97 | 104 | message: "Error looking up user in database.",
|
98 | 105 | error: err,
|
99 |
| - }) |
| 106 | + }); |
| 107 | + next(); |
100 | 108 | }
|
101 |
| - }) |
| 109 | + }); |
102 | 110 |
|
103 | 111 | // a route to handle logging out requests to /auth/logout
|
104 |
| - router.get("/logout", function (req, res) { |
| 112 | + router.get("/logout", function (req, res, next) { |
105 | 113 | // nothing really to do here... logging out with JWT authentication is handled entirely by the front-end by deleting the token from the browser's memory
|
106 | 114 | res.json({
|
107 | 115 | success: true,
|
108 | 116 | message:
|
109 | 117 | "There is actually nothing to do on the server side... you simply need to delete your token from the browser's local storage!",
|
110 |
| - }) |
111 |
| - }) |
| 118 | + }); |
| 119 | + next(); |
| 120 | + }); |
112 | 121 |
|
113 |
| - return router |
114 |
| -} |
| 122 | + return router; |
| 123 | +}; |
115 | 124 |
|
116 | 125 | // export the router
|
117 |
| -module.exports = authenticationRouter |
| 126 | +module.exports = authenticationRouter; |
0 commit comments