fix: github登录无法将用户数据插入数据库

This commit is contained in:
liuweiqing 2024-02-25 21:21:21 +08:00
parent 478d228313
commit 65db119a9a
2 changed files with 20 additions and 1 deletions

View File

@ -1,7 +1,26 @@
"use client";
import { insertUserProfile } from "@/utils/supabase/supabaseutils";
import { createClient } from "@/utils/supabase/client";
import { useEffect } from "react";
export function SignInGitHub() {
useEffect(() => {
const supabase = createClient();
const { data: data } = supabase.auth.onAuthStateChange(
async (event, session) => {
if (event === "SIGNED_IN") {
// 用户登录成功,执行后续操作
await insertUserProfile(session!.user, supabase);
}
}
);
return () => {
// call unsubscribe to remove the callback
data.subscription.unsubscribe();
};
}, []);
const signInWithGithub = async () => {
const supabase = createClient();
const { data, error } = await supabase.auth.signInWithOAuth({

View File

@ -182,7 +182,7 @@ export async function insertUserProfile(data: any, supabase: SupabaseClient) {
if (user) {
const { data, error: profileError } = await supabase
.from("profiles")
.insert([{ id: user.id, email: user.email }]);
.upsert([{ id: user.id, email: user.email }]);
if (profileError) {
console.error("Failed to create user profile:", profileError);