Skip to content

Commit

Permalink
feat: raw id and access tokens
Browse files Browse the repository at this point in the history
  • Loading branch information
peterphanouvong committed Jan 22, 2024
1 parent adec5c9 commit d094900
Showing 1 changed file with 73 additions and 8 deletions.
81 changes: 73 additions & 8 deletions src/frontend/AuthProvider.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -45,19 +45,16 @@ const tokenFetcher = async (url) => {
throw new Error('Failed to fetch token');
}
};

/**
*
* @param {{children: import('react').ReactNode, options?: {apiPath: string} | undefined} props
* @param {children: import('react').ReactNode, options?: {apiPath: string} | undefined} props
* @returns
*/
export const KindeProvider = ({children, options}) => {
export const KindeProvider = ({children}) => {
const [state, setState] = useState({
...config.initialState
});

config.apiPath = options?.apiPath ? options.apiPath : '/api/auth';

const setupUrl = `${config.apiPath}/setup`;

// try and get the user (by fetching /api/auth/setup) -> this needs to do the OAuth stuff
Expand All @@ -75,12 +72,16 @@ export const KindeProvider = ({children, options}) => {
organization,
permissions,
user,
userOrganizations
userOrganizations,
idTokenRaw
} = tokens;

const getAccessToken = () => accessToken;
const getAccessTokenRaw = () => accessTokenEncoded;
const getAccessTokenEncoded = () => accessTokenEncoded;
const getToken = () => accessTokenEncoded;
const getIdToken = () => idToken;
const getIdTokenRaw = () => idTokenRaw;
const getPermissions = () => permissions;
const getOrganization = () => organization;
const getUser = () => user;
Expand Down Expand Up @@ -197,17 +198,24 @@ export const KindeProvider = ({children, options}) => {
setState((previous) => ({
...previous,
accessToken,
accessTokenEncoded,
accessTokenRaw: accessTokenEncoded,
idToken,
idTokenRaw,
idTokenEncoded: idTokenRaw,
isLoading: false,
organization,
permissions,
user,
userOrganizations,
getAccessToken,
getAccessTokenRaw,
getAccessTokenEncoded,
getBooleanFlag,
getClaim,
getFlag,
getIdToken,
getIdTokenRaw,
getIntegerFlag,
getOrganization,
getPermission,
Expand Down Expand Up @@ -237,11 +245,68 @@ export const KindeProvider = ({children, options}) => {
}
}, [state.user]);

// provide this stuff to the rest of your app
const {
user,
accessToken,
accessTokenRaw,
accessTokenEncoded,
idToken,
idTokenEncoded,
idTokenRaw,
getAccessToken,
getAccessTokenRaw,
getIdTokenRaw,
getToken,
getClaim,
getFlag,
getIdToken,
getBooleanFlag,
getStringFlag,
getIntegerFlag,
getOrganization,
getPermission,
getPermissions,
getUser,
getUserOrganizations,
permissions,
organization,
userOrganizations,
error,
isLoading
} = state;

return (
<AuthContext.Provider
value={{
...state,
isAuthenticated: !!state.user
user,
error,
accessToken,
idToken,
accessTokenEncoded,
accessTokenRaw,
idTokenEncoded,
idTokenRaw,
getAccessToken,
getAccessTokenRaw,
getToken,
getClaim,
getFlag,
getIdToken,
getIdTokenRaw,
getBooleanFlag,
getStringFlag,
getIntegerFlag,
getOrganization,
getPermission,
getPermissions,
getUser,
getUserOrganizations,
permissions,
organization,
userOrganizations,
isLoading,
isAuthenticated: !!user
}}
>
{children}
Expand Down

0 comments on commit d094900

Please sign in to comment.