Skip to content

Commit

Permalink
Utschub/feature/ta-2 (#19)
Browse files Browse the repository at this point in the history
* added jsonwebtokens for login/registration for authentication

* added login feature
  • Loading branch information
andrewqian2001 authored Sep 29, 2022
1 parent ddba683 commit cecd729
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 6 deletions.
3 changes: 3 additions & 0 deletions server/config/constants.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"jsonwebtokenSecret" : "secretKey"
}
35 changes: 33 additions & 2 deletions server/routes/auth.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
const express = require('express');
const router = express.Router();
const { check, validationResult } = require('express-validator');
const User = require("../models/User");
const jsonwebtoken = require('jsonwebtoken');
const constants = require('../config/constants.json');
const bcrypt = require('bcryptjs');

//http://localhost:8000/api/auth

Expand All @@ -17,8 +22,34 @@ router.get('/', (request, response) => { // note: it is just a slash since we de
DESC: Authenticate and get token (so that we can access private routes)
ACCESS: Public
*/
router.post('/', (request, response) => { // note: it is just a slash since we defined the route already in server.js
response.send('User is logged in');
router.post('/', [
check('email', 'Please enter a valid email').isEmail(),
check('email', 'Please use a UofT email').custom(value => {
return value.includes("@mail.utoronto.ca");
}),
check('password', 'Please enter a password with 5 or more characters').isLength({min: 5})
], async (request, response) => {
const errorMessages = validationResult(request);
if(!errorMessages.isEmpty()) return response.status(400).json({errors: errorMessages.array()});
const { email, password } = request.body;
try{
let user = await User.findOne({email});
const errMsg = {message: 'User credentials is invalid'};
if(!user) return response.status(400).json(errMsg);
const passwordMatches = await bcrypt.compare(password, user.password); //compares password to encrypted password
if(!passwordMatches) return response.sendStatus(400).json(errMsg);
jsonwebtoken.sign(
{user: { id: user.id}}, //user id is auto generated by mongoose
constants.jsonwebtokenSecret, {
expiresIn: 7200 //2 hours
}, (error, token) => {
if(error) throw error;
response.json({token});
});
}catch(err){
console.log(err.message);
response.status(500); //server error
}
});

module.exports = router;
17 changes: 13 additions & 4 deletions server/routes/users.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ const express = require('express');
const router = express.Router();
const { check, validationResult } = require('express-validator');
const User = require("../models/User");
const jsonwebtoken = require('jsonwebtoken');
const constants = require('../config/constants.json');
const bcrypt = require('bcryptjs');


Expand All @@ -20,8 +22,8 @@ router.post('/', [
}),
check('password', 'Please enter a password with 5 or more characters').isLength({min: 5})
], async (request, response) => {
const errors = validationResult(request);
if(!errors.isEmpty()) return response.status(400).json({errors: errors.array()});
const errorMessages = validationResult(request);
if(!errorMessages.isEmpty()) return response.status(400).json({errors: errorMessages.array()});
const {name, email, password} = request.body;
try{
let user = await User.findOne({email: email});
Expand All @@ -34,10 +36,17 @@ router.post('/', [
const salt = await bcrypt.genSalt() //encrypt the password
user.password = await bcrypt.hash(password, salt);
await user.save();
response.send("User is registered");
jsonwebtoken.sign(
{user: { id: user.id}}, //user id is auto generated by mongoose
constants.jsonwebtokenSecret, {
expiresIn: 7200 //2 hours
}, (error, token) => {
if(error) throw error;
response.json({token});
});
}catch(error){
console.log(error.message);
response.send(500); //server error
response.sendStatus(500); //server error
}
});

Expand Down

0 comments on commit cecd729

Please sign in to comment.