feat: log out and auth redirect
This commit is contained in:
@@ -1,7 +1,17 @@
|
|||||||
import { Link, useLocation } from "@tanstack/react-router"
|
import { Link, useLocation } from "@tanstack/react-router"
|
||||||
import { FilesIcon, HomeIcon, User2Icon } from "lucide-react"
|
import { useAuth } from "@workos-inc/authkit-react"
|
||||||
|
import {
|
||||||
|
ChevronDownIcon,
|
||||||
|
FilesIcon,
|
||||||
|
HomeIcon,
|
||||||
|
LogOutIcon,
|
||||||
|
SettingsIcon,
|
||||||
|
User2Icon,
|
||||||
|
} from "lucide-react"
|
||||||
import {
|
import {
|
||||||
DropdownMenu,
|
DropdownMenu,
|
||||||
|
DropdownMenuContent,
|
||||||
|
DropdownMenuItem,
|
||||||
DropdownMenuTrigger,
|
DropdownMenuTrigger,
|
||||||
} from "@/components/ui/dropdown-menu"
|
} from "@/components/ui/dropdown-menu"
|
||||||
import {
|
import {
|
||||||
@@ -19,7 +29,7 @@ export function DashboardSidebar() {
|
|||||||
<SidebarHeader>
|
<SidebarHeader>
|
||||||
<SidebarMenu>
|
<SidebarMenu>
|
||||||
<SidebarMenuItem>
|
<SidebarMenuItem>
|
||||||
<UserSwitcher />
|
<UserMenu />
|
||||||
</SidebarMenuItem>
|
</SidebarMenuItem>
|
||||||
</SidebarMenu>
|
</SidebarMenu>
|
||||||
<MainSidebarMenu />
|
<MainSidebarMenu />
|
||||||
@@ -61,17 +71,34 @@ function MainSidebarMenu() {
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
function UserSwitcher() {
|
function UserMenu() {
|
||||||
|
const { signOut } = useAuth()
|
||||||
|
|
||||||
|
function handleSignOut() {
|
||||||
|
signOut()
|
||||||
|
}
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<DropdownMenu>
|
<DropdownMenu>
|
||||||
<DropdownMenuTrigger asChild>
|
<DropdownMenuTrigger asChild>
|
||||||
<SidebarMenuButton size="lg" className="w-fit px-1.5">
|
<SidebarMenuButton className="w-fit px-1.5 group-data-[collapsible=icon]:px-1.5! data-[state=open]:bg-sidebar-accent">
|
||||||
<div className="bg-sidebar-primary text-sidebar-primary-foreground flex aspect-square size-8 items-center justify-center rounded-md">
|
<div className="bg-sidebar-primary text-sidebar-primary-foreground flex aspect-square size-5 items-center justify-center rounded-md">
|
||||||
<User2Icon className="size-4" />
|
<User2Icon className="size-3" />
|
||||||
</div>
|
</div>
|
||||||
<span>Kenneth</span>
|
<span className="truncate font-medium">Kenneth</span>
|
||||||
|
<ChevronDownIcon className="opacity-50" />
|
||||||
</SidebarMenuButton>
|
</SidebarMenuButton>
|
||||||
</DropdownMenuTrigger>
|
</DropdownMenuTrigger>
|
||||||
|
<DropdownMenuContent className="w-64" align="start" side="bottom">
|
||||||
|
<DropdownMenuItem>
|
||||||
|
<SettingsIcon />
|
||||||
|
Settings
|
||||||
|
</DropdownMenuItem>
|
||||||
|
<DropdownMenuItem onClick={handleSignOut}>
|
||||||
|
<LogOutIcon />
|
||||||
|
Log out
|
||||||
|
</DropdownMenuItem>
|
||||||
|
</DropdownMenuContent>
|
||||||
</DropdownMenu>
|
</DropdownMenu>
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
@@ -1,19 +1,65 @@
|
|||||||
import { createFileRoute, Navigate, Outlet } from "@tanstack/react-router"
|
import {
|
||||||
import { Authenticated, Unauthenticated } from "convex/react"
|
createFileRoute,
|
||||||
|
Navigate,
|
||||||
|
Outlet,
|
||||||
|
useLocation,
|
||||||
|
} from "@tanstack/react-router"
|
||||||
|
import { useAuth } from "@workos-inc/authkit-react"
|
||||||
|
import {
|
||||||
|
Authenticated,
|
||||||
|
AuthLoading,
|
||||||
|
Unauthenticated,
|
||||||
|
useConvexAuth,
|
||||||
|
} from "convex/react"
|
||||||
|
import { useEffect, useState } from "react"
|
||||||
|
import { LoadingSpinner } from "@/components/ui/loading-spinner"
|
||||||
|
|
||||||
export const Route = createFileRoute("/_authenticated")({
|
export const Route = createFileRoute("/_authenticated")({
|
||||||
component: AuthenticatedLayout,
|
component: AuthenticatedLayout,
|
||||||
})
|
})
|
||||||
|
|
||||||
function AuthenticatedLayout() {
|
function AuthenticatedLayout() {
|
||||||
|
const { search } = useLocation()
|
||||||
|
const { isLoading } = useConvexAuth()
|
||||||
|
const { isLoading: authKitLoading } = useAuth()
|
||||||
|
const [hasProcessedAuth, setHasProcessedAuth] = useState(false)
|
||||||
|
|
||||||
|
// Check if we're in the middle of processing an auth code
|
||||||
|
const hasAuthCode = search && typeof search === "object" && "code" in search
|
||||||
|
|
||||||
|
// Track when auth processing is complete
|
||||||
|
useEffect(() => {
|
||||||
|
if (!authKitLoading && !isLoading) {
|
||||||
|
// Delay to ensure auth state is fully synchronized
|
||||||
|
const timer = setTimeout(() => {
|
||||||
|
setHasProcessedAuth(true)
|
||||||
|
}, 500)
|
||||||
|
return () => clearTimeout(timer)
|
||||||
|
}
|
||||||
|
}, [authKitLoading, isLoading])
|
||||||
|
|
||||||
|
// Show loading during auth code processing or while auth state is syncing
|
||||||
|
if (hasAuthCode || authKitLoading || isLoading || !hasProcessedAuth) {
|
||||||
|
return (
|
||||||
|
<div className="flex h-screen w-full items-center justify-center">
|
||||||
|
<LoadingSpinner className="size-10" />
|
||||||
|
</div>
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
<Authenticated>
|
<Authenticated>
|
||||||
<Outlet />
|
<Outlet />
|
||||||
</Authenticated>
|
</Authenticated>
|
||||||
<Unauthenticated>
|
<Unauthenticated>
|
||||||
<Navigate to="/login" />
|
<Navigate replace to="/login" />
|
||||||
</Unauthenticated>
|
</Unauthenticated>
|
||||||
|
<AuthLoading>
|
||||||
|
<div className="flex h-screen w-full items-center justify-center">
|
||||||
|
<LoadingSpinner className="size-10" />
|
||||||
|
</div>
|
||||||
|
</AuthLoading>
|
||||||
</>
|
</>
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
@@ -1,9 +1,9 @@
|
|||||||
import { createFileRoute } from "@tanstack/react-router"
|
import { createFileRoute, Navigate } from "@tanstack/react-router"
|
||||||
|
|
||||||
export const Route = createFileRoute("/_authenticated")({
|
export const Route = createFileRoute("/_authenticated")({
|
||||||
component: RouteComponent,
|
component: RouteComponent,
|
||||||
})
|
})
|
||||||
|
|
||||||
function RouteComponent() {
|
function RouteComponent() {
|
||||||
return <div>Hello "/"!</div>
|
return <Navigate replace to="/files" />
|
||||||
}
|
}
|
||||||
|
@@ -1,9 +1,13 @@
|
|||||||
import { createFileRoute } from "@tanstack/react-router"
|
import { createFileRoute } from "@tanstack/react-router"
|
||||||
|
import { useAuth } from "@workos-inc/authkit-react"
|
||||||
|
import { Button } from "../components/ui/button"
|
||||||
|
|
||||||
export const Route = createFileRoute("/login")({
|
export const Route = createFileRoute("/login")({
|
||||||
component: RouteComponent,
|
component: RouteComponent,
|
||||||
})
|
})
|
||||||
|
|
||||||
function RouteComponent() {
|
function RouteComponent() {
|
||||||
return <div>Hello "/login"!</div>
|
const { signIn } = useAuth()
|
||||||
|
|
||||||
|
return <Button onClick={() => signIn()}>Login</Button>
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user