summaryrefslogtreecommitdiffhomepage
path: root/packages/console/app/src/routes/user-menu.tsx
blob: 0af89f81b0c3e58661b0a43dac9c9cbe76099a9a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import { action, redirect } from "@solidjs/router"
import { getRequestEvent } from "solid-js/web"
import { useAuthSession } from "~/context/auth.session"
import { Dropdown } from "~/component/dropdown"
import "./user-menu.css"

const logout = action(async () => {
  "use server"
  const auth = await useAuthSession()
  const event = getRequestEvent()
  const current = auth.data.current
  if (current)
    await auth.update((val) => {
      delete val.account?.[current]
      const first = Object.keys(val.account ?? {})[0]
      val.current = first
      event!.locals.actor = undefined
      return val
    })
  throw redirect("/zen")
})

export function UserMenu(props: { email: string | null | undefined }) {
  return (
    <div data-component="user-menu">
      <Dropdown trigger={props.email ?? ""} align="right">
        <form action={logout} method="post">
          <button type="submit" formaction={logout} data-slot="item">
            Logout
          </button>
        </form>
      </Dropdown>
    </div>
  )
}