From 95cc1ee9b5d6cc67affaffc5dd780ccc25639c0c Mon Sep 17 00:00:00 2001 From: Kang Ming Date: Mon, 6 Dec 2021 20:35:24 +0800 Subject: [PATCH] fix: update http methods to send authorization header --- studio/lib/common/fetch/base.ts | 11 +++++++++++ studio/lib/common/fetch/delete.ts | 4 +++- studio/lib/common/fetch/get.ts | 4 +++- studio/lib/common/fetch/patch.ts | 4 +++- studio/lib/common/fetch/post.ts | 4 +++- 5 files changed, 23 insertions(+), 4 deletions(-) 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(e: any, requestId: string): SupaResponse { @@ -62,3 +63,13 @@ export async function handleResponseError( return { error } as unknown as SupaResponse } } + +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_( ): Promise> { 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_( '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( 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( '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( 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( '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( 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( 'Content-Type': 'application/json', Accept: 'application/json', 'X-Request-Id': requestId, + 'Authorization': `Bearer ${accessToken}`, ...headers, }, ...otherOptions,