mirror of https://github.com/buster-so/buster.git
set cookies
This commit is contained in:
parent
02d0f9f2c4
commit
504a3360b6
|
@ -2,7 +2,7 @@
|
||||||
|
|
||||||
import { BASE_URL } from './buster_rest/instances';
|
import { BASE_URL } from './buster_rest/instances';
|
||||||
import type { RequestInit } from 'next/dist/server/web/spec-extension/request';
|
import type { RequestInit } from 'next/dist/server/web/spec-extension/request';
|
||||||
import { createClient } from '../context/Supabase/server';
|
import { createServerSupabaseClient } from '../context/Supabase/server';
|
||||||
|
|
||||||
export interface FetchConfig extends RequestInit {
|
export interface FetchConfig extends RequestInit {
|
||||||
baseURL?: string;
|
baseURL?: string;
|
||||||
|
@ -10,7 +10,7 @@ export interface FetchConfig extends RequestInit {
|
||||||
}
|
}
|
||||||
|
|
||||||
export const serverFetch = async <T>(url: string, config: FetchConfig = {}): Promise<T> => {
|
export const serverFetch = async <T>(url: string, config: FetchConfig = {}): Promise<T> => {
|
||||||
const supabase = await createClient();
|
const supabase = await createServerSupabaseClient();
|
||||||
const sessionData = await supabase.auth.getSession();
|
const sessionData = await supabase.auth.getSession();
|
||||||
const accessToken = sessionData.data?.session?.access_token;
|
const accessToken = sessionData.data?.session?.access_token;
|
||||||
|
|
||||||
|
|
|
@ -1,21 +1,21 @@
|
||||||
import { createServerClient } from '@supabase/ssr';
|
import { CookieOptions, createServerClient } from '@supabase/ssr';
|
||||||
import { cookies } from 'next/headers';
|
import { cookies } from 'next/headers';
|
||||||
|
|
||||||
export async function createClient() {
|
export const COOKIE_OPTIONS: CookieOptions = {
|
||||||
|
path: '/',
|
||||||
|
secure: process.env.NODE_ENV === 'production', // Only use secure in production
|
||||||
|
sameSite: 'lax', // Type assertion to fix the error
|
||||||
|
httpOnly: true, // Make cookies HttpOnly
|
||||||
|
maxAge: 60 * 60 * 24 * 7 // 1 week
|
||||||
|
};
|
||||||
|
|
||||||
|
export const createServerSupabaseClient = async () => {
|
||||||
const cookieStore = await cookies();
|
const cookieStore = await cookies();
|
||||||
|
|
||||||
return await createServerClient(
|
return createServerClient(
|
||||||
process.env.NEXT_PUBLIC_SUPABASE_URL!,
|
process.env.NEXT_PUBLIC_SUPABASE_URL!,
|
||||||
process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY!,
|
process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY!,
|
||||||
{
|
{
|
||||||
cookieOptions: {
|
|
||||||
secure: true,
|
|
||||||
httpOnly: true
|
|
||||||
},
|
|
||||||
auth: {
|
|
||||||
autoRefreshToken: true,
|
|
||||||
persistSession: true
|
|
||||||
},
|
|
||||||
cookies: {
|
cookies: {
|
||||||
getAll() {
|
getAll() {
|
||||||
return cookieStore.getAll();
|
return cookieStore.getAll();
|
||||||
|
@ -23,7 +23,7 @@ export async function createClient() {
|
||||||
setAll(cookiesToSet) {
|
setAll(cookiesToSet) {
|
||||||
try {
|
try {
|
||||||
cookiesToSet.forEach(({ name, value, options }) =>
|
cookiesToSet.forEach(({ name, value, options }) =>
|
||||||
cookieStore.set(name, value, options)
|
cookieStore.set(name, value, { ...options, ...COOKIE_OPTIONS })
|
||||||
);
|
);
|
||||||
} catch {
|
} catch {
|
||||||
// The `setAll` method was called from a Server Component.
|
// The `setAll` method was called from a Server Component.
|
||||||
|
@ -34,4 +34,4 @@ export async function createClient() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
}
|
};
|
||||||
|
|
|
@ -1,3 +1,4 @@
|
||||||
|
import { COOKIE_OPTIONS } from '@/context/Supabase/server';
|
||||||
import { createServerClient } from '@supabase/ssr';
|
import { createServerClient } from '@supabase/ssr';
|
||||||
import { User } from '@supabase/supabase-js';
|
import { User } from '@supabase/supabase-js';
|
||||||
import { NextResponse, type NextRequest } from 'next/server';
|
import { NextResponse, type NextRequest } from 'next/server';
|
||||||
|
@ -23,7 +24,7 @@ export async function updateSession(request: NextRequest) {
|
||||||
request
|
request
|
||||||
});
|
});
|
||||||
cookiesToSet.forEach(({ name, value, options }) =>
|
cookiesToSet.forEach(({ name, value, options }) =>
|
||||||
supabaseResponse.cookies.set(name, value, options)
|
supabaseResponse.cookies.set(name, value, { ...options, ...COOKIE_OPTIONS })
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue