The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.
TypeScript aialternativeauthdatabasedenoembeddingsexamplefirebasenextjsoauth2pgvectorpostgispostgrespostgresqlpostgrestrealtimesupabasevectorswebsockets
fix: only set headers.Authorization when access token is available to prevent token has wrong format error
phamhieu9c0c4449477b
diff --git a/studio/lib/common/fetch/base.ts b/studio/lib/common/fetch/base.tsindex d408d5813ff..0d517e85bdb 100644--- a/studio/lib/common/fetch/base.ts+++ b/studio/lib/common/fetch/base.ts@@ -69,7 +69,23 @@ export function getAccessToken() {if (!tokenData) return undefinedconst tokenObj = tryParseJson(tokenData)if (tokenObj === false) {- return ""+ return ''}return tokenObj.currentSession.access_token}++export function constructHeaders(requestId: string, optionHeaders?: { [prop: string]: any }) {+ let headers: { [prop: string]: any } = {+ 'Content-Type': 'application/json',+ Accept: 'application/json',+ 'X-Request-Id': requestId,+ ...optionHeaders,+ }++ if (process.env.NEXT_PUBLIC_GOTRUE_ENABLED) {+ const accessToken = getAccessToken()+ if (accessToken) headers.Authorization = `Bearer ${accessToken}`+ }++ return headers+}diff --git a/studio/lib/common/fetch/delete.ts b/studio/lib/common/fetch/delete.tsindex 7befcbdcf40..d9b27abc1a4 100644--- a/studio/lib/common/fetch/delete.ts+++ b/studio/lib/common/fetch/delete.ts@@ -1,4 +1,4 @@-import { handleError, handleResponse, handleResponseError, getAccessToken } from './base'+import { handleError, handleResponse, handleResponseError, constructHeaders } from './base'import { uuidv4 } from '../../helpers'import { SupaResponse } from 'types/base'@@ -8,21 +8,15 @@ export async function delete_<T = any>(options?: { [prop: string]: any }): Promise<SupaResponse<T>> {const requestId = uuidv4()- const { headers, ...otherOptions } = options ?? {}- const accessToken = getAccessToken()try {+ const { headers: optionHeaders, ...otherOptions } = options ?? {}+ const headers = constructHeaders(requestId, optionHeaders)const response = await fetch(url, {method: 'DELETE',body: JSON.stringify(data),credentials: 'include',referrerPolicy: 'no-referrer-when-downgrade',- headers: {- 'Content-Type': 'application/json',- Accept: 'application/json',- 'X-Request-Id': requestId,- 'Authorization': `Bearer ${accessToken}`,- ...headers,- },+ headers,...otherOptions,})diff --git a/studio/lib/common/fetch/get.ts b/studio/lib/common/fetch/get.tsindex e1c2c954578..50d389e23d1 100644--- a/studio/lib/common/fetch/get.ts+++ b/studio/lib/common/fetch/get.ts@@ -1,4 +1,4 @@-import { handleError, handleResponse, handleResponseError, getAccessToken } from './base'+import { handleError, handleResponse, handleResponseError, constructHeaders } from './base'import { uuidv4 } from '../../helpers'import { SupaResponse } from 'types/base'@@ -8,19 +8,13 @@ export async function get<T = any>(): Promise<SupaResponse<T>> {const requestId = uuidv4()try {- const { headers, ...otherOptions } = options ?? {}- const accessToken = getAccessToken()+ const { headers: optionHeaders, ...otherOptions } = options ?? {}+ const headers = constructHeaders(requestId, optionHeaders)const response = await fetch(url, {method: 'GET',credentials: 'include',referrerPolicy: 'no-referrer-when-downgrade',- headers: {- 'Content-Type': 'application/json',- Accept: 'application/json',- 'X-Request-Id': requestId,- 'Authorization': `Bearer ${accessToken}`,- ...headers,- },+ headers,...otherOptions,})if (!response.ok) return handleResponseError(response, requestId)diff --git a/studio/lib/common/fetch/patch.ts b/studio/lib/common/fetch/patch.tsindex 5c5ca12adf4..6a6be18d24a 100644--- a/studio/lib/common/fetch/patch.ts+++ b/studio/lib/common/fetch/patch.ts@@ -1,4 +1,4 @@-import { handleError, handleResponse, handleResponseError, getAccessToken } from './base'+import { constructHeaders, handleError, handleResponse, handleResponseError } from './base'import { uuidv4 } from '../../helpers'import { SupaResponse } from 'types/base'@@ -9,20 +9,14 @@ export async function patch<T = any>(): Promise<SupaResponse<T>> {const requestId = uuidv4()try {- const { headers, ...otherOptions } = options ?? {}- const accessToken = getAccessToken()+ const { headers: optionHeaders, ...otherOptions } = options ?? {}+ const headers = constructHeaders(requestId, optionHeaders)const response = await fetch(url, {method: 'PATCH',body: JSON.stringify(data),credentials: 'include',referrerPolicy: 'no-referrer-when-downgrade',- headers: {- 'Content-Type': 'application/json',- Accept: 'application/json',- 'X-Request-Id': requestId,- 'Authorization': `Bearer ${accessToken}`,- ...headers,- },+ headers,...otherOptions,})diff --git a/studio/lib/common/fetch/post.ts b/studio/lib/common/fetch/post.tsindex 890641c92d6..69c12dd0204 100644--- a/studio/lib/common/fetch/post.ts+++ b/studio/lib/common/fetch/post.ts@@ -1,4 +1,4 @@-import { handleError, handleResponse, handleResponseError, getAccessToken } from './base'+import { handleError, handleResponse, handleResponseError, constructHeaders } from './base'import { uuidv4 } from 'lib/helpers'import { SupaResponse } from 'types/base'@@ -9,20 +9,14 @@ export async function post<T = any>(): Promise<SupaResponse<T>> {const requestId = uuidv4()try {- const { headers, ...otherOptions } = options ?? {}- const accessToken = getAccessToken()+ const { headers: optionHeaders, ...otherOptions } = options ?? {}+ const headers = constructHeaders(requestId, optionHeaders)const response = await fetch(url, {method: 'POST',body: JSON.stringify(data),credentials: 'include',referrerPolicy: 'no-referrer-when-downgrade',- headers: {- 'Content-Type': 'application/json',- Accept: 'application/json',- 'X-Request-Id': requestId,- 'Authorization': `Bearer ${accessToken}`,- ...headers,- },+ headers,...otherOptions,})if (!response.ok) return handleResponseError(response, requestId)