Skip to content

Commit 58acb0a

Browse files
committed
fix: auth service logout
1 parent 0d1674e commit 58acb0a

File tree

2 files changed

+11
-6
lines changed

2 files changed

+11
-6
lines changed

src/controllers/Auth/controller.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -68,10 +68,11 @@ routes.post(
6868
Authorization,
6969
asyncHandler(async function logout(req: Request, res: Response) {
7070
const { UserId } = req.getBody()
71+
const userData = req.getState('user')
7172

72-
const message = await AuthService.logout(UserId)
73+
const message = await AuthService.logout(UserId, userData)
7374
const buildResponse = BuildResponse.deleted({ message })
7475

75-
return res.status(200).json(buildResponse)
76+
return res.clearCookie('token', { path: '/v1' }).json(buildResponse)
7677
})
7778
)

src/controllers/Auth/service.ts

+8-4
Original file line numberDiff line numberDiff line change
@@ -156,13 +156,17 @@ class AuthService {
156156

157157
/**
158158
*
159-
* @param userId
159+
* @param UserId
160160
*/
161-
public static async logout(userId: string) {
162-
const userData = await UserService.getOne(userId)
161+
public static async logout(UserId: string, userData: any) {
162+
if (userData?.id !== UserId) {
163+
throw new ResponseError.Unauthorized('Invalid user login!')
164+
}
165+
166+
const data = await UserService.getOne(UserId)
163167

164168
// remove refresh token by user id
165-
await RefreshTokenService.delete(userData.id)
169+
await RefreshTokenService.delete(data.id)
166170
const message = 'You have logged out of the application'
167171

168172
return message

0 commit comments

Comments
 (0)