Skip to content

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

fix: getAccessToken from url fragment when it's available

phamhieu9ba410d72eb4
diff --git a/studio/lib/common/fetch/base.ts b/studio/lib/common/fetch/base.ts
index 0d517e85bdb..c63cba4aef7 100644
--- a/studio/lib/common/fetch/base.ts
+++ b/studio/lib/common/fetch/base.ts
@@ -66,7 +66,12 @@ export async function handleResponseError<T = unknown>(
export function getAccessToken() {
const tokenData = window?.localStorage['supabase.auth.token']
- if (!tokenData) return undefined
+ if (!tokenData) {
+ // try to get from url fragment
+ const access_token = getParameterByName('access_token')
+ if (access_token) return access_token
+ else return undefined
+ }
const tokenObj = tryParseJson(tokenData)
if (tokenObj === false) {
return ''
@@ -74,6 +79,18 @@ export function getAccessToken() {
return tokenObj.currentSession.access_token
}
+// get param from URL fragment
+function getParameterByName(name: string, url?: string) {
+ if (!url) url = window?.location?.href || ''
+ // eslint-disable-next-line no-useless-escape
+ name = name.replace(/[\[\]]/g, '\\$&')
+ const regex = new RegExp('[?&#]' + name + '(=([^&#]*)|&|#|$)'),
+ results = regex.exec(url)
+ if (!results) return null
+ if (!results[2]) return ''
+ return decodeURIComponent(results[2].replace(/\+/g, ' '))
+}
+
export function constructHeaders(requestId: string, optionHeaders?: { [prop: string]: any }) {
let headers: { [prop: string]: any } = {
'Content-Type': 'application/json',
diff --git a/studio/pages/callback.tsx b/studio/pages/callback.tsx
deleted file mode 100644
index e9537707a5b..00000000000
--- a/studio/pages/callback.tsx
+++ /dev/null
@@ -1,43 +0,0 @@
-import Head from 'next/head'
-import { NextPage } from 'next'
-import { useEffect } from 'react'
-import { useRouter } from 'next/router'
-import Connecting from 'components/ui/Loading'
-import { auth } from 'lib/gotrue'
-
-const AuthCallback: NextPage = () => {
- const router = useRouter()
-
- useEffect(() => {
- auth.onAuthStateChange((event) => {
- if (event == 'SIGNED_IN') {
- router.push('/')
- }
- })
-
- // 5 seconds timeout
- const timer = setTimeout(() => {
- router.push('/')
- }, 5000)
- return () => clearTimeout(timer)
- }, [])
-
- return (
- <>
- <Head>
- <title>Supabase</title>
- <meta name="description" content="Supabase Studio" />
- <link rel="icon" href="/favicon.ico" />
- </Head>
- <div className="flex h-full">
- <main
- style={{ maxHeight: '100vh' }}
- className="w-full flex flex-col flex-1 overflow-y-auto"
- >
- <Connecting />
- </main>
- </div>
- </>
- )
-}
-export default AuthCallback