Skip to content
Snippets Groups Projects
Commit 980a3dbe authored by Ahmad, Rezwan (PG/T - Comp Sci & Elec Eng)'s avatar Ahmad, Rezwan (PG/T - Comp Sci & Elec Eng)
Browse files

Authentication auth.js File Added as Middleware

parent 85fdafbe
No related branches found
No related tags found
1 merge request!9Create endpoint for user-registration
......@@ -170,13 +170,13 @@ body: {
export async function updateUser(req,res){
try {
const id = req.query.id;
const {userId} = req.user;
if(id){
if(userId){
const body = req.body;
// update the data
UserModel.updateOne({ _id : id }, body, function(err, data){
UserModel.updateOne({ _id : userId }, body, function(err, data){
if(err) throw err;
return res.status(201).send({ msg : "Record Updated...!"});
......
import jwt from 'jsonwebtoken';
import ENV from '../config.js'
export default async function Auth(req, res, next){
try {
const token = req.headers.authorization.split(" ")[1];
const decodedToken = await jwt.verify(token, ENV.JWT_SECRET);
req.user = decodedToken;
next();
} catch (error) {
res.status(401).json({ error : "Authentication Failed!"})
}
}
\ No newline at end of file
import { Router } from "express";
import * as controller from '../controllers/appController.js';
import Auth from '../middleware/auth.js';
const router = Router();
/** POST Methods */
......@@ -12,6 +14,6 @@ router.route('/user/:username').get(controller.getUser); // GetUser
/** PUT Methods */
router.route('/updateuser').put(controller.updateUser); // is use to update the user profile
router.route('/updateuser').put(Auth, controller.updateUser); // is use to update the user profile
export default router;
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment