Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add isInitialized flag #30

Merged
merged 2 commits into from
Aug 20, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading