diff --git a/packages/auth-provider/src/common/utilities.ts b/packages/auth-provider/src/common/utilities.ts index 5688f75..11b53be 100644 --- a/packages/auth-provider/src/common/utilities.ts +++ b/packages/auth-provider/src/common/utilities.ts @@ -3,6 +3,7 @@ import { AUTH_TYPES, HEADERS, JWT, + decodeToken, verifyAndExtractToken, } from "@versini/auth-common"; import { getFingerprintHash } from "@versini/ui-fingerprint"; @@ -13,6 +14,15 @@ import type { ServiceCallProps } from "./types"; const isProd = process.env.NODE_ENV === "production"; const isDev = !isProd; +export const getUserIdFromToken = (token: string) => { + try { + const jwt = decodeToken(token); + return jwt ? (jwt[JWT.USER_ID_KEY] as string) : ""; + } catch (_error) { + return ""; + } +}; + export const serviceCall = async ({ type, clientId, diff --git a/packages/auth-provider/src/components/AuthProvider/AuthProvider.tsx b/packages/auth-provider/src/components/AuthProvider/AuthProvider.tsx index 0ea120b..0d7de43 100644 --- a/packages/auth-provider/src/components/AuthProvider/AuthProvider.tsx +++ b/packages/auth-provider/src/components/AuthProvider/AuthProvider.tsx @@ -30,6 +30,7 @@ import { authenticateUser, getCustomFingerprint, getPreAuthCode, + getUserIdFromToken, graphQLCall, logoutUser, } from "../../common/utilities"; @@ -106,8 +107,14 @@ export const AuthProvider = ({ async (message: string) => { logger("invalidateAndLogout: invalidating and logging out"); const { user } = state; + const userId = user?.userId || getUserIdFromToken(idToken); + if (!userId) { + logger( + "invalidateAndLogout: user cannot be identified, logging out without userId", + ); + } await logoutUser({ - userId: user?.userId || "", + userId, idToken, accessToken, refreshToken,