Skip to content

Logout without a form #239

Answered by caprica
caprica asked this question in Q&A
Sep 9, 2023 · 1 comments · 1 reply
Discussion options

You must be logged in to vote

This is what I ended up doing, is it OK or is there a better way?

export function UserMenu() {
  const submit = useSubmit()

  const handleLogout = (event: Event) => submit(null, { method: "post", action: "/logout" })

  return <Menu onClickLogout={handleLogout} />
}

And the action:

// app/routes/logout.tsx
export const action = async ({ request }: ActionArgs) => {
    await authenticator.logout(request, { redirectTo: '/sign-in' })
}

// a loader is needed, this simply redirects if the user navigates to this route directly
export const loader = () => redirect('/')

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@sergiodxa
Comment options

Answer selected by sergiodxa
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants