From b7eb2fd90aeff425cf18cf9ad8a4e671be698650 Mon Sep 17 00:00:00 2001 From: Leonardo Christino <leomilho@gmail.com> Date: Tue, 24 Oct 2023 22:44:11 +0200 Subject: [PATCH] fix(auth): move to username only only surfacing username, and not userID, and username should be shown in UI --- apps/web/src/components/navbar/navbar.tsx | 4 ++-- .../lib/data-access/authorization/useAuth.tsx | 13 +++++-------- .../backend-message-receiver/WebSocketHandler.tsx | 1 - libs/shared/lib/data-access/store/authSlice.ts | 8 ++++---- 4 files changed, 11 insertions(+), 15 deletions(-) diff --git a/apps/web/src/components/navbar/navbar.tsx b/apps/web/src/components/navbar/navbar.tsx index 0042a5619..76534f601 100644 --- a/apps/web/src/components/navbar/navbar.tsx +++ b/apps/web/src/components/navbar/navbar.tsx @@ -214,7 +214,7 @@ export const Navbar = (props: NavbarComponentProps) => { {auth.authorized ? ( <> <div className="menu-title"> - <h2>user: {auth.userID}</h2> + <h2>user: {auth.username}</h2> <h3 className="text-xs">session: {auth.sessionID}</h3> </div> <li> @@ -285,7 +285,7 @@ export const Navbar = (props: NavbarComponentProps) => { ) : ( <> <div className="menu-title"> - <h2>user: {auth.userID}</h2> + <h2>user: {auth.username}</h2> <h3 className="text-xs">session: {auth.sessionID}</h3> </div> <div> diff --git a/libs/shared/lib/data-access/authorization/useAuth.tsx b/libs/shared/lib/data-access/authorization/useAuth.tsx index c4c64b8f5..a2a185421 100644 --- a/libs/shared/lib/data-access/authorization/useAuth.tsx +++ b/libs/shared/lib/data-access/authorization/useAuth.tsx @@ -5,10 +5,8 @@ import { useAppDispatch, useAuthorizationCache } from '../store'; import { authorized } from '../store/authSlice'; export type AuthenticationHeader = { - sessionData: { - userID: string; - sessionID: string; - }; + username: string; + sessionID: string; jwt: string; }; @@ -30,7 +28,7 @@ export const useAuth = () => { console.warn('skipping login'); dispatch( authorized({ - userID: 'UserID', + username: 'UserID', sessionID: 'SessionID', jwt: 'jwt', authorized: true, @@ -50,8 +48,8 @@ export const useAuth = () => { .then((res: AuthenticationHeader) => { dispatch( authorized({ - userID: res.sessionData.userID, - sessionID: res.sessionData.sessionID, + username: res.username, + sessionID: res.sessionID, jwt: res.jwt, authorized: true, }) @@ -70,7 +68,6 @@ export const useAuth = () => { init.method = init.method || 'GET'; init.headers = { 'Content-Type': 'application/json', - // Userid: auth.userID || '', sessionid: auth.sessionID || '', // Authorization: `Bearer ${auth.jwt}`, ...init.headers, diff --git a/libs/shared/lib/data-access/socket/backend-message-receiver/WebSocketHandler.tsx b/libs/shared/lib/data-access/socket/backend-message-receiver/WebSocketHandler.tsx index 203e56bbb..d457018c0 100644 --- a/libs/shared/lib/data-access/socket/backend-message-receiver/WebSocketHandler.tsx +++ b/libs/shared/lib/data-access/socket/backend-message-receiver/WebSocketHandler.tsx @@ -37,7 +37,6 @@ export class WebSocketHandler implements BackendMessageReceiver { const params = new URLSearchParams(); if (this.authHeader?.sessionID) params.set('sessionID', this.authHeader?.sessionID ?? ''); - if (this.authHeader?.userID) params.set('userID', this.authHeader?.userID ?? ''); if (this.authHeader?.jwt) params.set('jwt', this.authHeader?.jwt ?? ''); this.webSocket = new WebSocket(this.url + '?' + params.toString()); this.webSocket.onopen = () => onOpen(); diff --git a/libs/shared/lib/data-access/store/authSlice.ts b/libs/shared/lib/data-access/store/authSlice.ts index 663ce51a8..13b1bfbad 100644 --- a/libs/shared/lib/data-access/store/authSlice.ts +++ b/libs/shared/lib/data-access/store/authSlice.ts @@ -5,7 +5,7 @@ export type UseIsAuthorizedState = { authorized: boolean | undefined; jwt: string | undefined; sessionID: string | undefined; - userID: string | undefined; + username: string | undefined; }; // Define the initial state using that type @@ -13,7 +13,7 @@ export const initialState: UseIsAuthorizedState = { authorized: undefined, jwt: undefined, sessionID: undefined, - userID: undefined, + username: undefined, }; export const authSlice = createSlice({ @@ -26,14 +26,14 @@ export const authSlice = createSlice({ state.authorized = action.payload.authorized; state.jwt = action.payload.jwt; state.sessionID = action.payload.sessionID; - state.userID = action.payload.userID; + state.username = action.payload.username; }, logout(state) { console.info('Logging out'); state.authorized = undefined; state.jwt = undefined; state.sessionID = undefined; - state.userID = undefined; + state.username = undefined; }, unauthorized(state) { console.warn('Unauthorized'); -- GitLab