feat: github登录(测试中)

This commit is contained in:
liuweiqing 2024-02-14 23:16:03 +08:00
parent c03741c396
commit 4ee08169df
3 changed files with 93 additions and 19 deletions

View File

@ -3,12 +3,24 @@ import { headers, cookies } from "next/headers";
import { createClient } from "@/utils/supabase/server";
import { redirect } from "next/navigation";
import * as Sentry from "@sentry/nextjs";
export default function Login({
import DeployButton from "@/components/DeployButton";
import SettingsLink from "@/components/SettingsLink";
//i18n
import { useTranslation } from "@/app/i18n";
import { FooterBase } from "@/components/Footer/FooterBase";
//supabase
import { insertUserProfile } from "@/utils/supabase/supabaseutils";
// signingithub
import { SignInGitHub } from "@/components/SignInGitHub";
export default async function Login({
searchParams,
params: { lng },
}: {
searchParams: { message: string };
params: { lng: string };
}) {
const { t } = await useTranslation(lng);
const signIn = async (formData: FormData) => {
"use server";
@ -54,22 +66,7 @@ export default function Login({
},
});
//profiles表 插入用户信息
const user = data?.user;
if (user) {
const { data, error: profileError } = await supabase
.from("profiles")
.insert([{ id: user.id, email: user.email }]);
if (profileError) {
console.error("Failed to create user profile:", profileError);
}
//sentry
Sentry.setUser({
email: user.email,
id: user.id,
ip_address: "{{auto}}}",
});
}
await insertUserProfile(data, supabase);
if (error) {
return redirect("/login?message=Could not authenticate user");
@ -80,6 +77,12 @@ export default function Login({
return (
<div className="flex-1 flex flex-col w-full px-8 sm:max-w-md justify-center gap-2">
<nav className="w-full flex justify-center border-b border-b-foreground/10 h-16">
<div className="w-full max-w-4xl flex justify-between items-center p-3 text-sm">
<DeployButton />
<SettingsLink />
</div>
</nav>
<Link
href="/"
className="absolute left-8 top-8 py-2 px-4 rounded-md no-underline text-foreground bg-btn-background hover:bg-btn-background-hover flex items-center group text-sm"
@ -100,7 +103,6 @@ export default function Login({
</svg>{" "}
Back
</Link>
<form
className="animate-in flex-1 flex flex-col w-full justify-center gap-2 text-foreground"
action={signIn}
@ -139,6 +141,22 @@ export default function Login({
</p>
)}
</form>
<SignInGitHub />
<footer className="w-full border-t border-t-foreground/10 p-8 flex justify-center text-center text-xs">
<div className="flex items-center space-x-4">
{" "}
{/* 添加flex容器来水平排列子元素 */}
<a
href="https://github.com/14790897/paper-ai"
target="_blank"
className="font-bold text-blue-600 hover:underline hover:text-blue-800"
rel="noreferrer"
>
{t("give me a star in GitHub")}
</a>
<FooterBase t={t} lng={lng} />
</div>
</footer>
</div>
);
}

View File

@ -0,0 +1,33 @@
"use client";
import { insertUserProfile } from "@/utils/supabase/supabaseutils";
import { createClient } from "@/utils/supabase/client";
export function SignInGitHub() {
const signInWithGithub = async () => {
const supabase = createClient();
const { data, error } = await supabase.auth.signInWithOAuth({
provider: "github",
});
//profiles表 插入用户信息
await insertUserProfile(data, supabase);
};
return (
<button
className="bg-black text-white rounded-md px-4 py-2 flex items-center justify-center"
onClick={signInWithGithub}
>
<svg
role="img"
viewBox="0 0 24 24"
xmlns="http://www.w3.org/2000/svg"
className="mr-2 h-4 w-4"
>
<title>GitHub icon</title>
<path
fill="currentColor"
d="M12 .3a12 12 0 0 0-3.8 23.4c.6.1.8-.3.8-.6v-2C8.7 21 8 20 8 20c-1.1 0-1.8-.9-1.8-.9-.6-1-.1-1.8.1-2 .7-.6 1.8-.4 2.7.1.1-.5.3-.9.5-1.1-2.3-.3-4.7-1.1-4.7-5 0-1.1.4-2 1-2.7 0-1 .1-2 .7-2.7 0 0 .9-.3 2.8 1a9.8 9.8 0 0 1 5.2 0c1.9-1.3 2.8-1 2.8-1 .6.7.7 1.7.7 2.7.7.7 1 1.6 1 2.7 0 3.9-2.4 4.7-4.7 5 .3.2.6.7.6 1.4v2.1c0 .3.2.7.8.6A12 12 0 0 0 12 .3"
/>
</svg>
Sign In with GitHub
</button>
);
}

View File

@ -7,6 +7,9 @@ import { Reference } from "@/utils/global";
//supabase
const supabase = createClient();
import { createClient } from "@/utils/supabase/client";
//sentry
import * as Sentry from "@sentry/nextjs";
//获取用户id
export async function getUser() {
const { data, error } = await supabase.auth.getSession();
@ -172,3 +175,23 @@ export async function fetchUserVipStatus(userId: string) {
return false;
}
}
//profiles表 插入用户信息
export async function insertUserProfile(data: any, supabase: SupabaseClient) {
const user = data?.user;
if (user) {
const { data, error: profileError } = await supabase
.from("profiles")
.insert([{ id: user.id, email: user.email }]);
if (profileError) {
console.error("Failed to create user profile:", profileError);
}
//sentry
Sentry.setUser({
email: user.email,
id: user.id,
ip_address: "{{auto}}}",
});
}
}