Skip to content

Commit

Permalink
handle logout
Browse files Browse the repository at this point in the history
  • Loading branch information
j8seangel committed Jun 5, 2024
1 parent 4c22c5b commit 33d1624
Showing 1 changed file with 17 additions and 1 deletion.
18 changes: 17 additions & 1 deletion apps/image-labeler/src/routes/__root.tsx
Original file line number Diff line number Diff line change
@@ -1,17 +1,33 @@
import { createRootRoute, Outlet } from '@tanstack/react-router'
import { TanStackRouterDevtools } from '@tanstack/router-devtools'
import { Fragment } from 'react'
import { Fragment, useCallback } from 'react'
import { GFWAPI } from '@globalfishingwatch/api-client'
import { Spinner } from '@globalfishingwatch/ui-components/spinner'
import { useGFWLogin, useGFWLoginRedirect } from '@globalfishingwatch/react-hooks/use-login'
import { Button } from '@globalfishingwatch/ui-components/button'

const RootComponent = () => {
const login = useGFWLogin()
useGFWLoginRedirect(login)

const handleLogoutClick = useCallback(() => {
GFWAPI.logout().then(() => window.location.reload())
}, [])

if (!login.logged) {
return <Spinner />
}

const isAdmin = login.user?.groups.some((group) => group.toLowerCase() === 'admin-group')
if (login.logged && !isAdmin) {
return (
<div>
You need to be an admin to access
<Button onClick={handleLogoutClick}>Logout</Button>
</div>
)
}

return (
<Fragment>
<Outlet />
Expand Down

0 comments on commit 33d1624

Please sign in to comment.