Supabase session
This commit is contained in:
@@ -22,6 +22,16 @@ export async function signInWithGitHub(origin: string) {
|
||||
return redirect(data.url);
|
||||
}
|
||||
|
||||
export async function getSession() {
|
||||
const supabase = await createClient();
|
||||
|
||||
try {
|
||||
return await Core.getSession(supabase);
|
||||
} catch {
|
||||
return redirect("/login");
|
||||
}
|
||||
}
|
||||
|
||||
export async function signOut() {
|
||||
const supabase = await createClient();
|
||||
|
||||
|
||||
+31
-21
@@ -4,6 +4,22 @@ import { SupabaseClient, User } from "@supabase/supabase-js";
|
||||
import "server-only";
|
||||
import { UserProfile } from "../user/types";
|
||||
|
||||
export async function signInWithGitHub(supabase: SupabaseClient) {
|
||||
const { data, error } = await supabase.auth.signInWithOAuth({
|
||||
provider: "github",
|
||||
options: {
|
||||
redirectTo: `/auth/callback`,
|
||||
},
|
||||
});
|
||||
|
||||
if (error) {
|
||||
console.error(error);
|
||||
throw error;
|
||||
}
|
||||
|
||||
return data.url;
|
||||
}
|
||||
|
||||
export async function getProfile(
|
||||
supabase: SupabaseClient,
|
||||
user: User,
|
||||
@@ -36,10 +52,11 @@ export async function getGithubProfile(
|
||||
.from("profiles")
|
||||
.insert({
|
||||
id: user.id,
|
||||
username: user.user_metadata.login || null,
|
||||
full_name: user.user_metadata.name || null,
|
||||
username: user.user_metadata.user_name || `user_${user.id.slice(0, 6)}`,
|
||||
avatar_url: user.user_metadata.avatar_url || null,
|
||||
})
|
||||
bio: "",
|
||||
created_at: new Date(),
|
||||
} satisfies UserProfile)
|
||||
.select()
|
||||
.single();
|
||||
|
||||
@@ -52,33 +69,26 @@ export async function getSession(
|
||||
supabase: SupabaseClient,
|
||||
): Promise<[User, UserProfile]> {
|
||||
const {
|
||||
data: { session },
|
||||
data: { user },
|
||||
error: sessionError,
|
||||
} = await supabase.auth.getSession();
|
||||
} = await supabase.auth.getUser();
|
||||
|
||||
if (sessionError || !session?.user) {
|
||||
if (sessionError || !user) {
|
||||
throw new Error("No authenticated user found");
|
||||
}
|
||||
|
||||
const user = session.user;
|
||||
|
||||
const profile = await getProfile(supabase, user);
|
||||
|
||||
return [user, profile];
|
||||
}
|
||||
|
||||
export async function signInWithGitHub(supabase: SupabaseClient) {
|
||||
const { data, error } = await supabase.auth.signInWithOAuth({
|
||||
provider: "github",
|
||||
options: {
|
||||
redirectTo: `/auth/callback`,
|
||||
},
|
||||
});
|
||||
|
||||
if (error) {
|
||||
console.error(error);
|
||||
throw error;
|
||||
export async function getSessionRedirect(
|
||||
supabase: SupabaseClient,
|
||||
redirect: (s: string) => void,
|
||||
): Promise<[User, UserProfile] | undefined> {
|
||||
try {
|
||||
return await getSession(supabase);
|
||||
} catch {
|
||||
redirect("/login");
|
||||
}
|
||||
|
||||
return data.url;
|
||||
}
|
||||
|
||||
@@ -1,9 +1,16 @@
|
||||
"use server";
|
||||
|
||||
import { createClient } from "@/lib/supabase/server";
|
||||
import { getSession, getSessionRedirect } from "../account/core";
|
||||
import * as Core from "./core";
|
||||
import { Run } from "./types";
|
||||
import { redirect } from "next/navigation";
|
||||
|
||||
export async function getLiveRuns(): Promise<Run[]> {
|
||||
const supabase = await createClient();
|
||||
|
||||
const _ = await getSession(supabase);
|
||||
|
||||
return [
|
||||
{
|
||||
id: 1,
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
export interface UserProfile {
|
||||
id: string;
|
||||
username: string;
|
||||
full_name: string;
|
||||
avatar_url: string;
|
||||
bio: string;
|
||||
created_at: Date;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user