Sign in with github implementation
This commit is contained in:
@@ -1,3 +1,26 @@
|
|||||||
"use server";
|
"use server";
|
||||||
|
|
||||||
import * as Core from "./core";
|
import * as Core from "./core";
|
||||||
|
import { createClient } from "@/lib/supabase";
|
||||||
|
import { redirect } from "next/navigation";
|
||||||
|
|
||||||
|
export async function signInWithGitHub() {
|
||||||
|
const supabase = await createClient();
|
||||||
|
|
||||||
|
const { data, error } = await supabase.auth.signInWithOAuth({
|
||||||
|
provider: "github",
|
||||||
|
});
|
||||||
|
|
||||||
|
if (error) {
|
||||||
|
console.error(error);
|
||||||
|
return redirect("/login?msg=Trouble");
|
||||||
|
}
|
||||||
|
|
||||||
|
return redirect(data.url);
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function signOut() {
|
||||||
|
const supabase = await createClient();
|
||||||
|
|
||||||
|
const { error } = await supabase.auth.signOut();
|
||||||
|
}
|
||||||
|
|||||||
@@ -1,3 +1,68 @@
|
|||||||
"use server";
|
"use server";
|
||||||
|
|
||||||
|
import { SupabaseClient, User } from "@supabase/supabase-js";
|
||||||
import "server-only";
|
import "server-only";
|
||||||
|
import { UserProfile } from "../user/types";
|
||||||
|
|
||||||
|
export async function getProfile(
|
||||||
|
supabase: SupabaseClient,
|
||||||
|
user: User,
|
||||||
|
): Promise<UserProfile> {
|
||||||
|
// Try to fetch existing profile
|
||||||
|
const { data: profile, error } = await supabase
|
||||||
|
.from("profiles")
|
||||||
|
.select("*")
|
||||||
|
.eq("id", user.id)
|
||||||
|
.single();
|
||||||
|
|
||||||
|
if (error && error.code !== "PGRST116") {
|
||||||
|
// PGRST116 = "No rows found" is fine
|
||||||
|
throw error;
|
||||||
|
}
|
||||||
|
|
||||||
|
// If no profile, create one
|
||||||
|
if (!profile) {
|
||||||
|
return getGithubProfile(supabase, user);
|
||||||
|
}
|
||||||
|
|
||||||
|
return profile;
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function getGithubProfile(
|
||||||
|
supabase: SupabaseClient,
|
||||||
|
user: User,
|
||||||
|
): Promise<UserProfile> {
|
||||||
|
const { data: newProfile, error: insertError } = await supabase
|
||||||
|
.from("profiles")
|
||||||
|
.insert({
|
||||||
|
id: user.id,
|
||||||
|
username: user.user_metadata.login || null,
|
||||||
|
full_name: user.user_metadata.name || null,
|
||||||
|
avatar_url: user.user_metadata.avatar_url || null,
|
||||||
|
})
|
||||||
|
.select()
|
||||||
|
.single();
|
||||||
|
|
||||||
|
if (insertError) throw insertError;
|
||||||
|
|
||||||
|
return newProfile;
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function getSession(
|
||||||
|
supabase: SupabaseClient,
|
||||||
|
): Promise<[User, UserProfile]> {
|
||||||
|
const {
|
||||||
|
data: { session },
|
||||||
|
error: sessionError,
|
||||||
|
} = await supabase.auth.getSession();
|
||||||
|
|
||||||
|
if (sessionError || !session?.user) {
|
||||||
|
throw new Error("No authenticated user found");
|
||||||
|
}
|
||||||
|
|
||||||
|
const user = session.user;
|
||||||
|
|
||||||
|
const profile = await getProfile(supabase, user);
|
||||||
|
|
||||||
|
return [user, profile];
|
||||||
|
}
|
||||||
|
|||||||
@@ -1,3 +0,0 @@
|
|||||||
export interface Account {
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -0,0 +1,6 @@
|
|||||||
|
export interface UserProfile {
|
||||||
|
id: string;
|
||||||
|
username: string;
|
||||||
|
full_name: string;
|
||||||
|
avatar_url: string;
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user