Basic logout
A user clicks a logout control in a web application.
Better Auth · Function
Signs out the current user through Better Auth’s client API. Optional fetch options can run logic such as redirecting to the login page after successful sign-out.
Call the client’s signOut method to end the current user session. You may provide fetchOptions to run an onSuccess callback, such as navigating to a login route after logout completes.
await authClient.signOut(options)| Name | Type | Description |
|---|---|---|
options | object | Optional request configuration, including a fetchOptions object with an onSuccess callback. |
A promise for the asynchronous sign-out request.
import { authClient } from "@/lib/auth-client";
await authClient.signOut({
fetchOptions: {
onSuccess: () => {
router.push("/login");
},
},
});Signs out through the client and navigates to the login page after a successful response.
A user clicks a logout control in a web application.
The application should return users to authentication after logout succeeds.
A client needs to run cleanup or UI updates after the sign-out request completes.
Use the client method authClient.signOut(). It signs out the user, and the request can be followed by a success callback supplied under fetchOptions.
Follow-up: How would you redirect after logout? Pass fetchOptions.onSuccess and navigate to the login page from that callback.
Call authClient.signOut({ fetchOptions: { onSuccess: ... } }). The callback runs after a successful sign-out and can perform application navigation.
Follow-up: What is the simplest form when no navigation is needed? Use await authClient.signOut() without options.
The documented client call is asynchronous, so await authClient.signOut() to complete the operation before continuing. The basic usage page presents this call as the client-side sign-out mechanism.
Follow-up: What should be added for a post-logout route? Configure an onSuccess callback inside fetchOptions.
authClientfetchOptionsonSuccess