From 12e92b8106e22e6174c3bb2491974ecb8570098c Mon Sep 17 00:00:00 2001 From: Xeovalyte Date: Mon, 5 Jun 2023 10:17:35 +0200 Subject: [PATCH] feat: Added get user by id, closes #18 --- webv2/server/api/auth.ts | 2 +- webv2/server/api/users/[id]/index.get.ts | 22 ++++++++++-- webv2/server/utils/user.ts | 43 ++++++++++++++++++++++++ webv2/types/global.d.ts | 1 + 4 files changed, 64 insertions(+), 4 deletions(-) create mode 100644 webv2/server/utils/user.ts diff --git a/webv2/server/api/auth.ts b/webv2/server/api/auth.ts index ba1e4d3..8d52cc7 100644 --- a/webv2/server/api/auth.ts +++ b/webv2/server/api/auth.ts @@ -1,4 +1,4 @@ -import jwt from 'jsonwebtoken' +import * as jwt from 'jsonwebtoken' type AccessTokenResponse = { access_token: string, diff --git a/webv2/server/api/users/[id]/index.get.ts b/webv2/server/api/users/[id]/index.get.ts index 788ded2..2fa94a2 100644 --- a/webv2/server/api/users/[id]/index.get.ts +++ b/webv2/server/api/users/[id]/index.get.ts @@ -1,5 +1,21 @@ -export default defineEventHandler((event) => { - return { - hello: 'world' +export default defineEventHandler(async (event) => { + const userId: string = event.context.params ? event.context.params.id : '@me' + + if (userId === '@me') { + return await getCurrentUser(event) + } + + try { + const user = await UserModel.findById(userId) + + if (!user) { + throw createError({ statusCode: 400, statusMessage: 'No user was found' }) + } + + return user + } catch (e) { + console.error('Failed to get user by id', e) + + throw createError({ statusCode: 500, statusMessage: 'Failed to get user' }) } }) diff --git a/webv2/server/utils/user.ts b/webv2/server/utils/user.ts new file mode 100644 index 0000000..8d1ce46 --- /dev/null +++ b/webv2/server/utils/user.ts @@ -0,0 +1,43 @@ +import * as jwt from 'jsonwebtoken' + +interface IDecodedToken { + discordId: string, + accessToken: string, + refreshToken: string +} + +export const getCurrentUser = async (event: any) => { + const token = getCookie(event, 'jwt') || null + + if (!token) { + throw createError({ statusCode: 401, statusMessage: 'JWT token is invalid' }) + } + + let decodedToken + try { + decodedToken = jwt.verify(token, config.jwtSecret) as IDecodedToken + } catch (e) { + console.error('Failed to verify JWT token', e) + + throw createError({ + statusCode: 401, + statusMessage: 'JWT token is invalid' + }) + } + + try { + const user = await UserModel.findOne({ 'discord.id': decodedToken.discordId }) + + if (!user) { + throw createError({ statusCode: 500, statusMessage: 'User query returned null' }) + } + + user.accessToken = decodedToken.accessToken + + return user + } catch (e) { + console.error('Failed to get user', e) + + throw createError({ statusCode: 500, statusMessage: 'Failed to get user' }) + } +} diff --git a/webv2/types/global.d.ts b/webv2/types/global.d.ts index f24fe6b..98a452d 100644 --- a/webv2/types/global.d.ts +++ b/webv2/types/global.d.ts @@ -19,5 +19,6 @@ declare global { moderator: boolean, }, teamId: string, + accessToken?: string } }