Refactor access by token

Signed-off-by: Gregoire Parant <g.parant@thecodingmachine.com>
This commit is contained in:
Gregoire Parant 2022-03-19 07:11:15 +01:00
parent 56fb73c682
commit 29a0b9c5ae
2 changed files with 6 additions and 54 deletions

View File

@ -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, queryPrivateAccessToken, urlManager } from "../Url/UrlManager";
import { GameConnexionTypes, urlManager } from "../Url/UrlManager";
import { localUserStore } from "./LocalUserStore";
import { CharacterTexture, LocalUser } from "./LocalUser";
import { Room } from "./Room";
@ -128,42 +128,6 @@ class ConnectionManager {
return Promise.reject(new Error("You will be redirect on login page"));
}
urlManager.pushRoomIdToUrl(this._currentRoom);
} else if (connexionType === GameConnexionTypes.privateAccessToken) {
const organizationMemberToken = urlManager.privateAccessToken;
//clear queryPrivateAccessToken query in window location
urlParams.delete(queryPrivateAccessToken);
//create play uri parameter
const playUri = window.location.protocol + "//" + window.location.host + window.location.pathname;
const data = await Axios.post(`${PUSHER_URL}/register`, {
organizationMemberToken,
playUri,
}).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.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) {

View File

@ -6,28 +6,24 @@ export enum GameConnexionTypes {
register /*@deprecated*/,
empty,
unknown,
jwt,
jwt /*@deprecated*/,
login,
privateAccessToken,
}
export const queryPrivateAccessToken = "privateAccessToken";
//this class is responsible with analysing and editing the game's url
class UrlManager {
public getGameConnexionType(): GameConnexionTypes {
const url = window.location.pathname.toString();
if (url === "/login") {
return GameConnexionTypes.login;
} else if (url === "/jwt") {
}
//@deprecated jwt url will be replace by "?token=<private access token>"
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;
}
//@deprecated register url will be replace by "?privateAccessToken=<private access token>"
//@deprecated register url will be replace by "?token=<private access token>"
else if (url.includes("register/")) {
return GameConnexionTypes.register;
} else if (url === "/") {
@ -37,14 +33,6 @@ class UrlManager {
}
}
/**
* @return string
*/
get privateAccessToken(): string | null {
const urlParams = new URLSearchParams(window.location.search.toString());
return urlParams.get(queryPrivateAccessToken);
}
/**
* @deprecated
*/