mirror of
https://github.com/openfrontio/OpenFrontIO.git
synced 2026-06-21 15:40:43 +00:00
Fix discord login issue (#1028)
Fixes #1016 ## Description: Fix the zod user schema validation to make roles optional. This was causing a validation error when looking up the user's roles. ## Please complete the following: - [x] I have added screenshots for all UI updates - [x] I process any text displayed to the user through translateText() and I've added it to the en.json file - [x] I have added relevant tests to the test directory - [x] I confirm I have thoroughly tested these changes and take full responsibility for any bugs introduced - [x] I understand that submitting code with bugs that could have been caught through manual testing blocks releases and new features for all contributors Co-authored-by: Scott Anderson <662325+scottanderson@users.noreply.github.com>
This commit is contained in:
committed by
evanpelle
parent
adb2d0653f
commit
01ae9bc7f4
+7
-15
@@ -1,4 +1,5 @@
|
||||
import { decodeJwt } from "jose";
|
||||
import { z } from "zod/v4";
|
||||
import {
|
||||
RefreshResponseSchema,
|
||||
TokenPayload,
|
||||
@@ -138,12 +139,9 @@ function _isLoggedIn(): IsLoggedInResponse {
|
||||
|
||||
const result = TokenPayloadSchema.safeParse(payload);
|
||||
if (!result.success) {
|
||||
const error = z.prettifyError(result.error);
|
||||
// Invalid response
|
||||
console.error(
|
||||
"Invalid payload",
|
||||
// JSON.stringify(payload),
|
||||
JSON.stringify(result.error),
|
||||
);
|
||||
console.error("Invalid payload", error);
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -171,11 +169,8 @@ export async function postRefresh(): Promise<boolean> {
|
||||
const body = await response.json();
|
||||
const result = RefreshResponseSchema.safeParse(body);
|
||||
if (!result.success) {
|
||||
console.error(
|
||||
"Invalid response",
|
||||
JSON.stringify(body),
|
||||
JSON.stringify(result.error),
|
||||
);
|
||||
const error = z.prettifyError(result.error);
|
||||
console.error("Invalid response", error);
|
||||
return false;
|
||||
}
|
||||
localStorage.setItem("token", result.data.token);
|
||||
@@ -201,11 +196,8 @@ export async function getUserMe(): Promise<UserMeResponse | false> {
|
||||
const body = await response.json();
|
||||
const result = UserMeResponseSchema.safeParse(body);
|
||||
if (!result.success) {
|
||||
console.error(
|
||||
"Invalid response",
|
||||
JSON.stringify(body),
|
||||
JSON.stringify(result.error),
|
||||
);
|
||||
const error = z.prettifyError(result.error);
|
||||
console.error("Invalid response", error);
|
||||
return false;
|
||||
}
|
||||
return result.data;
|
||||
|
||||
@@ -42,7 +42,7 @@ export const UserMeResponseSchema = z.object({
|
||||
}),
|
||||
player: z.object({
|
||||
publicId: z.string(),
|
||||
roles: z.string().array(),
|
||||
roles: z.string().array().optional(),
|
||||
}),
|
||||
});
|
||||
export type UserMeResponse = z.infer<typeof UserMeResponseSchema>;
|
||||
|
||||
@@ -12,7 +12,7 @@ export class Client {
|
||||
public readonly clientID: ClientID,
|
||||
public readonly persistentID: string,
|
||||
public readonly claims: TokenPayload | null,
|
||||
public readonly roles: string[] | null,
|
||||
public readonly roles: string[] | undefined,
|
||||
public readonly ip: string,
|
||||
public readonly username: string,
|
||||
public readonly ws: WebSocket,
|
||||
|
||||
@@ -314,7 +314,7 @@ export function startWorker() {
|
||||
config,
|
||||
);
|
||||
|
||||
const roles: string[] | null = null;
|
||||
let roles: string[] | undefined;
|
||||
|
||||
// Check user roles
|
||||
if (claims !== null) {
|
||||
@@ -323,6 +323,7 @@ export function startWorker() {
|
||||
log.warn("Token is not valid", claims);
|
||||
return;
|
||||
}
|
||||
roles = result.player.roles;
|
||||
}
|
||||
|
||||
// TODO: Validate client settings based on roles
|
||||
|
||||
Reference in New Issue
Block a user