I am new to Next.js and I'm still learning about how to properly secure your API keys in prod. This is my question:
I have a supabase connected to my next.js project and i use drizzle ORM to communicate with it. I use supabase for auth, and i wanted to set up my own "Users" table with a foreign key to "auth.users.id" table in supabase. This was my idea of a service function:
services/handle_new_user.ts:
'use server';
// services/handle_new_user.ts
import { db } from "@/lib/drizzle/db"; // your drizzle db client instance
import { user } from "@/db/migrations/schema";
import type { UserInsert } from "@/db/types";
export async function handleNewAuthUser(authUser: { id: string; email: string }) {
const profile: UserInsert = {
authId: authUser.id,
email: authUser.email,
};
await db.insert(user).values(profile);
}
Now, the db client is this:
import 'dotenv/config'
import { drizzle } from 'drizzle-orm/postgres-js'
import postgres from 'postgres'
const connectionString = process.env.DIRECT_URL!
// Disable prefetch as it is not supported for "Transaction" pool mode
export const client = postgres(connectionString, { prepare: false })
export const db = drizzle(client);
it loads up the env variable meaning it should never be run client side, so "use server" should always be set.
The idea was for this function to be apart of the already provided supabase signup function. Note: supabase has a public api key that is used when connecting to supabase client, also it's run in client side. Part of the sign-up section looks like this: