delightful-garage-16669
09/21/2023, 9:24 AMchilly-king-10285
09/21/2023, 9:24 AMchilly-king-10285
09/21/2023, 9:25 AMdelightful-garage-16669
09/21/2023, 9:27 AMchilly-king-10285
09/21/2023, 9:27 AMchilly-king-10285
09/21/2023, 9:28 AMory_lo_
prefix, which makes them easily identifiable and distinguishes them from other types of tokens ([source](https://www.ory.sh/docs/security-compliance/token-formats)). Ory provides logout on all supported clients, including server-rendered browser applications, single page applications (SPA), and native API applications. In each case, Ory first generates a logout URL for a given Ory Session cookie. You can then open the logout URL in the browser or call it using an AJAX request ([source](https://www.ory.sh/docs/kratos/self-service/flows/user-logout)). Here is an example of how to handle logout in a server-side application: javascript import { Configuration, OAuth2Api } from "@ory/client" import { Request, Response } from "express" const ory = new OAuth2Api( new Configuration({ basePath: `https://${process.env.ORY_PROJECT_SLUG}.<http://projects.oryapis.com|projects.oryapis.com>`, accessToken: process.env.ORY_API_KEY, }), ) export async function handleLogout(request: Request, response: Response) { const challenge = request.query.logout_challenge.toString() const { data: logoutRequest } = await ory.getOAuth2LogoutRequest({ logoutChallenge: challenge.toString(), }) // The user did not want to sign out from the given app. if (!allowLogout()) { await ory .rejectOAuth2LogoutRequest({ logoutChallenge: challenge, }) .then(() => { // Do something - redirect somewhere, for example the default home page. }) return } ory .acceptOAuth2LogoutRequest({ logoutChallenge: challenge, }) .then(({ data }) => response.redirect(data.redirect_to)) }
([source](https://www.ory.sh/docs/hydra/guides/logout)) In this example, the handleLogout
function checks if the user wants to log out by calling ory.getOAuth2LogoutRequest
. If the user does not want to log out, the function calls ory.rejectOAuth2LogoutRequest
. If the user does want to log out, the function calls ory.acceptOAuth2LogoutRequest
and then redirects the user to the specified URL. You can use this information to update your cache when a user logs out.delightful-garage-16669
09/21/2023, 9:30 AMchilly-king-10285
09/21/2023, 9:30 AMchilly-king-10285
09/21/2023, 9:31 AMjavascript app.use((req, res, next) => { if (!req.cookies['ory_kratos_session']) { console.log('User is not logged in or has logged out'); // Handle the logout event here, e.g. by clearing cached data } next(); });
In this example, ory_kratos_session
is the name of the session cookie. You should replace this with the actual name of your session cookie. Please note that this is a simplified example and actual implementation may vary based on your application's requirements and the specifics of your setup. For more information on handling sessions in Ory Kratos, you can refer to the [Session Management Overview](https://www.ory.sh/docs/kratos/session-management/overview) in the Ory documentation.delightful-garage-16669
09/21/2023, 9:38 AMchilly-king-10285
09/21/2023, 9:38 AMchilly-king-10285
09/21/2023, 9:39 AMjavascript <http://app.post|app.post>('/logout', (req, res) => { // Handle the logout process here, e.g. by deleting the session cookie // Trigger the webhook <http://axios.post|axios.post>('<https://your-webhook-url>', { event: 'user_logout', user_id: req.user.id }); res.redirect('/login'); });
In this example, <https://your-webhook-url>
is the URL of your webhook. You should replace this with the actual URL of your webhook. Please note that this is a simplified example and actual implementation may vary based on your application's requirements and the specifics of your setup. For more information on using webhooks with Ory, you can refer to the [Ory Actions and webhooks documentation](https://www.ory.sh/docs/guides/integrate-with-ory-cloud-through-webhooks).