silly-vegetable-17473
02/28/2024, 12:13 PMchilly-king-10285
02/28/2024, 12:13 PMchilly-king-10285
02/28/2024, 12:14 PMGET /oauth2/sessions/logout
This endpoint supports both OpenID Connect Front- and Back-channel Enabled Logout. The back-channel logout is performed asynchronously and does not affect the logout flow. This operation does not require authentication.
For more details on OpenID Connect Front- and Back-channel logout, you can refer to the official OpenID specifications:
- [Front-channel Logout](https://openid.net/specs/openid-connect-frontchannel-1_0.html)
- [Back-channel Logout](https://openid.net/specs/openid-connect-backchannel-1_0.html)
(Source: [Hydra API Reference](https://www.ory.sh/docs/hydra/reference/api))
### Using Ory Kratos
For Ory Kratos, there are different approaches depending on the type of application you are working with (e.g., server-rendered browser applications, single-page applications (SPA), or native API applications).
#### Server-rendered Browser Applications
You can generate a logout URL for a given Ory Session cookie and open the logout URL in the browser. Here's an example using Express:
typescript
import { Configuration, FrontendApi } from "@ory/client"
import { Request, Response } from "express"
const frontend = new FrontendApi(
new Configuration({
basePath: "<https://playground.projects.oryapis.com/>",
}),
)
const route = (req: Request, res: Response) => {
frontend
.createBrowserLogoutFlow({ cookie: req.header("cookie") })
.then(({ data }) => {
console.log(data.logout_url) // The logout URL
console.log(data.logout_token) // The logout token
// You can render the logout URL like so:
// <a href="{{data.logout_url}}>Logout</a>
// Or call the logout token:
// kratos.updateLogoutFlow(data.logout_token).then(() => {
// Logged out
// })
})
}
(Source: [Kratos Self-service Flows - User logout](https://www.ory.sh/docs/kratos/self-service/flows/user-logout))
#### Single Page Application (SPA)
For SPAs, you can call the logout URL using an AJAX request. Here's an example using React:chilly-king-10285
02/28/2024, 12:14 PMGET /oauth2/sessions/logout
This endpoint supports both OpenID Connect Front- and Back-channel Enabled Logout. The back-channel logout is performed asynchronously and does not affect the logout flow. This operation does not require authentication. For more details on OpenID Connect Front- and Back-channel logout, you can refer to the official OpenID specifications: - [Front-channel Logout](https://openid.net/specs/openid-connect-frontchannel-1_0.html) - [Back-channel Logout](https://openid.net/specs/openid-connect-backchannel-1_0.html) (Source: [Hydra API Reference](https://www.ory.sh/docs/hydra/reference/api)) ### Using Ory Kratos For Ory Kratos, there are different approaches depending on the type of application you are working with (e.g., server-rendered browser applications, single-page applications (SPA), or native API applications). #### Server-rendered Browser Applications You can generate a logout URL for a given Ory Session cookie and open the logout URL in the browser. Here's an example using Express: typescript import { Configuration, FrontendApi } from "@ory/client" import { Request, Response } from "express" const frontend = new FrontendApi( new Configuration({ basePath: "<https://playground.projects.oryapis.com/>", }), ) const route = (req: Request, res: Response) => { frontend .createBrowserLogoutFlow({ cookie: req.header("cookie") }) .then(({ data }) => { console.log(data.logout_url) // The logout URL console.log(data.logout_token) // The logout token // You can render the logout URL like so: // <a href="{{data.logout_url}}>Logout</a> // Or call the logout token: // kratos.updateLogoutFlow(data.logout_token).then(() => { // Logged out // }) }) }
(Source: [Kratos Self-service Flows - User logout](https://www.ory.sh/docs/kratos/self-service/flows/user-logout)) #### Single Page Application (SPA) For SPAs, you can call the logout URL using an AJAX request. Here's an example using React: typescript import { Configuration, FrontendApi } from "@ory/client" const frontend = new FrontendApi( new Configuration({ basePath: "<http://localhost:4000>", // Use your local Ory Tunnel URL baseOptions: { withCredentials: true, }, }), ) export function Logout() { const handleLogout = async () => { try { // Create a "logout flow" in Ory Identities const { data: flow } = await frontend.createBrowserLogoutFlow() // Use the received token to "update" the flow and thus perform the logout await frontend.updateLogoutFlow({ token: flow.logout_token, }) } catch (error) { // The user could not be logged out } // Logout was successful } return <button onClick={handleLogout}>Logout</button> }
(Source: [Kratos Self-service Flows - User logout for SPA](https://www.ory.sh/docs/kratos/self-service/flows/user-logout#single-page-applicationsilly-vegetable-17473
02/28/2024, 12:24 PMchilly-king-10285
02/28/2024, 12:24 PMchilly-king-10285
02/28/2024, 12:25 PM