From f1549a0ee0eb2faf2775f5f1409e87b555b08bbc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20N=C3=A9grier?= Date: Tue, 22 Mar 2022 12:14:23 +0100 Subject: [PATCH] Removing the temporary error message when moving to open id screen --- front/src/Connexion/ConnectionManager.ts | 28 ++++++++++++++++-------- front/src/Phaser/Game/GameManager.ts | 10 ++++++++- front/src/Phaser/Login/EmptyScene.ts | 17 ++++++++++++++ front/src/Phaser/Login/LoginScene.ts | 5 ++++- 4 files changed, 49 insertions(+), 11 deletions(-) create mode 100644 front/src/Phaser/Login/EmptyScene.ts diff --git a/front/src/Connexion/ConnectionManager.ts b/front/src/Connexion/ConnectionManager.ts index 3e314e50..09e7257d 100644 --- a/front/src/Connexion/ConnectionManager.ts +++ b/front/src/Connexion/ConnectionManager.ts @@ -45,8 +45,10 @@ class ConnectionManager { /** * TODO fix me to be move in game manager + * + * Returns the URL that we need to redirect to to load the OpenID screen, or "null" if no redirection needs to happen. */ - public loadOpenIDScreen() { + public loadOpenIDScreen(): URL | null { const state = localUserStore.generateState(); const nonce = localUserStore.generateNonce(); localUserStore.setAuthToken(null); @@ -59,7 +61,6 @@ class ConnectionManager { redirectUrl.searchParams.append("state", state); redirectUrl.searchParams.append("nonce", nonce); redirectUrl.searchParams.append("playUri", this._currentRoom.key); - window.location.assign(redirectUrl.toString()); return redirectUrl; } @@ -83,8 +84,10 @@ class ConnectionManager { /** * Tries to login to the node server and return the starting map url to be loaded + * + * @return returns a promise to the Room we are going to load OR a pointer to the URL we must redirect to if authentication is needed. */ - public async initGameConnexion(): Promise { + public async initGameConnexion(): Promise { const connexionType = urlManager.getGameConnexionType(); this.connexionType = connexionType; this._currentRoom = null; @@ -101,8 +104,9 @@ class ConnectionManager { if (connexionType === GameConnexionTypes.login) { this._currentRoom = await Room.createRoom(new URL(localUserStore.getLastRoomUrl())); - if (this.loadOpenIDScreen() !== null) { - return Promise.reject(new Error("You will be redirect on login page")); + const redirect = this.loadOpenIDScreen(); + if (redirect !== null) { + return redirect; } urlManager.pushRoomIdToUrl(this._currentRoom); } else if (connexionType === GameConnexionTypes.jwt) { @@ -124,8 +128,11 @@ class ConnectionManager { analyticsClient.loggedWithSso(); } catch (err) { console.error(err); - this.loadOpenIDScreen(); - return Promise.reject(new Error("You will be redirect on login page")); + const redirect = this.loadOpenIDScreen(); + if (redirect === null) { + throw new Error("Unable to redirect on login page."); + } + return redirect; } urlManager.pushRoomIdToUrl(this._currentRoom); } else if (connexionType === GameConnexionTypes.register) { @@ -212,8 +219,11 @@ class ConnectionManager { err.response?.data && err.response.data !== "User cannot to be connected on openid provider") ) { - this.loadOpenIDScreen(); - return Promise.reject(new Error("You will be redirect on login page")); + const redirect = this.loadOpenIDScreen(); + if (redirect === null) { + throw new Error("Unable to redirect on login page."); + } + return redirect; } } } diff --git a/front/src/Phaser/Game/GameManager.ts b/front/src/Phaser/Game/GameManager.ts index 520929eb..0220fca1 100644 --- a/front/src/Phaser/Game/GameManager.ts +++ b/front/src/Phaser/Game/GameManager.ts @@ -9,6 +9,7 @@ import { EnableCameraSceneName } from "../Login/EnableCameraScene"; import { LoginSceneName } from "../Login/LoginScene"; import { SelectCharacterSceneName } from "../Login/SelectCharacterScene"; import { GameScene } from "./GameScene"; +import { EmptySceneName } from "../Login/EmptyScene"; /** * This class should be responsible for any scene starting/stopping @@ -32,7 +33,14 @@ export class GameManager { public async init(scenePlugin: Phaser.Scenes.ScenePlugin): Promise { this.scenePlugin = scenePlugin; - this.startRoom = await connectionManager.initGameConnexion(); + const result = await connectionManager.initGameConnexion(); + if (result instanceof URL) { + window.location.assign(result.toString()); + // window.location.assign is not immediate and Javascript keeps running after. + // so we need to redirect to an empty Phaser scene, waiting for the redirection to take place + return EmptySceneName; + } + this.startRoom = result; this.loadMap(this.startRoom); //If player name was not set show login scene with player name diff --git a/front/src/Phaser/Login/EmptyScene.ts b/front/src/Phaser/Login/EmptyScene.ts new file mode 100644 index 00000000..4511a160 --- /dev/null +++ b/front/src/Phaser/Login/EmptyScene.ts @@ -0,0 +1,17 @@ +import { Scene } from "phaser"; + +export const EmptySceneName = "EmptyScene"; + +export class EmptyScene extends Scene { + constructor() { + super({ + key: EmptySceneName, + }); + } + + preload() {} + + create() {} + + update(time: number, delta: number): void {} +} diff --git a/front/src/Phaser/Login/LoginScene.ts b/front/src/Phaser/Login/LoginScene.ts index 94f04db9..14cee6a1 100644 --- a/front/src/Phaser/Login/LoginScene.ts +++ b/front/src/Phaser/Login/LoginScene.ts @@ -28,7 +28,10 @@ export class LoginScene extends ResizableScene { gameManager.currentStartedRoom && gameManager.currentStartedRoom.authenticationMandatory ) { - connectionManager.loadOpenIDScreen(); + const redirect = connectionManager.loadOpenIDScreen(); + if (redirect !== null) { + window.location.assign(redirect.toString()); + } loginSceneVisibleIframeStore.set(true); } loginSceneVisibleStore.set(true);