Skip to content

Commit

Permalink
feat: add isInitialized flag
Browse files Browse the repository at this point in the history
add isInitialized flag
  • Loading branch information
simeng-li committed Aug 20, 2024
1 parent 423e005 commit ae3ccd2
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 8 deletions.
6 changes: 3 additions & 3 deletions packages/rn-sample/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,18 @@ const endpoint = 'https://<your-logto-endpoint>';
const appId = '<your-app-id>';

const Content = () => {
const { signIn, signOut, client, isAuthenticated, getIdTokenClaims } = useLogto();
const { signIn, signOut, client, isAuthenticated, isInitialized, getIdTokenClaims } = useLogto();
const [claims, setClaims] = useState<IdTokenClaims>();

useEffect(() => {
const get = async () => {
setClaims(await getIdTokenClaims());
};

if (isAuthenticated) {
if (isInitialized && isAuthenticated) {
void get();
}
}, [isAuthenticated, getIdTokenClaims]);
}, [isAuthenticated, getIdTokenClaims, isInitialized]);

return (
<View style={styles.container}>
Expand Down
2 changes: 1 addition & 1 deletion packages/rn/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"publishConfig": {
"access": "public"
},
"version": "0.2.0",
"version": "0.3.0",
"type": "module",
"main": "./lib/index.js",
"types": "./lib/index.d.ts",
Expand Down
8 changes: 8 additions & 0 deletions packages/rn/src/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,13 @@ import type { LogtoClient } from './client';

export type LogtoContextProps = {
client: LogtoClient;
/**
* Indicates if the client is initialized.
*
* - `true`: The client is initialized, and the authentication state is fetched.
* - `false`: The client is not initialized.
*/
isInitialized: boolean;
isAuthenticated: boolean;
setIsAuthenticated: React.Dispatch<React.SetStateAction<boolean>>;
};
Expand All @@ -15,6 +22,7 @@ export const throwContextError = (): never => {
export const LogtoContext = createContext<LogtoContextProps>({
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
client: undefined!,
isInitialized: false,
isAuthenticated: false,
setIsAuthenticated: throwContextError,
});
8 changes: 6 additions & 2 deletions packages/rn/src/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { LogtoContext } from './context';
* `LogtoProvider` component to wrap the root component of the app.
*/
export const useLogto = () => {
const { client, isAuthenticated, setIsAuthenticated } = useContext(LogtoContext);
const { client, isAuthenticated, setIsAuthenticated, isInitialized } = useContext(LogtoContext);

useEffect(() => {
// This is required to handle the redirect from the browser on a web-based expo app
Expand Down Expand Up @@ -45,6 +45,10 @@ export const useLogto = () => {
* tokens are valid.
*/
isAuthenticated,
/**
* Indicates if the client is initialized and the authentication state is retrieved from the storage.
*/
isInitialized,
/** @see {@link LogtoClient.getRefreshToken} */
getRefreshToken: client.getRefreshToken.bind(client),
/** @see {@link LogtoClient.getAccessToken} */
Expand All @@ -66,7 +70,7 @@ export const useLogto = () => {
/** @see {@link LogtoClient.fetchUserInfo} */
fetchUserInfo: client.fetchUserInfo.bind(client),
}),
[client, isAuthenticated, signIn, signOut]
[client, isAuthenticated, isInitialized, signIn, signOut]
);

return memorized;
Expand Down
8 changes: 6 additions & 2 deletions packages/rn/src/provider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,26 @@ export type LogtoProviderProps = {
export const LogtoProvider = ({ config, children }: LogtoProviderProps) => {
const memorizedLogtoClient = useMemo(() => new LogtoClient(config), [config]);
const [isAuthenticated, setIsAuthenticated] = useState(false);
const [isInitialized, setIsInitialized] = useState(false);

useEffect(() => {
(async () => {
const isAuthenticated = await memorizedLogtoClient.isAuthenticated();

setIsAuthenticated(isAuthenticated);

// Mark the client as initialized.
setIsInitialized(true);
})();
}, [memorizedLogtoClient]);

const memorizedContextValue = useMemo(
() => ({
client: memorizedLogtoClient,
isAuthenticated,
isInitialized,
setIsAuthenticated,
}),
[memorizedLogtoClient, isAuthenticated]
[memorizedLogtoClient, isAuthenticated, isInitialized]
);

return <LogtoContext.Provider value={memorizedContextValue}>{children}</LogtoContext.Provider>;
Expand Down

0 comments on commit ae3ccd2

Please sign in to comment.