ancient-dentist-17854
04/17/2025, 4:57 AMbrief-egg-61061
04/17/2025, 4:57 AMbrief-egg-61061
04/17/2025, 4:58 AMtypescript import { Configuration, FrontendApi, Session } from "@ory/client" import { useEffect, useState } from "react" const frontend = new FrontendApi( new Configuration({ basePath: "<http://localhost:4000>", // Use your local Ory Tunnel URL baseOptions: { withCredentials: true, }, }), ) export function checkSession() { const [session, setSession] = useState<Session>(undefined) useEffect(() => { frontend // the cookie is automatically sent with the request .toSession() .then(({ data: session }) => { setSession(session) }) .catch((error) => { // The session could not be fetched // This might occur if the current session has expired }) }, []) // Use session data here }
2. For native applications, you can validate sessions using session tokens: typescript import { Configuration, FrontendApi } from "@ory/client" const frontend = new FrontendApi( new Configuration({ basePath: `https://${process.env.ORY_PROJECT_SLUG}.<http://projects.oryapis.com|projects.oryapis.com>`, }), ) export async function checkSession(sessionId: string, token: string) { return await frontend.toSession({ xSessionToken: token, }) }
3. You can also request a tokenized version of the session (like a JWT) by using the tokenize_as
parameter: typescript // When using a token template, the token is included in the `tokenized` field of the session const session = await client.toSession("the-session-token", { tokenize_as: "example-jwt-template" }) console.log(session.tokenized) // The JWT
The tokenized field in the session response will contain the JWT that you can then use for validation purposes. [Ory Session Management Overview](https://www.ory.sh/docs/kratos/session-management/overview) [Ory API Reference](https://www.ory.sh/docs/reference/api) [Ory Proxy with JSON Web Tokens](https://www.ory.sh/docs/guides/cli/proxy-and-tunnel#use-ory-proxy-with-json-web-tokens)