<@U04UQ68083H> how do i implement logout with self...
# general
e
@chilly-king-10285 how do i implement logout with self-service session?
c
A message from kapa.ai
To implement logout with Ory’s self-service session/logout flow, you use different endpoints depending on your app type. --- ### 1. Server‑rendered browser apps Flow: 1. Create a browser logout flow (gets a logout URL tied to the current session cookie). 2. Redirect the user’s browser to that logout URL. Example (Express): [[Sign out](https://www.ory.com/docs/identities/get-started/sign-out)]
Copy code
ts // /logout route app.get("/logout", async (req, res) => {   try {     const { logout_url } = await ory.createBrowserLogoutFlow({       cookie: req.header("cookie"),     })     res.redirect(logout_url)   } catch (err) {     res.redirect("/")   } })
Under the hood this calls: -
GET /self-service/logout/browser
to create the logout URL [[createBrowserLogoutFlow](https://www.ory.sh/docs/reference/api)] - Then the browser follows that URL, which hits
GET /self-service/logout
to complete logout [[updateLogoutFlow](https://www.ory.sh/docs/reference/api)] After successful logout, Ory: - Invalidates the session - Removes the session cookie - Redirects to
return_to
or the configured default post‑logout URL [[Sign out](https://www.ory.com/docs/identities/get-started/sign-out); [Logout flow](https://www.ory.com/docs/kratos/self-service/flows/user-logout)] --- ### 2. SPA (single‑page application) Flow: 1. Call
createBrowserLogoutFlow
from the browser (with credentials). 2. Call
updateLogoutFlow
with the returned
logout_token
. Example (React): [[SPA logout](https://www.ory.com/docs/kratos/self-service/flows/user-logout#single-page-application-spa)]
Copy code
ts import { Configuration, FrontendApi } from "@ory/client"  const frontend = new FrontendApi(   new Configuration({     basePath: "<http://localhost:4000>", // Ory Tunnel / project URL     baseOptions: { withCredentials: true },   }), )  export function Logout() {   const handleLogout = async () => {     try {       const { data: flow } = await frontend.createBrowserLogoutFlow()       await frontend.updateLogoutFlow({ token: flow.logout_token })     } catch (error) {       // handle error     }   }    return <button onClick={handleLogout}>Logout</button> }
--- ### 3. Native / API clients (mobile, backend using session tokens) Use the native logout endpoint directly with the session token: -
DELETE /self-service/logout/api
with JSON body
{ "session_token": "<token>" }
[[performNativeLogout](https://www.ory.sh/docs/reference/api); [Native logout](https://www.ory.com/docs/kratos/self-service/flows/user-logout#native-api-applications)] TypeScript example:
Copy code
ts 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 logout(token: string) {   return await frontend.performNativeLogout({     performNativeLogoutBody: {       session_token: token,     },   }) }
This revokes the session token but does not touch browser cookies (so it’s for non‑browser/native use). [[performNativeLogout](https://www.ory.sh/docs/reference/api)] --- If you tell me your app type (SSR, SPA, mobile, etc.), I can point you to the exact pattern you should use.
e
@chilly-king-10285, how do i spa logout wihtout using the SDK and just native requests to logout
c
A message from kapa.ai
You can do SPA logout with plain HTTP calls against the browser logout endpoints; the SDK is just a thin wrapper around these. For SPAs, the flow is: 1. Create a browser logout flow (gets
logout_token
and
logout_url
). 2. Complete the logout by calling the logout URL (or
/self-service/logout?token=...
). 3. Use
credentials: "include"
so cookies (session, CSRF) are sent. [[Logout flow](https://www.ory.com/docs/kratos/bring-your-own-ui/custom-ui-basic-integration#logout-flow); [SPA flows](https://www.ory.com/docs/kratos/self-service#browser-flows-for-client-side-apps-single-page-apps-reactjs-angular-nextjs-)] ### 1. Create logout flow
Copy code
js // Step 1: Get logout_url and logout_token const res = await fetch(   "https://$<http://PROJECT_SLUG.projects.oryapis.com/self-service/logout/browser|PROJECT_SLUG.projects.oryapis.com/self-service/logout/browser>",   {     method: "GET",     headers: {       "Accept": "application/json",     },     credentials: "include", // important for cookies   }, )  if (!res.ok) {   // handle error (e.g. user not logged in) }  const flow = await res.json() // flow.logout_url // flow.logout_token
This corresponds to
GET /self-service/logout/browser
and returns JSON like: [[Logout flow](https://www.ory.com/docs/kratos/bring-your-own-ui/custom-ui-basic-integration#logout-flow)]
Copy code
json {   "logout_url": "https://$<http://PROJECT_SLUG.projects.oryapis.com/self-service/logout?token=J6yIQf7ABx1BBiPOj036dJKSQmKwGnX6|PROJECT_SLUG.projects.oryapis.com/self-service/logout?token=J6yIQf7ABx1BBiPOj036dJKSQmKwGnX6>",   "logout_token": "J6yIQf7ABx1BBiPOj036dJKSQmKwGnX6" }
### 2. Complete logout (AJAX style) Instead of redirecting, call the logout URL via `fetch`:
Copy code
js // Step 2: Call the logout URL (or build it from the token) await fetch(flow.logout_url, {   method: "GET",   headers: {     "Accept": "application/json",   },   credentials: "include", })  // On success, Ory invalidates the session and clears the cookie. // If Accept != application/json, this would be a 303 redirect instead.
This hits
GET /self-service/logout?token=...
(the
updateLogoutFlow
endpoint). [[Logout flow](https://www.ory.com/docs/kratos/bring-your-own-ui/custom-ui-basic-integration#logout-flow); [updateLogoutFlow](https://www.ory.sh/docs/reference/api)] That’s all you need for SPA logout without the SDK: two
fetch
calls with
credentials: "include"
.
e
@chilly-king-10285 will this log me out from other applications using self-service session
c
A message from kapa.ai
No, the standard self-service logout flow only terminates the current Ory Identities session (the one associated with the browser’s session cookie or session token). It does not automatically revoke or log out: - Other active sessions of the same user (e.g. other devices/browsers) - Sessions in other applications that maintain their own sessions The docs explicitly distinguish: - Self-service logout: terminates the current session only. [[Logout flow](https://www.ory.com/docs/kratos/self-service/flows/user-logout)] - Revoke specific / other sessions: separate self-service APIs to revoke other sessions: - Revoke a specific session:
disableMySession
[[Revoke specific session](https://www.ory.com/docs/kratos/session-management/list-revoke-get-sessions#revoking-a-specific-session)] - Revoke all other sessions:
disableMyOtherSessions
[[Revoke all other sessions](https://www.ory.com/docs/kratos/session-management/list-revoke-get-sessions#revoking-all-other-sessions)] If you want “log out from everywhere” behavior, you need to call those revoke-session APIs (or use the
revoke_active_sessions
Action hook on login) in addition to the normal self-service logout. [[One active session](https://www.ory.com/docs/actions/revoke-active-sessions)]