-
-
Notifications
You must be signed in to change notification settings - Fork 46
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add getServerSession and SessionProvider
- Loading branch information
1 parent
df98c2b
commit 55ba82c
Showing
14 changed files
with
231 additions
and
26 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
"use client" | ||
// Copyright © 2024 Ory Corp | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
export { useSession } from "./useSession" | ||
export { SessionProvider, useSession } from "./session-provider" |
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,99 @@ | ||
"use client" | ||
import { Session } from "@ory/client-fetch" | ||
import { | ||
createContext, | ||
useCallback, | ||
useContext, | ||
useEffect, | ||
useState, | ||
} from "react" | ||
import { frontendClient } from "./frontendClient" | ||
|
||
type SessionData = | ||
| { | ||
session: Session | ||
state: "authenticated" | ||
} | ||
| { | ||
state: "unauthenticated" | ||
} | ||
| { | ||
state: "loading" | ||
} | ||
| { | ||
state: "error" | ||
error: Error | ||
} | ||
|
||
const SessionContext = createContext<SessionData>({ | ||
state: "loading", | ||
}) | ||
|
||
export function useSession() { | ||
if (!SessionContext) { | ||
throw new Error("[Ory/Elements] useSession must be used on the client") | ||
} | ||
const session = useContext(SessionContext) | ||
if (!session) { | ||
throw new Error( | ||
"[Ory/Elements] useSession must be used within a SessionProvider", | ||
) | ||
} | ||
return session | ||
} | ||
type SessionProviderProps = { | ||
session?: Session | null | ||
/** | ||
* The time in milliseconds after which the session should be considered stale. | ||
* If the session is stale, the session will be refreshed. | ||
* If set to Infinity, the session will never be refreshed. | ||
*/ | ||
staleTime?: number | ||
} & React.PropsWithChildren | ||
|
||
export function SessionProvider({ | ||
session: initialSession, | ||
staleTime = Infinity, | ||
children, | ||
}: SessionProviderProps) { | ||
const [lastSync, setLastSync] = useState<number>(0) | ||
const [session, setSession] = useState<SessionData>(() => { | ||
if (initialSession) { | ||
return { | ||
session: initialSession, | ||
state: initialSession.active ? "authenticated" : "unauthenticated", | ||
} | ||
} | ||
|
||
return { state: "unauthenticated" } | ||
}) | ||
|
||
const fetchSession = useCallback(async () => { | ||
try { | ||
const session = await frontendClient().toSession() | ||
setSession({ | ||
session, | ||
state: session.active ? "authenticated" : "unauthenticated", | ||
}) | ||
setLastSync(Date.now()) | ||
} catch (error) { | ||
setSession({ state: "error", error: error as Error }) | ||
} | ||
}, []) | ||
|
||
const isLoading = session.state === "loading" | ||
|
||
useEffect(() => { | ||
// If the session is already loaded or we're currently loading it, we don't need to fetch it again | ||
if (lastSync + staleTime > Date.now() || isLoading) { | ||
return | ||
} | ||
void fetchSession() | ||
}, [fetchSession, lastSync, staleTime, isLoading]) | ||
|
||
return ( | ||
<SessionContext.Provider value={session}> | ||
{children} | ||
</SessionContext.Provider> | ||
) | ||
} |
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
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,12 @@ | ||
import { Session } from "@ory/client-fetch" | ||
import { serverSideFrontendClient } from "./client" | ||
import { getCookieHeader } from "./utils" | ||
|
||
export async function getServerSession(): Promise<Session | null> { | ||
const cookie = await getCookieHeader() | ||
return serverSideFrontendClient | ||
.toSession({ | ||
cookie, | ||
}) | ||
.catch(() => null) | ||
} |
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,53 @@ | ||
// Copyright © 2024 Ory Corp | ||
// SPDX-License-Identifier: Apache-2.0 | ||
import { FlowType, SettingsFlow } from "@ory/client-fetch" | ||
|
||
import { initOverrides, QueryParams } from "../types" | ||
import { serverSideFrontendClient } from "./client" | ||
import { getFlow } from "./flow" | ||
import { toFlowParams } from "./utils" | ||
|
||
/** | ||
* Use this method in an app router page to fetch an existing login flow or to create a new one. This method works with server-side rendering. | ||
* | ||
* ``` | ||
* import { Login } from "@ory/elements-react/theme" | ||
* import { getLoginFlow, OryPageParams } from "@ory/nextjs/app" | ||
* import { enhanceConfig } from "@ory/nextjs" | ||
* | ||
* import config from "@/ory.config" | ||
* import CardHeader from "@/app/auth/login/card-header" | ||
* | ||
* export default async function LoginPage(props: OryPageParams) { | ||
* const flow = await getLoginFlow(props.searchParams) | ||
* | ||
* if (!flow) { | ||
* return null | ||
* } | ||
* | ||
* return ( | ||
* <Login | ||
* flow={flow} | ||
* config={enhanceConfig(config)} | ||
* components={{ | ||
* Card: { | ||
* Header: CardHeader, | ||
* }, | ||
* }} | ||
* /> | ||
* ) | ||
* } | ||
* ``` | ||
* | ||
* @param params - The query parameters of the request. | ||
*/ | ||
export async function getSettingsFlow( | ||
params: QueryParams | Promise<QueryParams>, | ||
): Promise<SettingsFlow | null | void> { | ||
const p = await toFlowParams(await params) | ||
return getFlow( | ||
await params, | ||
() => serverSideFrontendClient.getSettingsFlowRaw(p, initOverrides), | ||
FlowType.Settings, | ||
) | ||
} |
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
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
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