<@U04UQ68083H> Best way to programmatically logout...
# ory-copilot
b
@chilly-king-10285 Best way to programmatically logout?
b
A message from kapa.ai
The best way to programmatically log out in Ory depends on whether you're building a browser-based application or a native/mobile application. Let me explain both approaches: ## For Native/Mobile Applications For native or mobile applications, you should use the
performNativeLogout
API endpoint:
Copy code
go func Logout(ctx context.Context, sessionToken string) error {       _, err := ory.FrontendApi.PerformNativeLogout(ctx).           PerformNativeLogoutBody(*client.NewPerformNativeLogoutBody(sessionToken)).           Execute()     if err != nil {           return err       }     // Logout was successful     return nil   }
This endpoint expects the session token to be provided in the request body. When successfully executed, it will invalidate the session token, effectively logging the user out. [Ory Kratos SDK Go Documentation](https://www.ory.sh/docs/kratos/sdk/go#perform-logout-flow) ## For Browser-Based Applications For browser-based applications, you should use the
createBrowserLogoutFlow
endpoint to generate a logout URL:
Copy code
javascript const logoutUrl = await ory     .frontend.createBrowserLogoutFlow()     .then(({ data }) => data.logout_url)
The user can then be redirected to this URL to complete the logout process. [SDK V1 Upgrade Guide](https://www.ory.sh/docs/guides/upgrade/sdk-v1#logout-for-browser-apps) ## Additional Considerations 1. Session Management: When implementing logout, remember that Ory maintains different session layers, especially when using OAuth2/OpenID Connect. You may need to handle multiple session layers depending on your setup. 2. Revoking Specific Sessions: If you want to revoke a specific session rather than the current one, you can use the
disableMySession
API:
Copy code
typescript await frontend.disableMySession({   id: sessionId,   xSessionToken: token, })
[Session Management Documentation](https://www.ory.sh/docs/kratos/session-management/list-revoke-get-sessions#revoking-a-specific-session) 3. Revoking All Other Sessions: You can also revoke all sessions except the current one:
Copy code
typescript await frontend.disableMyOtherSessions({   xSessionToken: token, })
[Session Management Documentation](https://www.ory.sh/docs/kratos/session-management/list-revoke-get-sessions#revoking-all-other-sessions) Choose the approach that best fits your application type and requirements.