-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
26 changed files
with
302 additions
and
296 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"tabWidth": 2, | ||
"semi": false, | ||
"arrowParens": "avoid", | ||
"singleQuote": true | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,126 +1,126 @@ | ||
const express = require("express"); // CommonJS import style! | ||
const express = require('express') // CommonJS import style! | ||
|
||
// mongoose models for MongoDB data manipulation | ||
const mongoose = require("mongoose"); | ||
const User = require("../models/User.js"); | ||
const mongoose = require('mongoose') | ||
const User = require('../models/User.js') | ||
|
||
// a method that constains code to handle authentication-specific routes | ||
const authenticationRouter = () => { | ||
// create a new router that we can customize | ||
const router = express.Router(); | ||
const router = express.Router() | ||
|
||
// a route to handle user signup requests to /auth/signup | ||
router.post("/signup", async (req, res, next) => { | ||
router.post('/signup', async (req, res, next) => { | ||
// console.log(`Incoming signup data: ${JSON.stringify(req.body, null, 0)}`) | ||
// grab the username and password from the POST body | ||
const username = req.body.username; | ||
const password = req.body.password; | ||
const username = req.body.username | ||
const password = req.body.password | ||
|
||
if (!username || !password) { | ||
// no username or password received in the POST body... send an error | ||
res.status(401).json({ | ||
success: false, | ||
message: `No username or password supplied.`, | ||
}); | ||
next(); | ||
}) | ||
next() | ||
} | ||
|
||
// try to create a new user | ||
try { | ||
const user = await new User({ username, password }).save(); | ||
const user = await new User({ username, password }).save() | ||
// user saved successfully... send a success response | ||
console.error(`New user: ${user}`); | ||
const token = user.generateJWT(); // generate a signed token | ||
console.error(`New user: ${user}`) | ||
const token = user.generateJWT() // generate a signed token | ||
res.json({ | ||
success: true, | ||
message: "User saved successfully.", | ||
message: 'User saved successfully.', | ||
token: token, | ||
username: user.username, | ||
}); // send the token to the client to store | ||
next(); | ||
}) // send the token to the client to store | ||
next() | ||
} catch (err) { | ||
// error saving user to database... send an error response | ||
console.error(`Failed to save user: ${err}`); | ||
console.error(`Failed to save user: ${err}`) | ||
res.status(500).json({ | ||
success: false, | ||
message: "Error saving user to database.", | ||
message: 'Error saving user to database.', | ||
error: err, | ||
}); | ||
next(); | ||
}) | ||
next() | ||
} | ||
}); | ||
}) | ||
|
||
// a route to handle login attempts requested to /auth/login | ||
router.post("/login", async function (req, res, next) { | ||
router.post('/login', async function (req, res, next) { | ||
// grab the name and password that were submitted as POST body data | ||
const username = req.body.username; | ||
const password = req.body.password; | ||
const username = req.body.username | ||
const password = req.body.password | ||
// console.log(`${username}, ${password}`) | ||
|
||
if (!username || !password) { | ||
// no username or password received in the POST body... send an error | ||
res | ||
.status(401) | ||
.json({ success: false, message: `No username or password supplied.` }); | ||
next(); | ||
.json({ success: false, message: `No username or password supplied.` }) | ||
next() | ||
} | ||
|
||
// find this user in the database | ||
try { | ||
const user = await User.findOne({ username: username }).exec(); | ||
const user = await User.findOne({ username: username }).exec() | ||
// check if user was found | ||
if (!user) { | ||
console.error(`User not found.`); | ||
console.error(`User not found.`) | ||
res.status(401).json({ | ||
success: false, | ||
message: "User not found in database.", | ||
}); | ||
next(); | ||
message: 'User not found in database.', | ||
}) | ||
next() | ||
} | ||
// if user exists, check if password is correct | ||
else if (!user.validPassword(password)) { | ||
console.error(`Incorrect password.`); | ||
console.error(`Incorrect password.`) | ||
res.status(401).json({ | ||
success: false, | ||
message: "Incorrect password.", | ||
}); | ||
next(); | ||
message: 'Incorrect password.', | ||
}) | ||
next() | ||
} | ||
// user found and password is correct... send a success response | ||
console.log("User logged in successfully."); | ||
const token = user.generateJWT(); // generate a signed token | ||
console.log('User logged in successfully.') | ||
const token = user.generateJWT() // generate a signed token | ||
res.json({ | ||
success: true, | ||
message: "User logged in successfully.", | ||
message: 'User logged in successfully.', | ||
token: token, | ||
username: user.username, | ||
}); // send the token to the client to store | ||
next(); | ||
}) // send the token to the client to store | ||
next() | ||
} catch (err) { | ||
// check error | ||
console.error(`Error looking up user: ${err}`); | ||
console.error(`Error looking up user: ${err}`) | ||
res.status(500).json({ | ||
success: false, | ||
message: "Error looking up user in database.", | ||
message: 'Error looking up user in database.', | ||
error: err, | ||
}); | ||
next(); | ||
}) | ||
next() | ||
} | ||
}); | ||
}) | ||
|
||
// a route to handle logging out requests to /auth/logout | ||
router.get("/logout", function (req, res, next) { | ||
router.get('/logout', function (req, res, next) { | ||
// 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 | ||
res.json({ | ||
success: true, | ||
message: | ||
"There is actually nothing to do on the server side... you simply need to delete your token from the browser's local storage!", | ||
}); | ||
next(); | ||
}); | ||
}) | ||
next() | ||
}) | ||
|
||
return router; | ||
}; | ||
return router | ||
} | ||
|
||
// export the router | ||
module.exports = authenticationRouter; | ||
module.exports = authenticationRouter |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.