Skip to main content

Implementing user logout

The logout flow allows users to securely terminate their sessions. This guide shows how to implement proper logout functionality in your application.

Logout flow

  1. Create a logout flow

    Initialize a browser-based logout flow or call the API endpoint

  2. Submit the logout request

    Complete the logout process to terminate the session

  3. Redirect after logout

    Send users to a suitable page after successful logout

Implement logout in express.js

// Create logout route
app.get("/logout", async (req, res) => {
try {
// Create a logout flow
const { data: logoutFlow } = await ory.createBrowserLogoutFlow({
cookie: req.header("cookie"),
})

// Redirect to logout URL
res.redirect(logoutFlow.logout_url)
} catch (err) {
console.error("Logout error:", err)
// Already logged out or error, redirect to login
res.redirect("/login")
}
})

After successful logout

After a successful logout, Ory:

  1. Invalidates the user's session
  2. Removes the session cookie from the browser
  3. Redirects the user to the specified return URL

Next steps

After implementing logout functionality, you should:

  1. Implement session refresh functionality
  2. Add multi-factor authentication
  3. Configure account recovery