mirror of
https://github.com/supabase/supabase.git
synced 2026-10-06 01:45:10 +03:00
fix: update http methods to send authorization header
This commit is contained in:
1 parent
b01f6f0892
commit
95cc1ee9b5
5 files changed
+23
-4
No files matched your search
@@ -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
|
||||
}
|
||||
@@ -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,
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -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,
|
||||
|
||||
Reference in new issue
Block a user