Skip to main content
End a user’s authenticated session and remove their access credentials from the device. Logging out a user removes their session from the device and requires them to authenticate again to access protected resources.

Using the React SDK

To log a user out, use the logout method from the useMoonKey hook:

Basic Usage

With Async Handling

Since logout returns a Promise, you can await it to run code after the user has been logged out:

Common Patterns

Logout with Confirmation

Ask the user to confirm before logging out:

Logout with Loading State

Show a loading state during logout:

Logout in Header/Navigation

Common pattern for a header component:

Logout with Redirect

Redirect to a specific page after logout:
Common pattern for user menus:

Effect of Logout

When you call logout(), MoonKey will:
  1. Clear the session - Remove session tokens and JWTs from IndexedDB
  2. Reset authentication state - Set isAuthenticated to false
  3. Clear user data - Remove the user object from the SDK state
  4. Revoke session on server - Invalidate the session on MoonKey’s backend
After logout, the user will need to authenticate again to access protected resources.

Using the REST API

For backend or custom implementations, you can logout by deleting the session via the REST API:

Backend Implementation

Best Practices

Always Handle Errors

Logout can fail due to network issues or other reasons:

Clear Local State

If you have additional local state, clear it on logout:

Redirect After Logout

Always redirect users to an appropriate page after logout:

Show Confirmation for Important Actions

For applications with unsaved data, confirm before logging out: