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

49 lines
1.3 KiB
TypeScript
Raw Normal View History

2024-02-04 12:55:09 +08:00
import { createClient } from "@/utils/supabase/server";
import Link from "next/link";
import { cookies } from "next/headers";
import { redirect } from "next/navigation";
2024-03-07 10:08:17 +08:00
import { insertUserProfile } from "@/utils/supabase/supabaseutils";
2024-01-18 15:46:18 +08:00
export default async function AuthButton() {
2024-02-04 12:55:09 +08:00
const cookieStore = cookies();
const supabase = createClient(cookieStore);
2024-01-18 15:46:18 +08:00
const {
2024-03-07 10:08:17 +08:00
data,
2024-01-18 15:46:18 +08:00
data: { user },
2024-02-04 12:55:09 +08:00
} = await supabase.auth.getUser();
2024-03-07 10:08:17 +08:00
//profiles表 插入用户信息
await insertUserProfile(data, supabase);
2024-03-09 16:10:55 +08:00
// console.log("1111 in AuthButton user:", user);
2024-01-18 15:46:18 +08:00
const signOut = async () => {
2024-02-04 12:55:09 +08:00
"use server";
2024-01-18 15:46:18 +08:00
2024-02-04 12:55:09 +08:00
const cookieStore = cookies();
const supabase = createClient(cookieStore);
await supabase.auth.signOut();
2024-02-08 10:29:21 +08:00
2024-02-04 12:55:09 +08:00
return redirect("/login");
};
2024-01-18 15:46:18 +08:00
return user ? (
<div className="flex items-center gap-4">
Hey, {user.email}!
2024-02-08 10:29:21 +08:00
{/* <div className="vip-icon bg-yellow-400 text-white p-2 rounded-full shadow-lg animate-pulse">
2024-02-04 12:55:09 +08:00
VIP
2024-02-08 10:29:21 +08:00
</div> */}
2024-01-18 15:46:18 +08:00
<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>
2024-02-04 12:55:09 +08:00
);
2024-01-18 15:46:18 +08:00
}