Skip to content

Instantly share code, notes, and snippets.

@jeremy-code
Created March 6, 2024 03:48
Show Gist options
  • Save jeremy-code/e6a9da1ae5bc1f5e5cc3cfc1fbadd190 to your computer and use it in GitHub Desktop.
Save jeremy-code/e6a9da1ae5bc1f5e5cc3cfc1fbadd190 to your computer and use it in GitHub Desktop.
getUser.ts for react-oidc-context
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