Skip to content

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

feat: Add automatic login flow support (#18641)

Kamil Ogórekebc778805393
diff --git a/studio/data/api.d.ts b/studio/data/api.d.ts
index 82697fa9173..f0321f3a6e2 100644
--- a/studio/data/api.d.ts
+++ b/studio/data/api.d.ts
@@ -808,6 +808,14 @@ export interface paths {
/** Gets github pull requests for a given repo */
get: operations['GitHubPullRequestController_getPullRequests']
}
+ '/platform/cli/login': {
+ /** Create CLI login session */
+ post: operations['CliLoginController_createCliLoginSession']
+ }
+ '/platform/cli/login/{session_id}': {
+ /** Retrieve CLI login session */
+ get: operations['CliLoginController_getCliLoginSession']
+ }
'/system/auth/{ref}/templates/{template}': {
/** Gets GoTrue template */
get: operations['AuthTemplateController_getTemplate']
@@ -10263,6 +10271,25 @@ export interface operations {
}
}
}
+ /** Create CLI login session */
+ CliLoginController_createCliLoginSession: {
+ requestBody: {
+ content: {
+ 'application/json': components['schemas']['CreateCliLoginSessionBody']
+ }
+ }
+ responses: {
+ /** @description Failed to create CLI login session */
+ 500: never
+ }
+ }
+ /** Retrieve CLI login session */
+ CliLoginController_getCliLoginSession: {
+ responses: {
+ /** @description Failed to retrieve CLI login session */
+ 500: never
+ }
+ }
/** Gets GoTrue template */
AuthTemplateController_getTemplate: {
parameters: {
diff --git a/studio/data/cli/login.ts b/studio/data/cli/login.ts
new file mode 100644
index 00000000000..704e940a654
--- /dev/null
+++ b/studio/data/cli/login.ts
@@ -0,0 +1,23 @@
+import { post } from 'data/fetchers'
+
+export async function createCliLoginSession(
+ sessionId: string,
+ publicKey: string,
+ tokenName?: string
+) {
+ if (!sessionId) {
+ throw new Error('sessionId is required')
+ }
+
+ const { data, error } = await post(`/platform/cli/login`, {
+ body: {
+ session_id: sessionId,
+ public_key: publicKey,
+ token_name: tokenName,
+ },
+ })
+
+ if (error) throw error
+
+ return data
+}
diff --git a/studio/pages/cli/login.tsx b/studio/pages/cli/login.tsx
new file mode 100644
index 00000000000..317a3c7e6c8
--- /dev/null
+++ b/studio/pages/cli/login.tsx
@@ -0,0 +1,72 @@
+import { NextPage } from 'next'
+import toast from 'react-hot-toast'
+import APIAuthorizationLayout from 'components/layouts/APIAuthorizationLayout'
+import { createCliLoginSession } from 'data/cli/login'
+import { useParams } from 'common'
+import { useRouter } from 'next/router'
+import { withAuth } from 'hooks'
+import Connecting from 'components/ui/Loading/Loading'
+import { useEffect, useState } from 'react'
+import Link from 'next/link'
+
+const CliLoginPage: NextPage = () => {
+ const router = useRouter()
+ const { session_id, public_key, token_name, success } = useParams()
+ const [isSuccessfulLogin, setSuccessfulLogin] = useState(false)
+
+ useEffect(() => {
+ if (!router.isReady) {
+ return
+ }
+
+ if (success) {
+ setSuccessfulLogin(true)
+ return
+ }
+
+ async function createSession() {
+ if (!session_id || !public_key) {
+ router.push('/404')
+ return
+ }
+
+ try {
+ const session = await createCliLoginSession(session_id, public_key, token_name)
+
+ if (session) {
+ router.push(`/cli/login?success=true`)
+ } else {
+ router.push(`/404`)
+ }
+ } catch (error: any) {
+ toast.error(`Failed to create login session: ${error.message}`)
+ router.push(`/500`)
+ }
+ }
+
+ createSession()
+ }, [router, router.isReady, session_id, public_key, token_name, success])
+
+ return (
+ <APIAuthorizationLayout>
+ <div className={`flex flex-col items-center justify-center h-full`}>
+ {isSuccessfulLogin ? (
+ <>
+ <p>Well done! Now close this window, go back to your terminal and hack away!</p>
+ <p>
+ If you ever want to remove your new token, go to{' '}
+ <Link href="/account/tokens" className="underline">
+ Access Tokens
+ </Link>{' '}
+ page.
+ </p>
+ </>
+ ) : (
+ <Connecting />
+ )}
+ </div>
+ </APIAuthorizationLayout>
+ )
+}
+
+export default withAuth(CliLoginPage)