mirror of
https://github.com/Since-3/since3-portal.git
synced 2026-10-12 21:41:16 +00:00
added authentication with proxy
This commit is contained in:
parent
75f8b0bc9d
commit
478adcabca
18 changed files with 609 additions and 15 deletions
8
lib/supabase/admin.ts
Normal file
8
lib/supabase/admin.ts
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
import { createClient } from "@supabase/supabase-js";
|
||||
|
||||
export function createAdminClient() {
|
||||
return createClient(
|
||||
process.env.NEXT_PUBLIC_SUPABASE_URL!,
|
||||
process.env.SUPABASE_SECRET_KEY!
|
||||
)
|
||||
}
|
||||
43
lib/supabase/dal.ts
Normal file
43
lib/supabase/dal.ts
Normal file
|
|
@ -0,0 +1,43 @@
|
|||
import "server-only"
|
||||
import { cache } from "react"
|
||||
import { redirect } from "next/navigation"
|
||||
import { createClient } from "@/lib/supabase/server"
|
||||
|
||||
|
||||
export const getSession = cache(async () => {
|
||||
const supabase = await createClient()
|
||||
const { data: { user } } = await supabase.auth.getUser()
|
||||
return user
|
||||
})
|
||||
|
||||
export const getProfile = cache(async () => {
|
||||
const user = await getSession()
|
||||
|
||||
if (!user) return null
|
||||
|
||||
const supabase = await createClient()
|
||||
const { data } = await supabase
|
||||
.from("profiles")
|
||||
.select("role, name, email")
|
||||
.eq("id", user.id)
|
||||
.single()
|
||||
|
||||
return data
|
||||
})
|
||||
|
||||
|
||||
export const verifySession = cache(async () => {
|
||||
const user = await getSession()
|
||||
if (!user) redirect("/login")
|
||||
|
||||
const profile = await getProfile();
|
||||
if(!profile) redirect("/login")
|
||||
|
||||
return {
|
||||
userId: user.id,
|
||||
role: profile.role,
|
||||
isLoggedIn: true,
|
||||
isAdmin: profile.role === "admin",
|
||||
isClient: profile.role === "client",
|
||||
}
|
||||
})
|
||||
|
|
@ -0,0 +1,14 @@
|
|||
create type user_role as enum ('admin', 'client');
|
||||
|
||||
create table profiles (
|
||||
id uuid primary key references auth.users(id) on delete cascade,
|
||||
email text not null,
|
||||
name text not null,
|
||||
role user_role not null default 'client',
|
||||
created_at timestamptz not null default now()
|
||||
);
|
||||
alter table profiles enable row level security;
|
||||
|
||||
create policy "Users can read their own profile"
|
||||
on profiles for select
|
||||
using (auth.uid() = id);
|
||||
|
|
@ -0,0 +1,17 @@
|
|||
create or replace function public.handle_new_user()
|
||||
returns trigger
|
||||
language plpgsql
|
||||
security definer
|
||||
set search_path = ''
|
||||
as $$
|
||||
begin
|
||||
insert into public.profiles (id, email, name, role)
|
||||
values (
|
||||
new.id,
|
||||
new.email,
|
||||
coalesce(new.raw_user_meta_data->>'name', ''),
|
||||
coalesce((new.raw_user_meta_data->>'role')::public.user_role, 'client')
|
||||
);
|
||||
return new;
|
||||
end;
|
||||
$$;
|
||||
37
lib/supabase/proxy.ts
Normal file
37
lib/supabase/proxy.ts
Normal file
|
|
@ -0,0 +1,37 @@
|
|||
import { createServerClient } from "@supabase/ssr"
|
||||
import { NextResponse, type NextRequest } from "next/server"
|
||||
|
||||
export function createClient(request: NextRequest) {
|
||||
let response = NextResponse.next({
|
||||
request: {
|
||||
headers: request.headers,
|
||||
},
|
||||
})
|
||||
|
||||
const supabase = createServerClient(
|
||||
process.env.NEXT_PUBLIC_SUPABASE_URL!,
|
||||
process.env.NEXT_PUBLIC_SUPABASE_PUBLISHABLE_KEY!,
|
||||
{
|
||||
cookies: {
|
||||
getAll() {
|
||||
return request.cookies.getAll()
|
||||
},
|
||||
setAll(cookiesToSet) {
|
||||
cookiesToSet.forEach(({ name, value }) =>
|
||||
request.cookies.set(name, value)
|
||||
)
|
||||
response = NextResponse.next({
|
||||
request: {
|
||||
headers: request.headers,
|
||||
},
|
||||
})
|
||||
cookiesToSet.forEach(({ name, value, options }) =>
|
||||
response.cookies.set(name, value, options)
|
||||
)
|
||||
},
|
||||
},
|
||||
}
|
||||
)
|
||||
|
||||
return { supabase, response }
|
||||
}
|
||||
|
|
@ -1,8 +1,28 @@
|
|||
import { createClient } from "@supabase/supabase-js";
|
||||
import { createServerClient } from "@supabase/ssr";
|
||||
import { cookies } from "next/headers";
|
||||
|
||||
export function createAdminClient() {
|
||||
return createClient(
|
||||
process.env.NEXT_PUBLIC_SUPABASE_URL!,
|
||||
process.env.SUPABASE_SECRET_KEY!
|
||||
)
|
||||
}
|
||||
export async function createClient() {
|
||||
const cookieStore = await cookies();
|
||||
|
||||
return createServerClient(
|
||||
process.env.NEXT_PUBLIC_SUPABASE_URL!,
|
||||
process.env.NEXT_PUBLIC_SUPABASE_PUBLISHABLE_KEY!,
|
||||
{
|
||||
cookies: {
|
||||
getAll() {
|
||||
return cookieStore.getAll();
|
||||
},
|
||||
setAll(cookiesToSet) {
|
||||
try {
|
||||
cookiesToSet.forEach(({ name, value, options }) =>
|
||||
cookieStore.set(name, value, options)
|
||||
);
|
||||
} catch {
|
||||
// Called from a Server Component that can't set cookies.
|
||||
// Safe to ignore because the proxy refreshes the session.
|
||||
}
|
||||
},
|
||||
},
|
||||
}
|
||||
);
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue