Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TA-3: add auth middleware and get logged in user object #21

Merged
merged 1 commit into from
Sep 29, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion server/controller/controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const User = require('../models/User');
//controller to delete a user from the "database"
exports.deleteUser = async (request, response) => {
try {
await User.findByIdAndDelete(request.userID);
await User.findByIdAndDelete(request.user._id);
response.json({message: "User deleted"});
} catch(error) {
console.log(error.message);
Expand Down
16 changes: 16 additions & 0 deletions server/middleware/auth.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
const jsonwebtoken = require('jsonwebtoken');
const constants = require('../config/constants.json');

module.exports = (request, response, next) => {
const token = request.header('x-auth-token');
if(!token){
return response.status(401).json({message: 'No token, authorization denied'});
}
try {
const decoded = jsonwebtoken.verify(token, constants.jsonwebtokenSecret);
request.user = decoded.user;
next();
} catch(error) {
response.status(401).json({message: 'Invalid token'});
}
}
27 changes: 10 additions & 17 deletions server/routes/auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,7 @@ const User = require("../models/User");
const jsonwebtoken = require('jsonwebtoken');
const constants = require('../config/constants.json');
const bcrypt = require('bcryptjs');

const auth = (request, response, next) => {
const token = request.header('x-auth-token');

if(!token){
return response.status(401).json({message: 'No token, authorization denied'});
}
try {
const decoded = jsonwebtoken.verify(token, constants.jsonwebtokenSecret);
request.userID = decoded.userID;
next();
} catch(error) {
response.status(401).json({message: 'Token is not valid'});
}
}
const auth = require('../middleware/auth');

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

Expand All @@ -28,8 +14,15 @@ const auth = (request, response, next) => {
DESC: Get logged in user
ACCESS: Private (getting user thats logged in so it should be private)
*/
router.get('/', (request, response) => { // note: it is just a slash since we defined the route already in server.js
response.send('Fetch logged in user');
router.get('/', auth, async (request, response) => { // note: it is just a slash since we defined the route already in server.js
try{
//request.user is assigned in the middleware (auth)
const user = await User.findById(request.user.id).select('-password');
response.json(user);
}catch (error) {
console.log(error.message);
response.status(500);
}
});

/*
Expand Down
2 changes: 1 addition & 1 deletion server/routes/users.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const User = require("../models/User");
const jsonwebtoken = require('jsonwebtoken');
const constants = require('../config/constants.json');
const bcrypt = require('bcryptjs');
const auth = require('../routes/auth');
const auth = require('../middleware/auth');
const controller = require('../controller/controller');


Expand Down