-
-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d23bb7c
commit 3d1861d
Showing
3 changed files
with
107 additions
and
2 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
// Copyright © 2024 Ory Corp | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
import { Session } from "@ory/client-fetch" | ||
import { useCallback, useEffect } from "react" | ||
import { create, useStore } from "zustand" | ||
import { subscribeWithSelector } from "zustand/middleware" | ||
import { useOryFlow } from "../context/flow-context" | ||
import { frontendClient } from "../util/client" | ||
|
||
type SessionStore = { | ||
setIsLoading: (loading: boolean) => void | ||
setSession: (session: Session) => void | ||
isLoading?: boolean | ||
session?: Session | ||
error: unknown | ||
setError: (error: unknown) => void | ||
} | ||
|
||
const sessionStore = create<SessionStore>()( | ||
subscribeWithSelector((set) => ({ | ||
isLoading: false, | ||
setIsLoading: (isLoading: boolean) => set({ isLoading }), | ||
session: undefined, | ||
setSession: (session: Session) => set({ session }), | ||
error: undefined, | ||
setError: (error: unknown) => set({ error }), | ||
})), | ||
) | ||
|
||
/** | ||
* A hook to get the current session from the Ory Network. | ||
* | ||
* Usage: | ||
* ```ts | ||
* const { session, error, isLoading } = useSession() | ||
* ``` | ||
* | ||
* @returns The current session, error and loading state. | ||
*/ | ||
export const useSession = () => { | ||
const { config } = useOryFlow() | ||
const store = useStore(sessionStore) | ||
|
||
const fetchSession = useCallback(async () => { | ||
const { session, isLoading, setSession, setIsLoading, setError } = | ||
sessionStore.getState() | ||
|
||
if (!!session || isLoading) { | ||
return | ||
} | ||
|
||
setIsLoading(true) | ||
|
||
try { | ||
const sessionData = await frontendClient(config.sdk.url).toSession() | ||
setSession(sessionData) | ||
} catch (e) { | ||
setError(e instanceof Error ? e.message : "Unknown error occurred") | ||
} finally { | ||
setIsLoading(false) | ||
} | ||
}, [config.sdk.url]) | ||
|
||
useEffect(() => { | ||
void fetchSession() | ||
}, [fetchSession]) | ||
|
||
return { | ||
session: store.session, | ||
error: store.error, | ||
isLoading: store.isLoading, | ||
} | ||
} |