I'm actually a technical novice. Thank you very much for your patience in answering my questions. Based on your response and the chat records from the AI Q&A session, I asked Gemini again. The answer they gave me is as follows. I wonder if the method Gemini provided me can ultimately solve the problem: The problem I encountered was not a code logic error. Instead, in the Next.js Serverless environment, the configuration of the Service Role client instance was overwritten by the context of the user session (JWT and public Key), resulting in the administrator Key being completely invalid. Therefore, the solution we must adopt is: Stop relying on the Service Key in Vercel Function and instead use the database RPC of SECURITY DEFINER, allowing the database to perform insertion operations with the highest privileges by itself and completely ignore any keys or JWTS sent by the client. Detailed answer This is an extremely technical and environment-related issue, and it is also the ultimate root cause of the RLS errors encountered. Why is it like this? Where is the problem? The core reason: The Request Context of the Vercel/Next.js Serverless Function is contaminated. In Vercel (or any Serverless/Edge) environment, When you are in a Server Function (for example, / API/credits or/app/auth/callback/route. The ts) using multiple Supabase client at the same time, in this pollution occurs. The chain of failure is as follows: A. User client creation: Your code first needs to read the user's session (JWT) to verify identity and read data (such as getUserCreditStats). To read the session, you will use a client to read Cookies, usually createClient(). This client is initialized with an anon (public) Key, but it will carry the user's JWT. B. Admin client Call: Immediately after, within the same function, you called the supabaseAdmin client to perform high-privilege INSERT operations (such as inserting welcome points records). C. Context Overwrite: In a Serverless/Edge Runtime environment, all Outbound network Requests (Outbound Requests) may share or wrongly inherit the HTTP Headers of the previous request. (2) Authorization Header leakage: When supabaseAdmin made a request, it mistakenly uploaded the user JWT read by the user client, causing Supabase to recognize the role as authenticated. (3) apikey override: Even worse, SUPABASE_SERVICE_ROLE_KEY (sb_secret_...) is used to initialize the Admin client. It has been overwritten by Anon Key. The final result: It should have been apikey: sb_secret_... The request of + role: service_role became an ordinary user request of apikey: anon + role: authenticated and was thus blocked by RLS. 2. How to know if the Service Role Key has been wrongly replaced with a public Key? We determine by analyzing the Supabase API Gateway Logs: Look at the apikey field: In the details on the right side of the API Gateway Log, there is a clear record indicating that the value of apikey is anon. ANON is an alias for public Key. Look at the role field: The role field immediately below the apikey is displayed as authenticated. According to the explanation of Supabase experts: (1) The Service Role Key (i.e., the Admin client) will never generate RLS errors. (2) Only Anon keys or user JWT will trigger the RLS error. When the log shows the RLS error (42501) and apikey: anon, it undoubtedly indicates that the Admin Key (Service Role Key) has failed and has been replaced by a public Key. 3. Final Solution Approach (Review) Since this is an environment-level issue, it is difficult to solve it completely through code adjustments (such as using global instances or disabling sessions). The only thorough solution is: (1) Stop using supabaseAdmin.insert in Vercel functions. (2) Insert the insertion logic into the SECURITY DEFINER stored procedure (RPC) of the Supabase database. (3) Only call supabaseAdmin.rpc(...) in the Vercel Function. . In this way, even if the client is contaminated with anon + authenticated, the RPC function will run with the highest privileges of postgres, ensuring the success of the insertion operation and avoiding any 42501 RLS errors.