From 3bd7266dcf79fc222b5a1d50baef0ae7c902d4a6 Mon Sep 17 00:00:00 2001 From: moonrailgun Date: Mon, 9 Oct 2023 00:10:52 +0800 Subject: [PATCH] refactor: hide invalid token error --- src/server/router/user.ts | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/src/server/router/user.ts b/src/server/router/user.ts index f4cc79b..b101932 100644 --- a/src/server/router/user.ts +++ b/src/server/router/user.ts @@ -1,5 +1,5 @@ import { Router } from 'express'; -import { header, body, validate } from '../middleware/validate'; +import { body, validate } from '../middleware/validate'; import { authUser, authUserWithToken, @@ -64,11 +64,15 @@ userRouter.post( throw new Error('Cannot get token'); } - const user = await authUserWithToken(token); + try { + const user = await authUserWithToken(token); - const newToken = jwtSign(user); + const newToken = jwtSign(user); - res.json({ info: user, token: newToken }); + res.json({ info: user, token: newToken }); + } catch (err) { + res.status(401).json({ message: 'Invalid token' }); + } } );