From 598c7412a250de5e74ac81ae38c83e81ad5ff57d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20N=C3=A9grier?= Date: Wed, 8 Dec 2021 14:46:23 +0100 Subject: [PATCH] When sending an invalid token, the HTTP API from the Pusher now returns a 401 instead of an HTTP 500. --- front/src/Connexion/Room.ts | 7 ++++--- pusher/src/Controller/MapController.ts | 15 ++++++++++----- 2 files changed, 14 insertions(+), 8 deletions(-) diff --git a/front/src/Connexion/Room.ts b/front/src/Connexion/Room.ts index 4b6d82a4..044d8d67 100644 --- a/front/src/Connexion/Room.ts +++ b/front/src/Connexion/Room.ts @@ -116,11 +116,12 @@ export class Room { this._contactPage = data.contactPage || CONTACT_URL; return new MapDetail(data.mapUrl, data.textures); } catch (e) { - console.error("Error => getMapDetail", e, e.response); - //TODO fix me and manage Error class - if (e.response?.data === "Token decrypted error") { + if (axios.isAxiosError(e) && e.response?.status == 401 && e.response?.data === "Token decrypted error") { + console.warn("JWT token sent could not be decrypted. Maybe it expired?"); localUserStore.setAuthToken(null); window.location.assign("/login"); + } else { + console.error("Error => getMapDetail", e, e.response); } throw e; } diff --git a/pusher/src/Controller/MapController.ts b/pusher/src/Controller/MapController.ts index 7f76ff9e..23eef566 100644 --- a/pusher/src/Controller/MapController.ts +++ b/pusher/src/Controller/MapController.ts @@ -8,6 +8,7 @@ import { isMapDetailsData, MapDetailsData } from "../Services/AdminApi/MapDetail import { socketManager } from "../Services/SocketManager"; import { AuthTokenData, jwtTokenManager } from "../Services/JWTTokenManager"; import { v4 } from "uuid"; +import { InvalidTokenError } from "./InvalidTokenError"; export class MapController extends BaseController { constructor(private App: TemplatedApp) { @@ -85,11 +86,15 @@ export class MapController extends BaseController { userId = authTokenData.identifier; console.info("JWT expire, but decoded", userId); } catch (e) { - // The token was not good, redirect user on login page - res.writeStatus("500"); - res.writeHeader("Access-Control-Allow-Origin", FRONT_URL); - res.end("Token decrypted error"); - return; + if (e instanceof InvalidTokenError) { + // The token was not good, redirect user on login page + res.writeStatus("401 Unauthorized"); + res.writeHeader("Access-Control-Allow-Origin", FRONT_URL); + res.end("Token decrypted error"); + return; + } else { + return this.errorToResponse(e, res); + } } } }