-
Notifications
You must be signed in to change notification settings - Fork 0
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
Siddharth Patil
authored and
Siddharth Patil
committed
Nov 14, 2023
1 parent
1ec4b9a
commit bf4d7b6
Showing
6 changed files
with
94 additions
and
2 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 |
---|---|---|
@@ -1,3 +1,37 @@ | ||
import User from "../models/user.model.js" | ||
import { errorHandler } from "../utils/error.js" | ||
import bcryptjs from 'bcryptjs' | ||
|
||
export const test = (req, res) =>{ | ||
res.send("hello world") | ||
} | ||
} | ||
|
||
export const updateUser = async (req, res, next) => { | ||
if (req.user.id !== req.params.id) | ||
return next(errorHandler(401, 'You can only update your own account!')); | ||
try { | ||
if (req.body.password) { | ||
req.body.password = bcryptjs.hashSync(req.body.password, 10); | ||
} | ||
|
||
const updatedUser = await User.findByIdAndUpdate( | ||
req.params.id, | ||
{ | ||
$set: { | ||
username: req.body.username, | ||
email: req.body.email, | ||
password: req.body.password, | ||
avatar: req.body.avatar, | ||
phone: req.body.phone | ||
}, | ||
}, | ||
{ new: true } | ||
); | ||
|
||
const { password, ...rest } = updatedUser._doc; | ||
|
||
res.status(200).json(rest); | ||
} catch (error) { | ||
next(error); | ||
} | ||
}; |
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,8 +1,10 @@ | ||
import express from 'express'; | ||
import { test } from '../controllers/user.controller.js'; | ||
import { test, updateUser } from '../controllers/user.controller.js'; | ||
import { verifyToken } from '../utils/verifyUser.js'; | ||
|
||
const router = express.Router(); | ||
|
||
router.get('/test', test) | ||
router.post('/update/:id', verifyToken, updateUser) | ||
|
||
export default router |
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,15 @@ | ||
import jwt from 'jsonwebtoken'; | ||
import { errorHandler } from './error.js'; | ||
|
||
export const verifyToken = (req, res, next) => { | ||
const token = req.cookies.access_token; | ||
|
||
if (!token) return next(errorHandler(401, 'Unauthorized')); | ||
|
||
jwt.verify(token, process.env.JWT_SECRET, (err, user) => { | ||
if (err) return next(errorHandler(403, 'Forbidden')); | ||
|
||
req.user = user; | ||
next(); | ||
}); | ||
}; |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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