Created
March 6, 2024 03:48
-
-
Save jeremy-code/e6a9da1ae5bc1f5e5cc3cfc1fbadd190 to your computer and use it in GitHub Desktop.
getUser.ts for react-oidc-context
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { User } from "oidc-client-ts"; | |
const { VITE_OIDC_AUTHORITY, VITE_OIDC_CLIENT_ID } = import.meta.env; | |
const AUTH_KEY = `oidc.user:${VITE_OIDC_AUTHORITY}:${VITE_OIDC_CLIENT_ID}`; | |
// Get the user from local storage (used for API requests where hooks are not available) | |
export const getUser = () => { | |
const storageString = localStorage.getItem(AUTH_KEY); | |
if (!storageString) return null; | |
return User.fromStorageString(storageString); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment