diff --git a/webv2/middleware/auth.ts b/webv2/middleware/auth.ts index 3e6ff2c..b9580c5 100644 --- a/webv2/middleware/auth.ts +++ b/webv2/middleware/auth.ts @@ -4,7 +4,7 @@ export default defineNuxtRouteMiddleware(async (to) => { } try { - const user = await $fetch('/api/users/@me') + const user: IUser = await $fetch('/api/users/@me') if (!user._id) { throw createError({ statusCode: 500, statusMessage: 'No user was found' }) diff --git a/webv2/server/api/users/[id]/index.get.ts b/webv2/server/api/users/[id]/index.get.ts index 2d5a1cd..1af7ff6 100644 --- a/webv2/server/api/users/[id]/index.get.ts +++ b/webv2/server/api/users/[id]/index.get.ts @@ -1,5 +1,8 @@ export default defineEventHandler(async (event) => { const userId: string = event.context.params ? event.context.params.id : '@me' - return await getUser(userId, event) + const user = await getUser(userId, event) + await applyUsername(user) + + return user }) diff --git a/webv2/server/api/users/[id]/minecraft/index.delete.ts b/webv2/server/api/users/[id]/minecraft/index.delete.ts index a10c0b8..45d2fee 100644 --- a/webv2/server/api/users/[id]/minecraft/index.delete.ts +++ b/webv2/server/api/users/[id]/minecraft/index.delete.ts @@ -11,7 +11,7 @@ export default defineEventHandler(async (event) => { await WhitelistModel.deleteOne({ uuid: user.minecraft.uuid }) user.minecraft = undefined - await user.save() + await applyUsername(user) } catch (e) { console.error('Failed to update documents', e) diff --git a/webv2/server/api/users/[id]/minecraft/index.get.ts b/webv2/server/api/users/[id]/minecraft/index.get.ts index 253b15b..5678172 100644 --- a/webv2/server/api/users/[id]/minecraft/index.get.ts +++ b/webv2/server/api/users/[id]/minecraft/index.get.ts @@ -12,7 +12,7 @@ export default defineEventHandler(async (event) => { try { user.minecraft = { uuid: user.minecraft.uuid, username: minecraftProfile.name } - await user.save() + await applyUsername(user) } catch (e) { console.error('Failed to update document', e) diff --git a/webv2/server/api/users/[id]/minecraft/index.put.ts b/webv2/server/api/users/[id]/minecraft/index.put.ts index 1047ae8..152433d 100644 --- a/webv2/server/api/users/[id]/minecraft/index.put.ts +++ b/webv2/server/api/users/[id]/minecraft/index.put.ts @@ -28,7 +28,7 @@ export default defineEventHandler(async (event) => { user.minecraft = { uuid: whitelistDoc.uuid, username: minecraftProfile.name } await whitelistDoc.save() - await user.save() + await applyUsername(user) } catch (e) { console.error('Failed to update documents', e) diff --git a/webv2/server/utils/models.ts b/webv2/server/utils/models.ts index d64ae4e..23275a5 100644 --- a/webv2/server/utils/models.ts +++ b/webv2/server/utils/models.ts @@ -1,7 +1,7 @@ import { Schema, Types, model } from 'mongoose' const userSchema = new Schema({ - username: { type: String, required: true }, + username: { type: String, required: false }, usernameType: { type: String, required: true, default: 'discord' }, discord: { id: { type: String, required: true, unique: true }, diff --git a/webv2/server/utils/user.ts b/webv2/server/utils/user.ts index 4937eb2..660f686 100644 --- a/webv2/server/utils/user.ts +++ b/webv2/server/utils/user.ts @@ -46,3 +46,15 @@ export const getUser = async (userId: string, event: any) => { throw createError({ statusCode: 500, statusMessage: 'Failed to get user' }) } } + +export const applyUsername = async (user: IUser) => { + if (user.usernameType === 'discord' || user.$isEmpty('minecraft') || !user.minecraft) { + user.username = user.discord.username + + await user.save() + } else { + user.username = user.minecraft.username + + await user.save() + } +} diff --git a/webv2/types/global.d.ts b/webv2/types/global.d.ts index 2175ac9..fdea89e 100644 --- a/webv2/types/global.d.ts +++ b/webv2/types/global.d.ts @@ -1,9 +1,12 @@ +import { Document } from 'mongoose' + export {} declare global { - interface IUser { + interface IUser extends Document { _id: string, username: string, + usernameType: string, discord: { id: string, username: string,