narrow-van-43826
07/28/2023, 6:28 AMchilly-king-10285
07/28/2023, 6:28 AMchilly-king-10285
07/28/2023, 6:28 AMjavascript 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 }) }, []) return session ? ( <table> <tr> <th>Session ID</th> <th>Expires at</th> <th>Authenticated at</th> </tr> <tr id={session.id}> <td>{session.id}</td> <td>{session.expires_at || ""}</td> <td>{session.authenticated_at || ""}</td> </tr> </table> ) : ( <div>Loading session data...</div> ) }
This code creates a new instance of the FrontendApi from the Ory SDK, and uses it to send a request to the /sessions/whoami
endpoint. The response from this endpoint contains the session data, which is then stored in a state variable. The session data includes the session ID, the time the session expires, and the time the session was authenticated. Please replace "<http://localhost:4000>"
with your local Ory Tunnel URL. Source: [Ory Documentation](https://www.ory.sh/docs/kratos/session-management/overview)