-
Notifications
You must be signed in to change notification settings - Fork 531
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
wip: added ids change to account modal
- Loading branch information
Showing
13 changed files
with
373 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
const modulename = 'WebServer:AuthChangeIdentifiers'; | ||
import { AuthedCtx } from '@core/components/WebServer/ctxTypes'; | ||
import consoleFactory from '@extras/console'; | ||
import consts from '@shared/consts'; | ||
import { GenericApiResp } from '@shared/genericApiTypes'; | ||
import { z } from 'zod'; | ||
import got from '@core/extras/got.js'; | ||
const console = consoleFactory(modulename); | ||
|
||
//Helpers | ||
const cfxHttpReqOptions = { | ||
timeout: { request: 6000 }, | ||
}; | ||
type ProviderDataType = {id: string, identifier: string}; | ||
|
||
const bodySchema = z.object({ | ||
cfxreId: z.string(), | ||
discordId: z.string(), | ||
}); | ||
export type ApiChangeIdentifiersReqSchema = z.infer<typeof bodySchema>; | ||
|
||
/** | ||
* Route to change your own identifiers | ||
*/ | ||
export default async function AuthChangeIdentifiers(ctx: AuthedCtx) { | ||
//Sanity check | ||
const schemaRes = bodySchema.safeParse(ctx.request.body); | ||
if (!schemaRes.success) { | ||
return ctx.send<GenericApiResp>({ | ||
error: `Invalid request body: ${schemaRes.error.message}`, | ||
}); | ||
} | ||
const { cfxreId, discordId } = schemaRes.data; | ||
|
||
//Validate & translate FiveM ID | ||
let citizenfxData: ProviderDataType | false = false; | ||
if (cfxreId.length) { | ||
try { | ||
if (consts.validIdentifiers.fivem.test(cfxreId)) { | ||
const id = cfxreId.split(':')[1]; | ||
const res = await got(`https://policy-live.fivem.net/api/getUserInfo/${id}`, cfxHttpReqOptions).json(); | ||
if (!res.username || !res.username.length) { | ||
return ctx.send<GenericApiResp>({ | ||
error: `(ERR1) Invalid CitizenFX ID`, | ||
}); | ||
} | ||
citizenfxData = { | ||
id: res.username, | ||
identifier: cfxreId, | ||
}; | ||
} else { | ||
return ctx.send<GenericApiResp>({ | ||
error: `(ERR3) Invalid CitizenFX ID`, | ||
}); | ||
} | ||
} catch (error) { | ||
return ctx.send<GenericApiResp>({ | ||
error: `Failed to resolve CitizenFX ID to game identifier with error: ${(error as Error).message}`, | ||
}); | ||
} | ||
} | ||
|
||
//Validate Discord ID | ||
let discordData: ProviderDataType | false = false; | ||
if (discordId.length) { | ||
if (!consts.validIdentifiers.discord.test(discordId)) { | ||
return ctx.send<GenericApiResp>({ | ||
error: `Invalid Discord ID.`, | ||
}); | ||
} | ||
discordData = { | ||
id: discordId.substring(8), | ||
identifier: discordId, | ||
}; | ||
} | ||
|
||
//Get vault admin | ||
const vaultAdmin = ctx.txAdmin.adminVault.getAdminByName(ctx.admin.name); | ||
if (!vaultAdmin) throw new Error('Wait, what? Where is that admin?'); | ||
|
||
//Edit admin and give output | ||
try { | ||
await ctx.txAdmin.adminVault.editAdmin(ctx.admin.name, null, citizenfxData, discordData); | ||
|
||
ctx.admin.logAction('Changing own identifiers.'); | ||
return ctx.send<GenericApiResp>({ success: true }); | ||
} catch (error) { | ||
return ctx.send<GenericApiResp>({ error: (error as Error).message }); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
const modulename = 'WebServer:AuthGetIdentifiers'; | ||
import { AuthedCtx } from '@core/components/WebServer/ctxTypes'; | ||
import consoleFactory from '@extras/console'; | ||
import { z } from 'zod'; | ||
const console = consoleFactory(modulename); | ||
|
||
//Helper functions | ||
const bodySchema = z.object({ | ||
oldPassword: z.string().optional(), | ||
newPassword: z.string(), | ||
}); | ||
export type ApiChangePasswordReqSchema = z.infer<typeof bodySchema>; | ||
|
||
|
||
/** | ||
* Returns the identifiers of the current admin | ||
*/ | ||
export default async function AuthGetIdentifiers(ctx: AuthedCtx) { | ||
//Get vault admin | ||
const vaultAdmin = ctx.txAdmin.adminVault.getAdminByName(ctx.admin.name); | ||
if (!vaultAdmin) throw new Error('Wait, what? Where is that admin?'); | ||
|
||
return ctx.send({ | ||
cfxreId: (vaultAdmin.providers.citizenfx) ? vaultAdmin.providers.citizenfx.identifier : '', | ||
discordId: (vaultAdmin.providers.discord) ? vaultAdmin.providers.discord.identifier : '', | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.