NEXT_PUBLIC_SUPABASE_URL=프로젝트 url
NEXT_PUBLIC_SUPABASE_ANON_KEY=
NEXT_SUPABASE_SERVICE_ROLE=
NEXT_SUPABASE_DB_PASSWORD=
알맞은 값 입력
"scripts": {
"generate-types": "npx supabase gen types typescript --project-id [project_id] --schema public > types_db.ts"
}
package.json 파일에 명령어 설치
Supabase 프로젝트의 데이터베이스 스키마를 기반으로 TypeScript 타입 파일을 자동으로 생성하고, 그 결과를 types_db.ts 파일에 저장하는 역할을 함
그리고
npm run generate-types
실행 시켜주면 types.db 를 생성 됌
npx supabase login
이거 입력해주면 supabse 프로젝트랑 로컬이랑 연동 끝임
types.db 파일 보면 내용이 채워져있음
npm install @supabase/ssr @supabase/supabase-js
client.ts
"use client";
import { createBrowserClient } from "@supabase/ssr";
export const createBrowserSupabaseClient = () =>
createBrowserClient(
process.env.NEXT_PUBLIC_SUPABASE_URL!,
process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY!
);
server.ts
import { createServerClient, type CookieOptions } from "@supabase/ssr";
import { cookies } from "next/headers";
import { Database } from "types_db";
export const createServerSupabaseClient = async (cookieStore: ReturnType<typeof cookies> = cookies(), admin: boolean = false) => {
return createServerClient<Database>(process.env.NEXT_PUBLIC_SUPABASE_URL!, admin ? process.env.NEXT_SUPABASE_SERVICE_ROLE! : process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY!, {
cookies: {
get(name: string) {
return cookieStore.get(name)?.value;
},
set(name: string, value: string, options: CookieOptions) {
try {
cookieStore.set({ name, value, ...options });
} catch (error) {
// The `set` method was called from a Server Component.
// This can be ignored if you have middleware refreshing
// user sessions.
}
},
remove(name: string, options: CookieOptions) {
try {
cookieStore.set({ name, value: "", ...options });
} catch (error) {
// The `delete` method was called from a Server Component.
// This can be ignored if you have middleware refreshing
// user sessions.
}
},
},
});
};
export const createServerSupabaseAdminClient = async (cookieStore: ReturnType<typeof cookies> = cookies()) => {
return createServerSupabaseClient(cookieStore, true);
};
middleware.ts
import { type NextRequest, NextResponse } from "next/server";
export const applyMiddlewareSupabaseClient = async (request: NextRequest) => {
// Create an unmodified response
let response = NextResponse.next({
request: {
headers: request.headers,
},
});
const supabase = createServerClient(
process.env.NEXT_PUBLIC_SUPABASE_URL!,
process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY!,
{
cookies: {
get(name: string) {
return request.cookies.get(name)?.value;
},
set(name: string, value: string, options: CookieOptions) {
// If the cookie is updated, update the cookies for the request and response
request.cookies.set({
name,
value,
...options,
});
response = NextResponse.next({
request: {
headers: request.headers,
},
});
response.cookies.set({
name,
value,
...options,
});
},
remove(name: string, options: CookieOptions) {
// If the cookie is removed, update the cookies for the request and response
request.cookies.set({
name,
value: "",
...options,
});
response = NextResponse.next({
request: {
headers: request.headers,
},
});
response.cookies.set({
name,
value: "",
...options,
});
},
},
}
);
// refreshing the auth token
await supabase.auth.getUser();
return response;
};
export async function middleware(request) {
return await applyMiddlewareSupabaseClient(request);
}
export const config = {
matcher: [
/*
* Match all request paths except for the ones starting with:
* - _next/static (static files)
* - _next/image (image optimization files)
* - favicon.ico (favicon file)
* Feel free to modify this pattern to include more paths.
*/
"/((?!_next/static|_next/image|favicon.ico|.*\\.(?:svg|png|jpg|jpeg|gif|webp)$).*)",
],
};