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' }) } }