paper-ai-release-24-07-21/components/AuthButton.tsx

41 lines
1.0 KiB
TypeScript
Raw Normal View History

2024-01-18 15:46:18 +08:00
import { createClient } from '@/utils/supabase/server'
import Link from 'next/link'
import { cookies } from 'next/headers'
import { redirect } from 'next/navigation'
export default async function AuthButton() {
const cookieStore = cookies()
const supabase = createClient(cookieStore)
const {
data: { user },
} = await supabase.auth.getUser()
const signOut = async () => {
'use server'
const cookieStore = cookies()
const supabase = createClient(cookieStore)
await supabase.auth.signOut()
return redirect('/login')
}
return user ? (
<div className="flex items-center gap-4">
Hey, {user.email}!
<form action={signOut}>
<button className="py-2 px-4 rounded-md no-underline bg-btn-background hover:bg-btn-background-hover">
Logout
</button>
</form>
</div>
) : (
<Link
href="/login"
className="py-2 px-3 flex rounded-md no-underline bg-btn-background hover:bg-btn-background-hover"
>
Login
</Link>
)
}