Skip to content
This repository has been archived by the owner on Aug 22, 2024. It is now read-only.

Commit

Permalink
Merge pull request #62 from elseu/feature/initial-data
Browse files Browse the repository at this point in the history
Initial data
  • Loading branch information
tpdewolf authored Mar 20, 2023
2 parents 0b9e5d4 + 69a9815 commit 7927d5c
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 21 deletions.
21 changes: 20 additions & 1 deletion src/OidcJwtProvider.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
import React, { createContext, useContext, useEffect, useRef, useState } from 'react';
import { StoreApi, UseBoundStore, useStore } from 'zustand';

import {
CSRF_TOKEN_STORAGE_KEY,
LOGGED_IN_TOKEN_STORAGE_KEY,
RETRY_LOGIN_STORAGE_KEY,
USER_INFO_TOKEN_STORAGE_KEY,
} from './constants';
import { Storage } from './storage';
import { createOidcJwtClientStore } from './store';
import { OidcJwtClientStore, OidcJwtProviderProps } from './types';
import { AuthState, OidcJwtClientStore, OidcJwtProviderProps } from './types';
import { removeTokenFromUrl } from './utils';

const OidcJwtContext = createContext<UseBoundStore<StoreApi<OidcJwtClientStore>> | undefined>(
Expand Down Expand Up @@ -78,6 +85,18 @@ const OidcJwtProvider: React.FC<React.PropsWithChildren<OidcJwtProviderProps>> =
const { client, removeTokenFromUrlFunction = removeTokenFromUrl, children } = props;
const store = useRef(createOidcJwtClientStore(client, removeTokenFromUrlFunction)).current;

useEffect(() => {
const initialState: AuthState = {
userInfo: Storage.get(USER_INFO_TOKEN_STORAGE_KEY),
csrfToken: Storage.get(CSRF_TOKEN_STORAGE_KEY),
isLoggedIn: !!Storage.get(LOGGED_IN_TOKEN_STORAGE_KEY),
isInitialized: !client,
didRetryLogin: Storage.get(RETRY_LOGIN_STORAGE_KEY) === 1,
};

store.setState({ authState: initialState });
}, [client, store]);

return (
<OidcJwtContext.Provider value={store}>
<OidcJwtInitializer {...props}>{children}</OidcJwtInitializer>
Expand Down
33 changes: 13 additions & 20 deletions src/store.ts
Original file line number Diff line number Diff line change
@@ -1,40 +1,33 @@
import { create } from 'zustand';

import {
CSRF_TOKEN_STORAGE_KEY,
LOGGED_IN_TOKEN_STORAGE_KEY,
RETRY_LOGIN_STORAGE_KEY,
USER_INFO_TOKEN_STORAGE_KEY
} from './constants';
import { Storage } from './storage';
import { OidcJwtClientOptions, OidcJwtClientStore } from './types';
import { AuthState, OidcJwtClientOptions, OidcJwtClientStore } from './types';
import { AuthService } from './utils/AuthService';

function createOidcJwtClientStore(
client: OidcJwtClientOptions | false,
removeTokenFromUrlFunction?: (url: string) => void
removeTokenFromUrlFunction?: (url: string) => void,
) {
return create<OidcJwtClientStore>(set => {
const initialState = {
userInfo: Storage.get(USER_INFO_TOKEN_STORAGE_KEY),
csrfToken: Storage.get(CSRF_TOKEN_STORAGE_KEY),
isLoggedIn: !!Storage.get(LOGGED_IN_TOKEN_STORAGE_KEY),
return create<OidcJwtClientStore>((set) => {
const initialState: AuthState = {
userInfo: undefined,
csrfToken: null,
isLoggedIn: false,
isInitialized: !client,
didRetryLogin: Storage.get(RETRY_LOGIN_STORAGE_KEY) === 1,
didRetryLogin: false,
};

const service = client
? new AuthService({
client,
removeTokenFromUrlFunction,
state: initialState,
})
client,
removeTokenFromUrlFunction,
state: initialState,
})
: null;

return {
service,
authState: initialState,
setState: newState => set({ authState: newState }),
setState: (newState) => set({ authState: newState }),
};
});
}
Expand Down

0 comments on commit 7927d5c

Please sign in to comment.