From 876ddc87d22d9f716eefded374e872b8183f265c Mon Sep 17 00:00:00 2001 From: Gregoire Parant Date: Mon, 7 Feb 2022 19:26:34 +0100 Subject: [PATCH] =?UTF-8?q?Change=20acc=C3=A8s=20token=20with=20query=20pr?= =?UTF-8?q?ivateAccessToken=20in=20the=20url?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Gregoire Parant --- front/src/Connexion/ConnectionManager.ts | 40 ++++++++++++++++++++++-- front/src/Url/UrlManager.ts | 23 ++++++++++++-- 2 files changed, 58 insertions(+), 5 deletions(-) diff --git a/front/src/Connexion/ConnectionManager.ts b/front/src/Connexion/ConnectionManager.ts index 05d0255d..6f1984ae 100644 --- a/front/src/Connexion/ConnectionManager.ts +++ b/front/src/Connexion/ConnectionManager.ts @@ -2,7 +2,7 @@ import Axios from "axios"; import { PUSHER_URL } from "../Enum/EnvironmentVariable"; import { RoomConnection } from "./RoomConnection"; import type { OnConnectInterface, PositionInterface, ViewportInterface } from "./ConnexionModels"; -import { GameConnexionTypes, urlManager } from "../Url/UrlManager"; +import { GameConnexionTypes, queryPrivateAccessToken, urlManager } from "../Url/UrlManager"; import { localUserStore } from "./LocalUserStore"; import { CharacterTexture, LocalUser } from "./LocalUser"; import { Room } from "./Room"; @@ -124,8 +124,42 @@ class ConnectionManager { return Promise.reject(new Error("You will be redirect on login page")); } urlManager.pushRoomIdToUrl(this._currentRoom); - } else if (connexionType === GameConnexionTypes.register) { - //@deprecated + } else if (connexionType === GameConnexionTypes.privateAccessToken) { + const organizationMemberToken = urlManager.getPrivateAccessToken; + + //clear queryPrivateAccessToken query in window location + urlParams.delete(queryPrivateAccessToken); + + const data = await Axios.post(`${PUSHER_URL}/register`, { organizationMemberToken }).then( + (res) => res.data + ); + if (!isRegisterData(data)) { + console.error("Invalid data received from /register route. Data: ", data); + throw new Error("Invalid data received from /register route."); + } + this.localUser = new LocalUser(data.userUuid, data.textures, data.email); + this.authToken = data.authToken; + localUserStore.saveUser(this.localUser); + localUserStore.setAuthToken(this.authToken); + analyticsClient.loggedWithToken(); + + const roomUrl = data.roomUrl; + + const query = urlParams.toString(); + this._currentRoom = await Room.createRoom( + new URL( + window.location.protocol + + "//" + + window.location.host + + roomUrl + + (query ? "?" + query : "") + //use urlParams because the token param must be deleted + window.location.hash + ) + ); + urlManager.pushRoomIdToUrl(this._currentRoom); + } + //@deprecated + else if (connexionType === GameConnexionTypes.register) { const organizationMemberToken = urlManager.getOrganizationToken(); const data = await Axios.post(`${PUSHER_URL}/register`, { organizationMemberToken }).then( (res) => res.data diff --git a/front/src/Url/UrlManager.ts b/front/src/Url/UrlManager.ts index cb0e1ed0..b8a967c6 100644 --- a/front/src/Url/UrlManager.ts +++ b/front/src/Url/UrlManager.ts @@ -3,13 +3,16 @@ import { localUserStore } from "../Connexion/LocalUserStore"; export enum GameConnexionTypes { room = 1, - register, + register /*@deprecated*/, empty, unknown, jwt, login, + privateAccessToken, } +export const queryPrivateAccessToken = "privateAccessToken"; + //this class is responsible with analysing and editing the game's url class UrlManager { public getGameConnexionType(): GameConnexionTypes { @@ -19,8 +22,13 @@ class UrlManager { } else if (url === "/jwt") { return GameConnexionTypes.jwt; } else if (url.includes("_/") || url.includes("*/") || url.includes("@/")) { + if (window.location.search.includes(queryPrivateAccessToken)) { + return GameConnexionTypes.privateAccessToken; + } return GameConnexionTypes.room; - } else if (url.includes("register/")) { + } + //@deprecated register url will be replace by "?privateAccessToken=" + else if (url.includes("register/")) { return GameConnexionTypes.register; } else if (url === "/") { return GameConnexionTypes.empty; @@ -29,6 +37,17 @@ class UrlManager { } } + /** + * @return string + */ + get getPrivateAccessToken(): string | null { + const urlParams = new URLSearchParams(window.location.search.toString()); + return urlParams.get(queryPrivateAccessToken); + } + + /** + * @deprecated + */ public getOrganizationToken(): string | null { const match = /\/register\/(.+)/.exec(window.location.pathname.toString()); return match ? match[1] : null;