|
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, next) => { |
| 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 |
| - }); |
25 |
| - next(); |
| 24 | + }) |
| 25 | + next() |
26 | 26 | }
|
27 | 27 |
|
28 | 28 | // try to create a new user
|
29 | 29 | try {
|
30 |
| - const user = await new User({ username, password }).save(); |
| 30 | + const user = await new User({ username, password }).save() |
31 | 31 | // user saved successfully... send a success response
|
32 |
| - console.error(`New user: ${user}`); |
33 |
| - const token = user.generateJWT(); // generate a signed token |
| 32 | + console.error(`New user: ${user}`) |
| 33 | + const token = user.generateJWT() // generate a signed token |
34 | 34 | res.json({
|
35 | 35 | success: true,
|
36 |
| - message: "User saved successfully.", |
| 36 | + message: 'User saved successfully.', |
37 | 37 | token: token,
|
38 | 38 | username: user.username,
|
39 |
| - }); // send the token to the client to store |
40 |
| - next(); |
| 39 | + }) // send the token to the client to store |
| 40 | + next() |
41 | 41 | } catch (err) {
|
42 | 42 | // error saving user to database... send an error response
|
43 |
| - console.error(`Failed to save user: ${err}`); |
| 43 | + console.error(`Failed to save user: ${err}`) |
44 | 44 | res.status(500).json({
|
45 | 45 | success: false,
|
46 |
| - message: "Error saving user to database.", |
| 46 | + message: 'Error saving user to database.', |
47 | 47 | error: err,
|
48 |
| - }); |
49 |
| - next(); |
| 48 | + }) |
| 49 | + next() |
50 | 50 | }
|
51 |
| - }); |
| 51 | + }) |
52 | 52 |
|
53 | 53 | // a route to handle login attempts requested to /auth/login
|
54 |
| - router.post("/login", async function (req, res, next) { |
| 54 | + router.post('/login', async function (req, res, next) { |
55 | 55 | // grab the name and password that were submitted as POST body data
|
56 |
| - const username = req.body.username; |
57 |
| - const password = req.body.password; |
| 56 | + const username = req.body.username |
| 57 | + const password = req.body.password |
58 | 58 | // console.log(`${username}, ${password}`)
|
59 | 59 |
|
60 | 60 | if (!username || !password) {
|
61 | 61 | // no username or password received in the POST body... send an error
|
62 | 62 | res
|
63 | 63 | .status(401)
|
64 |
| - .json({ success: false, message: `No username or password supplied.` }); |
65 |
| - next(); |
| 64 | + .json({ success: false, message: `No username or password supplied.` }) |
| 65 | + next() |
66 | 66 | }
|
67 | 67 |
|
68 | 68 | // find this user in the database
|
69 | 69 | try {
|
70 |
| - const user = await User.findOne({ username: username }).exec(); |
| 70 | + const user = await User.findOne({ username: username }).exec() |
71 | 71 | // check if user was found
|
72 | 72 | if (!user) {
|
73 |
| - console.error(`User not found.`); |
| 73 | + console.error(`User not found.`) |
74 | 74 | res.status(401).json({
|
75 | 75 | success: false,
|
76 |
| - message: "User not found in database.", |
77 |
| - }); |
78 |
| - next(); |
| 76 | + message: 'User not found in database.', |
| 77 | + }) |
| 78 | + next() |
79 | 79 | }
|
80 | 80 | // if user exists, check if password is correct
|
81 | 81 | else if (!user.validPassword(password)) {
|
82 |
| - console.error(`Incorrect password.`); |
| 82 | + console.error(`Incorrect password.`) |
83 | 83 | res.status(401).json({
|
84 | 84 | success: false,
|
85 |
| - message: "Incorrect password.", |
86 |
| - }); |
87 |
| - next(); |
| 85 | + message: 'Incorrect password.', |
| 86 | + }) |
| 87 | + next() |
88 | 88 | }
|
89 | 89 | // user found and password is correct... send a success response
|
90 |
| - console.log("User logged in successfully."); |
91 |
| - const token = user.generateJWT(); // generate a signed token |
| 90 | + console.log('User logged in successfully.') |
| 91 | + const token = user.generateJWT() // generate a signed token |
92 | 92 | res.json({
|
93 | 93 | success: true,
|
94 |
| - message: "User logged in successfully.", |
| 94 | + message: 'User logged in successfully.', |
95 | 95 | token: token,
|
96 | 96 | username: user.username,
|
97 |
| - }); // send the token to the client to store |
98 |
| - next(); |
| 97 | + }) // send the token to the client to store |
| 98 | + next() |
99 | 99 | } catch (err) {
|
100 | 100 | // check error
|
101 |
| - console.error(`Error looking up user: ${err}`); |
| 101 | + console.error(`Error looking up user: ${err}`) |
102 | 102 | res.status(500).json({
|
103 | 103 | success: false,
|
104 |
| - message: "Error looking up user in database.", |
| 104 | + message: 'Error looking up user in database.', |
105 | 105 | error: err,
|
106 |
| - }); |
107 |
| - next(); |
| 106 | + }) |
| 107 | + next() |
108 | 108 | }
|
109 |
| - }); |
| 109 | + }) |
110 | 110 |
|
111 | 111 | // a route to handle logging out requests to /auth/logout
|
112 |
| - router.get("/logout", function (req, res, next) { |
| 112 | + router.get('/logout', function (req, res, next) { |
113 | 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
|
114 | 114 | res.json({
|
115 | 115 | success: true,
|
116 | 116 | message:
|
117 | 117 | "There is actually nothing to do on the server side... you simply need to delete your token from the browser's local storage!",
|
118 |
| - }); |
119 |
| - next(); |
120 |
| - }); |
| 118 | + }) |
| 119 | + next() |
| 120 | + }) |
121 | 121 |
|
122 |
| - return router; |
123 |
| -}; |
| 122 | + return router |
| 123 | +} |
124 | 124 |
|
125 | 125 | // export the router
|
126 |
| -module.exports = authenticationRouter; |
| 126 | +module.exports = authenticationRouter |
0 commit comments