Change type of unauthorized (#2123)

* Change type of unauthorized
* Fix redirect on received erroScreenMessage from API
This commit is contained in:
César Cardinale 2022-04-26 18:17:12 +02:00 committed by GitHub
parent 0316565f12
commit 97e3397398
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 22 additions and 11 deletions

View File

@ -2,6 +2,7 @@
import { fly } from "svelte/transition"; import { fly } from "svelte/transition";
import { errorScreenStore } from "../../Stores/ErrorScreenStore"; import { errorScreenStore } from "../../Stores/ErrorScreenStore";
import { gameManager } from "../../Phaser/Game/GameManager"; import { gameManager } from "../../Phaser/Game/GameManager";
import { connectionManager } from "../../Connexion/ConnectionManager";
import { get } from "svelte/store"; import { get } from "svelte/store";
import { onDestroy } from "svelte"; import { onDestroy } from "svelte";
@ -11,7 +12,8 @@
let errorScreen = get(errorScreenStore); let errorScreen = get(errorScreenStore);
function click() { function click() {
window.location.reload(); if (errorScreen.type === "unauthorized") void connectionManager.logout();
else window.location.reload();
} }
let details = errorScreen.details; let details = errorScreen.details;
let timeVar = errorScreen.timeToRetry ?? 0; let timeVar = errorScreen.timeToRetry ?? 0;
@ -37,9 +39,9 @@
<p class="details"> <p class="details">
{detailsStylized}{#if $errorScreenStore.type === "retry"}<div class="loading" />{/if} {detailsStylized}{#if $errorScreenStore.type === "retry"}<div class="loading" />{/if}
</p> </p>
{#if $errorScreenStore.type === "retry" && $errorScreenStore.canRetryManual} {#if ($errorScreenStore.type === "retry" && $errorScreenStore.canRetryManual) || $errorScreenStore.type === "unauthorized"}
<button type="button" class="nes-btn is-primary button" on:click={click}> <button type="button" class="nes-btn is-primary button" on:click={click}>
<img src={reload} alt="" class="reload" /> {#if $errorScreenStore.type === "retry"}<img src={reload} alt="" class="reload" />{/if}
{$errorScreenStore.buttonTitle} {$errorScreenStore.buttonTitle}
</button> </button>
{/if} {/if}

View File

@ -483,9 +483,15 @@ export class RoomConnection implements RoomConnection {
} }
case "errorScreenMessage": { case "errorScreenMessage": {
this._errorScreenMessageStream.next(message.errorScreenMessage); this._errorScreenMessageStream.next(message.errorScreenMessage);
if (message.errorScreenMessage.code !== "retry") this.closed = true; console.error("An error occurred server side: " + JSON.stringify(message.errorScreenMessage));
console.error("An error occurred server side: " + message.errorScreenMessage.code); if (message.errorScreenMessage.code !== "retry") {
errorScreenStore.setError(message.errorScreenMessage); this.closed = true;
}
if (message.errorScreenMessage.type === "redirect" && message.errorScreenMessage.urlToRedirect) {
window.location.assign(message.errorScreenMessage.urlToRedirect);
} else {
errorScreenStore.setError(message.errorScreenMessage);
}
break; break;
} }
default: { default: {

View File

@ -6,7 +6,6 @@ import { ReconnectingTextures } from "../Reconnecting/ReconnectingScene";
import { localeDetector } from "../../i18n/locales"; import { localeDetector } from "../../i18n/locales";
import { errorScreenStore } from "../../Stores/ErrorScreenStore"; import { errorScreenStore } from "../../Stores/ErrorScreenStore";
import { isErrorApiData } from "../../Messages/JsonMessages/ErrorApiData"; import { isErrorApiData } from "../../Messages/JsonMessages/ErrorApiData";
import { connectionManager } from "../../Connexion/ConnectionManager";
export const EntrySceneName = "EntryScene"; export const EntrySceneName = "EntryScene";
@ -49,9 +48,7 @@ export class EntryScene extends Scene {
.catch((err) => { .catch((err) => {
const errorType = isErrorApiData.safeParse(err?.response?.data); const errorType = isErrorApiData.safeParse(err?.response?.data);
if (errorType.success) { if (errorType.success) {
if (errorType.data.type === "unauthorized") { if (errorType.data.type === "redirect") {
void connectionManager.logout();
} else if (errorType.data.type === "redirect") {
window.location.assign(errorType.data.urlToRedirect); window.location.assign(errorType.data.urlToRedirect);
} else errorScreenStore.setError(err?.response?.data); } else errorScreenStore.setError(err?.response?.data);
} else { } else {

View File

@ -34,6 +34,12 @@ export const isErrorApiRedirectData = z.object({
export const isErrorApiUnauthorizedData = z.object({ export const isErrorApiUnauthorizedData = z.object({
type: z.literal("unauthorized"), type: z.literal("unauthorized"),
code: z.string(),
title: z.string(),
subtitle: z.string(),
details: z.string(),
image: z.string(),
buttonTitle: z.optional(z.nullable(z.string())),
}); });
export const isErrorApiData = z.discriminatedUnion("type", [ export const isErrorApiData = z.discriminatedUnion("type", [

View File

@ -651,7 +651,7 @@ export class SocketManager implements ZoneEventListener {
public emitErrorScreenMessage(client: compressors.WebSocket, errorApi: ErrorApiData) { public emitErrorScreenMessage(client: compressors.WebSocket, errorApi: ErrorApiData) {
const errorMessage = new ErrorScreenMessage(); const errorMessage = new ErrorScreenMessage();
errorMessage.setType(errorApi.type); errorMessage.setType(errorApi.type);
if (errorApi.type == "retry" || errorApi.type == "error") { if (errorApi.type == "retry" || errorApi.type == "error" || errorApi.type == "unauthorized") {
errorMessage.setCode(new StringValue().setValue(errorApi.code)); errorMessage.setCode(new StringValue().setValue(errorApi.code));
errorMessage.setTitle(new StringValue().setValue(errorApi.title)); errorMessage.setTitle(new StringValue().setValue(errorApi.title));
errorMessage.setSubtitle(new StringValue().setValue(errorApi.subtitle)); errorMessage.setSubtitle(new StringValue().setValue(errorApi.subtitle));