fix(frontend): cookies console warnings (#10124)

### Changes 🏗️

<img width="600" alt="Screenshot_2025-06-06_at_3 00 40_PM"
src="https://github.com/user-attachments/assets/2793793e-356f-47b0-8624-9d73af414ff3"
/>

☝🏽 Fix the following warning that gets logged to the console when
running the dev server in the Front-end. It shouldn't cause an actual
auth issue, as Next.js made sure `cookies` can still be called sync;
however, it is safer if we just migrate our calls to `cookies` to be
async 🙏🏽

### Checklist 📋

#### For code changes:

- [x] I have clearly listed my changes in the PR description
- [x] I have made a test plan
- [x] I have tested my changes according to the test plan:
- [x] There is no cookie warnings when running the FE dev server lcoally
  - [x] Authentication works as expected
This commit is contained in:
Ubbe
2025-06-06 15:24:55 +04:00
committed by GitHub
parent 12972fde77
commit f881570325
9 changed files with 22 additions and 18 deletions

View File

@@ -9,7 +9,7 @@ export async function GET(request: Request) {
const next = searchParams.get("next") ?? "/";
if (code) {
const supabase = getServerSupabase();
const supabase = await getServerSupabase();
if (!supabase) {
return NextResponse.redirect(`${origin}/error`);

View File

@@ -12,7 +12,7 @@ export async function GET(request: NextRequest) {
const next = searchParams.get("next") ?? "/";
if (token_hash && type) {
const supabase = getServerSupabase();
const supabase = await getServerSupabase();
if (!supabase) {
redirect("/error");

View File

@@ -21,7 +21,7 @@ export async function login(
turnstileToken: string,
) {
return await Sentry.withServerActionInstrumentation("login", {}, async () => {
const supabase = getServerSupabase();
const supabase = await getServerSupabase();
const api = new BackendAPI();
if (!supabase) {
@@ -60,7 +60,7 @@ export async function providerLogin(provider: LoginProvider) {
"providerLogin",
{},
async () => {
const supabase = getServerSupabase();
const supabase = await getServerSupabase();
const api = new BackendAPI();
if (!supabase) {

View File

@@ -6,7 +6,7 @@ import BackendApi from "@/lib/autogpt-server-api";
import { NotificationPreferenceDTO } from "@/lib/autogpt-server-api/types";
export async function updateSettings(formData: FormData) {
const supabase = getServerSupabase();
const supabase = await getServerSupabase();
const {
data: { user },
} = await supabase.auth.getUser();

View File

@@ -9,7 +9,7 @@ export async function sendResetEmail(email: string, turnstileToken: string) {
"sendResetEmail",
{},
async () => {
const supabase = getServerSupabase();
const supabase = await getServerSupabase();
const origin = process.env.FRONTEND_BASE_URL || "http://localhost:3000";
if (!supabase) {
@@ -42,7 +42,7 @@ export async function changePassword(password: string, turnstileToken: string) {
"changePassword",
{},
async () => {
const supabase = getServerSupabase();
const supabase = await getServerSupabase();
if (!supabase) {
redirect("/error");

View File

@@ -17,7 +17,7 @@ export async function signup(
"signup",
{},
async () => {
const supabase = getServerSupabase();
const supabase = await getServerSupabase();
if (!supabase) {
redirect("/error");

View File

@@ -89,21 +89,22 @@ export default class BackendAPI {
this.wsUrl = wsUrl;
}
private get supabaseClient(): SupabaseClient | null {
private async getSupabaseClient(): Promise<SupabaseClient | null> {
return isClient
? createBrowserClient(
process.env.NEXT_PUBLIC_SUPABASE_URL!,
process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY!,
{ isSingleton: true },
)
: getServerSupabase();
: await getServerSupabase();
}
async isAuthenticated(): Promise<boolean> {
if (!this.supabaseClient) return false;
const supabaseClient = await this.getSupabaseClient();
if (!supabaseClient) return false;
const {
data: { session },
} = await this.supabaseClient.auth.getSession();
} = await supabaseClient.auth.getSession();
return session != null;
}
@@ -750,9 +751,10 @@ export default class BackendAPI {
const maxRetries = 3;
while (retryCount < maxRetries) {
const supabaseClient = await this.getSupabaseClient();
const {
data: { session },
} = (await this.supabaseClient?.auth.getSession()) || {
} = (await supabaseClient?.auth.getSession()) || {
data: { session: null },
};
@@ -803,9 +805,10 @@ export default class BackendAPI {
const maxRetries = 3;
while (retryCount < maxRetries) {
const supabaseClient = await this.getSupabaseClient();
const {
data: { session },
} = (await this.supabaseClient?.auth.getSession()) || {
} = (await supabaseClient?.auth.getSession()) || {
data: { session: null },
};
@@ -980,8 +983,9 @@ export default class BackendAPI {
async connectWebSocket(): Promise<void> {
return (this.wsConnecting ??= new Promise(async (resolve, reject) => {
try {
const supabaseClient = await this.getSupabaseClient();
const token =
(await this.supabaseClient?.auth.getSession())?.data.session
(await supabaseClient?.auth.getSession())?.data.session
?.access_token || "";
const wsUrlWithToken = `${this.wsUrl}?token=${token}`;
this.webSocket = new WebSocket(wsUrlWithToken);

View File

@@ -1,10 +1,10 @@
import type { UnsafeUnwrappedCookies } from "next/headers";
import { createServerClient } from "@supabase/ssr";
export default function getServerSupabase() {
export default async function getServerSupabase() {
// Need require here, so Next.js doesn't complain about importing this on client side
const { cookies } = require("next/headers");
const cookieStore = cookies() as UnsafeUnwrappedCookies;
const cookieStore = await cookies();
try {
const supabase = createServerClient(

View File

@@ -1,7 +1,7 @@
import getServerSupabase from "./getServerSupabase";
const getServerUser = async () => {
const supabase = getServerSupabase();
const supabase = await getServerSupabase();
if (!supabase) {
return { user: null, error: "Failed to create Supabase client" };