Skip to content

The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.

fix: update http methods to send authorization header

Kang Ming95cc1ee9b5d6
diff --git a/studio/lib/common/fetch/base.ts b/studio/lib/common/fetch/base.ts
index 63eec4a3664..d408d5813ff 100644
--- a/studio/lib/common/fetch/base.ts
+++ b/studio/lib/common/fetch/base.ts
@@ -1,3 +1,4 @@
+import { tryParseJson } from 'lib/helpers'
import { SupaResponse } from 'types/base'
export function handleError<T>(e: any, requestId: string): SupaResponse<T> {
@@ -62,3 +63,13 @@ export async function handleResponseError<T = unknown>(
return { error } as unknown as SupaResponse<T>
}
}
+
+export function getAccessToken() {
+ const tokenData = window?.localStorage['supabase.auth.token']
+ if (!tokenData) return undefined
+ const tokenObj = tryParseJson(tokenData)
+ if (tokenObj === false) {
+ return ""
+ }
+ return tokenObj.currentSession.access_token
+}
diff --git a/studio/lib/common/fetch/delete.ts b/studio/lib/common/fetch/delete.ts
index be40673d20e..7befcbdcf40 100644
--- a/studio/lib/common/fetch/delete.ts
+++ b/studio/lib/common/fetch/delete.ts
@@ -1,4 +1,4 @@
-import { handleError, handleResponse, handleResponseError } from './base'
+import { handleError, handleResponse, handleResponseError, getAccessToken } from './base'
import { uuidv4 } from '../../helpers'
import { SupaResponse } from 'types/base'
@@ -9,6 +9,7 @@ export async function delete_<T = any>(
): Promise<SupaResponse<T>> {
const requestId = uuidv4()
const { headers, ...otherOptions } = options ?? {}
+ const accessToken = getAccessToken()
try {
const response = await fetch(url, {
method: 'DELETE',
@@ -19,6 +20,7 @@ export async function delete_<T = any>(
'Content-Type': 'application/json',
Accept: 'application/json',
'X-Request-Id': requestId,
+ 'Authorization': `Bearer ${accessToken}`,
...headers,
},
...otherOptions,
diff --git a/studio/lib/common/fetch/get.ts b/studio/lib/common/fetch/get.ts
index 393fcd6989c..e1c2c954578 100644
--- a/studio/lib/common/fetch/get.ts
+++ b/studio/lib/common/fetch/get.ts
@@ -1,4 +1,4 @@
-import { handleError, handleResponse, handleResponseError } from './base'
+import { handleError, handleResponse, handleResponseError, getAccessToken } from './base'
import { uuidv4 } from '../../helpers'
import { SupaResponse } from 'types/base'
@@ -9,6 +9,7 @@ export async function get<T = any>(
const requestId = uuidv4()
try {
const { headers, ...otherOptions } = options ?? {}
+ const accessToken = getAccessToken()
const response = await fetch(url, {
method: 'GET',
credentials: 'include',
@@ -17,6 +18,7 @@ export async function get<T = any>(
'Content-Type': 'application/json',
Accept: 'application/json',
'X-Request-Id': requestId,
+ 'Authorization': `Bearer ${accessToken}`,
...headers,
},
...otherOptions,
diff --git a/studio/lib/common/fetch/patch.ts b/studio/lib/common/fetch/patch.ts
index e64f5f9ebf4..5c5ca12adf4 100644
--- a/studio/lib/common/fetch/patch.ts
+++ b/studio/lib/common/fetch/patch.ts
@@ -1,4 +1,4 @@
-import { handleError, handleResponse, handleResponseError } from './base'
+import { handleError, handleResponse, handleResponseError, getAccessToken } from './base'
import { uuidv4 } from '../../helpers'
import { SupaResponse } from 'types/base'
@@ -10,6 +10,7 @@ export async function patch<T = any>(
const requestId = uuidv4()
try {
const { headers, ...otherOptions } = options ?? {}
+ const accessToken = getAccessToken()
const response = await fetch(url, {
method: 'PATCH',
body: JSON.stringify(data),
@@ -19,6 +20,7 @@ export async function patch<T = any>(
'Content-Type': 'application/json',
Accept: 'application/json',
'X-Request-Id': requestId,
+ 'Authorization': `Bearer ${accessToken}`,
...headers,
},
...otherOptions,
diff --git a/studio/lib/common/fetch/post.ts b/studio/lib/common/fetch/post.ts
index a58d3164aa9..890641c92d6 100644
--- a/studio/lib/common/fetch/post.ts
+++ b/studio/lib/common/fetch/post.ts
@@ -1,4 +1,4 @@
-import { handleError, handleResponse, handleResponseError } from './base'
+import { handleError, handleResponse, handleResponseError, getAccessToken } from './base'
import { uuidv4 } from 'lib/helpers'
import { SupaResponse } from 'types/base'
@@ -10,6 +10,7 @@ export async function post<T = any>(
const requestId = uuidv4()
try {
const { headers, ...otherOptions } = options ?? {}
+ const accessToken = getAccessToken()
const response = await fetch(url, {
method: 'POST',
body: JSON.stringify(data),
@@ -19,6 +20,7 @@ export async function post<T = any>(
'Content-Type': 'application/json',
Accept: 'application/json',
'X-Request-Id': requestId,
+ 'Authorization': `Bearer ${accessToken}`,
...headers,
},
...otherOptions,