Skip to content

Commit 7687c31

Browse files
authored
feat: add isInitialized flag (#30)
* feat: add isInitialized flag add isInitialized flag * fix: update workflow update ci workflow
1 parent 423e005 commit 7687c31

File tree

7 files changed

+27
-11
lines changed

7 files changed

+27
-11
lines changed

.github/workflows/commitlint.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ on:
44
pull_request:
55
types: [opened, edited, synchronize, reopened]
66

7-
concurrency:
7+
concurrency:
88
group: commitlint-${{ github.ref }}
99
cancel-in-progress: true
1010

@@ -17,7 +17,7 @@ jobs:
1717
fetch-depth: 0
1818

1919
- name: Setup Node and pnpm
20-
uses: silverhand-io/actions-node-pnpm-run-steps@v4
20+
uses: silverhand-io/actions-node-pnpm-run-steps@v5
2121
with:
2222
pnpm-version: 9
2323

.github/workflows/main.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ jobs:
1919
- uses: actions/checkout@v4
2020

2121
- name: Setup Node and pnpm
22-
uses: silverhand-io/actions-node-pnpm-run-steps@v4
22+
uses: silverhand-io/actions-node-pnpm-run-steps@v5
2323
with:
2424
pnpm-version: 9
2525

packages/rn-sample/App.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,18 @@ const endpoint = 'https://<your-logto-endpoint>';
99
const appId = '<your-app-id>';
1010

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

1515
useEffect(() => {
1616
const get = async () => {
1717
setClaims(await getIdTokenClaims());
1818
};
1919

20-
if (isAuthenticated) {
20+
if (isInitialized && isAuthenticated) {
2121
void get();
2222
}
23-
}, [isAuthenticated, getIdTokenClaims]);
23+
}, [isAuthenticated, getIdTokenClaims, isInitialized]);
2424

2525
return (
2626
<View style={styles.container}>

packages/rn/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"publishConfig": {
44
"access": "public"
55
},
6-
"version": "0.2.0",
6+
"version": "0.3.0",
77
"type": "module",
88
"main": "./lib/index.js",
99
"types": "./lib/index.d.ts",

packages/rn/src/context.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,13 @@ import type { LogtoClient } from './client';
44

55
export type LogtoContextProps = {
66
client: LogtoClient;
7+
/**
8+
* Indicates if the client is initialized.
9+
*
10+
* - `true`: The client is initialized, and the authentication state is fetched.
11+
* - `false`: The client is not initialized.
12+
*/
13+
isInitialized: boolean;
714
isAuthenticated: boolean;
815
setIsAuthenticated: React.Dispatch<React.SetStateAction<boolean>>;
916
};
@@ -15,6 +22,7 @@ export const throwContextError = (): never => {
1522
export const LogtoContext = createContext<LogtoContextProps>({
1623
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
1724
client: undefined!,
25+
isInitialized: false,
1826
isAuthenticated: false,
1927
setIsAuthenticated: throwContextError,
2028
});

packages/rn/src/hooks.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import { LogtoContext } from './context';
1111
* `LogtoProvider` component to wrap the root component of the app.
1212
*/
1313
export const useLogto = () => {
14-
const { client, isAuthenticated, setIsAuthenticated } = useContext(LogtoContext);
14+
const { client, isAuthenticated, setIsAuthenticated, isInitialized } = useContext(LogtoContext);
1515

1616
useEffect(() => {
1717
// This is required to handle the redirect from the browser on a web-based expo app
@@ -45,6 +45,10 @@ export const useLogto = () => {
4545
* tokens are valid.
4646
*/
4747
isAuthenticated,
48+
/**
49+
* Indicates if the client is initialized and the authentication state is retrieved from the storage.
50+
*/
51+
isInitialized,
4852
/** @see {@link LogtoClient.getRefreshToken} */
4953
getRefreshToken: client.getRefreshToken.bind(client),
5054
/** @see {@link LogtoClient.getAccessToken} */
@@ -66,7 +70,7 @@ export const useLogto = () => {
6670
/** @see {@link LogtoClient.fetchUserInfo} */
6771
fetchUserInfo: client.fetchUserInfo.bind(client),
6872
}),
69-
[client, isAuthenticated, signIn, signOut]
73+
[client, isAuthenticated, isInitialized, signIn, signOut]
7074
);
7175

7276
return memorized;

packages/rn/src/provider.tsx

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,22 +16,26 @@ export type LogtoProviderProps = {
1616
export const LogtoProvider = ({ config, children }: LogtoProviderProps) => {
1717
const memorizedLogtoClient = useMemo(() => new LogtoClient(config), [config]);
1818
const [isAuthenticated, setIsAuthenticated] = useState(false);
19+
const [isInitialized, setIsInitialized] = useState(false);
1920

2021
useEffect(() => {
2122
(async () => {
2223
const isAuthenticated = await memorizedLogtoClient.isAuthenticated();
23-
2424
setIsAuthenticated(isAuthenticated);
25+
26+
// Mark the client as initialized.
27+
setIsInitialized(true);
2528
})();
2629
}, [memorizedLogtoClient]);
2730

2831
const memorizedContextValue = useMemo(
2932
() => ({
3033
client: memorizedLogtoClient,
3134
isAuthenticated,
35+
isInitialized,
3236
setIsAuthenticated,
3337
}),
34-
[memorizedLogtoClient, isAuthenticated]
38+
[memorizedLogtoClient, isAuthenticated, isInitialized]
3539
);
3640

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

0 commit comments

Comments
 (0)