On this page
Logout & reset
Last updated
Call resetUser() only on real sign-out — never on anonymous page loads. Covers SPA guards and server-rendered logout handlers.
resetUser() clears the cached identity and generates a fresh anonymous $device_id on the next event. Use it only on real sign-out — when someone who was logged in is no longer the active user.
#When to call resetUser()
Call resetUser() only when all three are true:
- Auth/session loading has finished (
loading === false). - Your app has no signed-in user (
userisnull— they signed out or the session expired). - The SDK still holds the previous login:
vt.getUserState() === 'identified'.
If (1)–(2) hold but (3) is false, the visitor was never logged in — do nothing.
#SPA pattern (client session hook)
// WRONG — wipes anonymous visitors on every reload
useEffect(() => {
if (!user) window.vt?.resetUser()
}, [user])
// CORRECT — reset only after real sign-out
useEffect(() => {
if (authLoading) return
const vt = window.vt
if (!vt) return
if (user?.id) {
vt.identify(user.id, { email: user.email, name: user.name })
} else if (vt.getUserState() === 'identified') {
vt.resetUser()
}
}, [authLoading, user])In an explicit logout button handler, you may call vt.resetUser() directly — the reactive guard above covers session expiry without a dedicated navigation.
#Server-rendered apps
When the layout reads the session on the server and passes currentUser to the client:
- Identify after
vt.init()fromwindow.__currentUseror a server-passed prop on every page load. - Reset in the logout handler only — never in a reactive effect when
useris null.
#Quick reference
| How the app knows the user | Where to identify() | When to resetUser() |
|---|---|---|
| Server renders user on every HTML response | After vt.init() from server-passed user | Logout handler only |
| Client session hook (Clerk, Supabase, …) | useEffect after loading === false | loading === false and user is null and getUserState() === 'identified' |
| Login handler only (anti-pattern) | — | Never rely on this alone |
Stack-specific examples: Identify users → Real-world examples.
#Next step
Verify events after wiring identify and logout.