narrow-hospital-35452
02/04/2025, 9:58 AMbrief-egg-61061
02/04/2025, 9:58 AMbrief-egg-61061
02/04/2025, 9:59 AM/sessions/whoami
endpoint or the corresponding SDK method. This endpoint provides information about the current session, including the last authentication time. Here are a few ways to do this: 1. Using cURL: You can make a request to the /sessions/whoami
endpoint using cURL: bash curl -H "Cookie: ory_kratos_session=..." \ "https://$<http://PROJECT_SLUG.projects.oryapis.com/sessions/whoami|PROJECT_SLUG.projects.oryapis.com/sessions/whoami>"
[Source: Kratos Session Management Overview](https://www.ory.sh/docs/kratos/session-management/overview#using-ory-session-cookie) 2. Using the Ory SDK in a React application: Here's an example of how to use the Ory SDK to check the session in a React component: javascript 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 .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> ) }
[Source: Kratos Bring-your-own-ui Integration Basics](https://www.ory.sh/docs/kratos/bring-your-own-ui/custom-ui-basic-integration#session-checks) The authenticated_at
field in the session response indicates when the user last logged in. Keep in mind that this information is only available for active sessions. If a user doesn't have an active session, you won't be able to retrieve their last login time using this method. For a more comprehensive history of user logins, you might need to implement additional logging or tracking mechanisms, as Ory Kratos doesn't provide built-in functionality to store and retrieve historical login data across multiple sessions.