Skip to content

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

Add revoke authorized app + update endpoints with latest changes

Joshen Limadbbf1bba774
diff --git a/studio/components/interfaces/Organization/OAuthApps/AuthorizedAppRow.tsx b/studio/components/interfaces/Organization/OAuthApps/AuthorizedAppRow.tsx
new file mode 100644
index 00000000000..9fe9939ec70
--- /dev/null
+++ b/studio/components/interfaces/Organization/OAuthApps/AuthorizedAppRow.tsx
@@ -0,0 +1,49 @@
+import Table from 'components/to-be-cleaned/Table'
+import { AuthorizedApp } from 'data/oauth/authorized-apps-query'
+import dayjs from 'dayjs'
+import { copyToClipboard } from 'lib/helpers'
+import { useState } from 'react'
+import { Button, IconCheck, IconClipboard, IconTrash } from 'ui'
+
+export interface AuthorizedAppRowProps {
+ app: AuthorizedApp
+ onSelectRevoke: () => void
+}
+
+const AuthorizedAppRow = ({ app, onSelectRevoke }: AuthorizedAppRowProps) => {
+ const [isCopied, setIsCopied] = useState(false)
+
+ return (
+ <Table.tr>
+ <Table.td>{app.name}</Table.td>
+ <Table.td>
+ <span className="font-mono truncate">{app.id}</span>
+ <Button
+ type="default"
+ icon={
+ isCopied ? <IconCheck className="text-brand-900" strokeWidth={3} /> : <IconClipboard />
+ }
+ className="ml-2 px-1"
+ onClick={() => {
+ copyToClipboard(app.id)
+ setIsCopied(true)
+ setTimeout(() => {
+ setIsCopied(false)
+ }, 3000)
+ }}
+ />
+ </Table.td>
+ <Table.td>{dayjs(app.authorized_at).format('DD/MM/YYYY, HH:mm:ss')}</Table.td>
+ <Table.td align="right">
+ <Button
+ type="default"
+ icon={<IconTrash />}
+ className="px-1"
+ onClick={() => onSelectRevoke()}
+ />
+ </Table.td>
+ </Table.tr>
+ )
+}
+
+export default AuthorizedAppRow
diff --git a/studio/components/interfaces/Organization/OAuthApps/OAuthApps.tsx b/studio/components/interfaces/Organization/OAuthApps/OAuthApps.tsx
index 1e1cda4a0c4..16a57ed315a 100644
--- a/studio/components/interfaces/Organization/OAuthApps/OAuthApps.tsx
+++ b/studio/components/interfaces/Organization/OAuthApps/OAuthApps.tsx
@@ -10,6 +10,9 @@ import { Alert, Button, IconX, Input } from 'ui'
import DeleteAppModal from './DeleteAppModal'
import OAuthAppRow from './OAuthAppRow'
import PublishAppModal from './PublishAppModal'
+import { AuthorizedApp, useAuthorizedAppsQuery } from 'data/oauth/authorized-apps-query'
+import AuthorizedAppRow from './AuthorizedAppRow'
+import RevokeAppModal from './RevokeAppModal'
const OAuthApps = () => {
const { slug } = useParams()
@@ -17,16 +20,14 @@ const OAuthApps = () => {
const [showPublishModal, setShowPublishModal] = useState(false)
const [selectedAppToUpdate, setSelectedAppToUpdate] = useState<OAuthApp>()
const [selectedAppToDelete, setSelectedAppToDelete] = useState<OAuthApp>()
+ const [selectedAppToRevoke, setSelectedAppToRevoke] = useState<AuthorizedApp>()
const {
data: publishedApps,
isLoading: isLoadingPublishedApps,
isSuccess: isSuccessPublishedApps,
isError: isErrorPublishedApps,
- } = useOAuthAppsQuery({
- slug,
- type: 'published',
- })
+ } = useOAuthAppsQuery({ slug })
const sortedPublishedApps = publishedApps?.sort((a, b) => {
return Number(new Date(a.created_at)) - Number(new Date(b.created_at))
@@ -37,13 +38,10 @@ const OAuthApps = () => {
isLoading: isLoadingAuthorizedApps,
isSuccess: isSuccessAuthorizedApps,
isError: isErrorAuthorizedApps,
- } = useOAuthAppsQuery({
- slug,
- type: 'authorized',
- })
+ } = useAuthorizedAppsQuery({ slug })
const sortedAuthorizedApps = authorizedApps?.sort((a, b) => {
- return Number(new Date(a.created_at)) - Number(new Date(b.created_at))
+ return Number(new Date(a.authorized_at)) - Number(new Date(b.authorized_at))
})
return (
@@ -176,7 +174,24 @@ const OAuthApps = () => {
<p className="prose text-sm">You do not have any authorized applications yet</p>
</div>
) : (
- <div></div>
+ <Table
+ className="mt-4"
+ head={[
+ <Table.th key="name">Name</Table.th>,
+ <Table.th key="name">ID</Table.th>,
+ <Table.th key="client-secret">Authorized at</Table.th>,
+ <Table.th key="delete-action"></Table.th>,
+ ]}
+ body={
+ sortedAuthorizedApps?.map((app) => (
+ <AuthorizedAppRow
+ key={app.id}
+ app={app}
+ onSelectRevoke={() => setSelectedAppToRevoke(app)}
+ />
+ )) ?? []
+ }
+ />
)}
</>
)}
@@ -197,6 +212,10 @@ const OAuthApps = () => {
selectedApp={selectedAppToDelete}
onClose={() => setSelectedAppToDelete(undefined)}
/>
+ <RevokeAppModal
+ selectedApp={selectedAppToRevoke}
+ onClose={() => setSelectedAppToRevoke(undefined)}
+ />
</>
)
}
diff --git a/studio/components/interfaces/Organization/OAuthApps/RevokeAppModal.tsx b/studio/components/interfaces/Organization/OAuthApps/RevokeAppModal.tsx
new file mode 100644
index 00000000000..a13efda8a0c
--- /dev/null
+++ b/studio/components/interfaces/Organization/OAuthApps/RevokeAppModal.tsx
@@ -0,0 +1,59 @@
+import { useParams } from 'common'
+import { useAuthorizedAppRevokeMutation } from 'data/oauth/authorized-app-revoke-mutation'
+import { AuthorizedApp } from 'data/oauth/authorized-apps-query'
+import { useStore } from 'hooks'
+import { Alert, Modal } from 'ui'
+
+export interface RevokeAppModalProps {
+ selectedApp?: AuthorizedApp
+ onClose: () => void
+}
+
+const RevokeAppModal = ({ selectedApp, onClose }: RevokeAppModalProps) => {
+ const { ui } = useStore()
+ const { slug } = useParams()
+ const { mutateAsync: revokeAuthorizedApp, isLoading: isDeleting } =
+ useAuthorizedAppRevokeMutation()
+
+ const onConfirmDelete = async () => {
+ if (!slug) return console.error('Slug is required')
+ if (!selectedApp?.id) return console.error('App ID is required')
+
+ try {
+ await revokeAuthorizedApp({ slug, id: selectedApp?.id })
+ ui.setNotification({
+ category: 'success',
+ message: `Successfully revoked the app "${selectedApp?.name}"`,
+ })
+ onClose()
+ } catch (error) {
+ ui.setNotification({
+ category: 'error',
+ message: `Failed to revoke app: ${(error as any).message}`,
+ })
+ }
+ }
+
+ return (
+ <Modal
+ size="medium"
+ alignFooter="right"
+ header={`Confirm to revoke ${selectedApp?.name}`}
+ visible={selectedApp !== undefined}
+ loading={false} // [TO UPDATE]
+ onCancel={onClose}
+ onConfirm={onConfirmDelete}
+ >
+ <Modal.Content>
+ <div className="py-4">
+ <Alert withIcon variant="warning" title="This action cannot be undone">
+ This application will no longer have access to your organization's settings and
+ projects.
+ </Alert>
+ </div>
+ </Modal.Content>
+ </Modal>
+ )
+}
+
+export default RevokeAppModal
diff --git a/studio/components/ui/AlertError.tsx b/studio/components/ui/AlertError.tsx
index 04294793831..23174bc0dac 100644
--- a/studio/components/ui/AlertError.tsx
+++ b/studio/components/ui/AlertError.tsx
@@ -19,7 +19,7 @@ const AlertError = ({ ref, subject }: AlertErrorProps) => {
<Alert
withIcon
variant="warning"
- title="Failed to retrieve upcoming invoice details"
+ title={subject}
actions={[
<Link key="contact-support" href={href}>
<a>
diff --git a/studio/data/oauth/authorized-app-revoke-mutation.ts b/studio/data/oauth/authorized-app-revoke-mutation.ts
new file mode 100644
index 00000000000..d47499a4a77
--- /dev/null
+++ b/studio/data/oauth/authorized-app-revoke-mutation.ts
@@ -0,0 +1,44 @@
+import { useMutation, UseMutationOptions, useQueryClient } from '@tanstack/react-query'
+import { delete_ } from 'lib/common/fetch'
+import { API_ADMIN_URL } from 'lib/constants'
+import { oauthAppKeys } from './keys'
+
+export type AuthorizedAppRevokeVariables = {
+ id: string
+ slug: string
+}
+
+export async function revokeAuthorizedApp({ id, slug }: AuthorizedAppRevokeVariables) {
+ if (!id) throw new Error('App ID is required')
+ if (!slug) throw new Error('Organization slug is required')
+
+ const response = await delete_(
+ `${API_ADMIN_URL}/organizations/${slug}/oauth/apps/${id}?type=authorized`
+ )
+ if (response.error) throw response.error
+ return response
+}
+
+type AuthorizedAppRevokeData = Awaited<ReturnType<typeof revokeAuthorizedApp>>
+
+export const useAuthorizedAppRevokeMutation = ({
+ onSuccess,
+ ...options
+}: Omit<
+ UseMutationOptions<AuthorizedAppRevokeData, unknown, AuthorizedAppRevokeVariables>,
+ 'mutationFn'
+> = {}) => {
+ const queryClient = useQueryClient()
+
+ return useMutation<AuthorizedAppRevokeData, unknown, AuthorizedAppRevokeVariables>(
+ (vars) => revokeAuthorizedApp(vars),
+ {
+ async onSuccess(data, variables, context) {
+ const { slug } = variables
+ await queryClient.invalidateQueries(oauthAppKeys.authorizedApps(slug))
+ await onSuccess?.(data, variables, context)
+ },
+ ...options,
+ }
+ )
+}
diff --git a/studio/data/oauth/authorized-apps-query.ts b/studio/data/oauth/authorized-apps-query.ts
new file mode 100644
index 00000000000..6c37cdaf441
--- /dev/null
+++ b/studio/data/oauth/authorized-apps-query.ts
@@ -0,0 +1,59 @@
+import { useQuery, useQueryClient, UseQueryOptions } from '@tanstack/react-query'
+import { get } from 'lib/common/fetch'
+import { API_ADMIN_URL } from 'lib/constants'
+import { useCallback } from 'react'
+import { oauthAppKeys } from './keys'
+import { ResponseError } from 'types'
+
+export type AuthorizedAppsVariables = {
+ slug?: string
+}
+
+export type AuthorizedApp = {
+ id: string
+ icon: string | null
+ name: string
+ website: string
+ authorized_at: string
+}
+
+export async function getAuthorizedApps({ slug }: AuthorizedAppsVariables, signal?: AbortSignal) {
+ if (!slug) throw new Error('Organization slug is required')
+
+ const response = await get(`${API_ADMIN_URL}/organizations/${slug}/oauth/apps?type=authorized`, {
+ signal,
+ })
+ if (response.error) throw response.error
+ return response as AuthorizedApp[]
+}
+
+export type AuthorizedAppsData = Awaited<ReturnType<typeof getAuthorizedApps>>
+export type AuthorizedAppsError = ResponseError
+
+export const useAuthorizedAppsQuery = <TData = AuthorizedAppsData>(
+ { slug }: AuthorizedAppsVariables,
+ {
+ enabled = true,
+ ...options
+ }: UseQueryOptions<AuthorizedAppsData, AuthorizedAppsError, TData> = {}
+) =>
+ useQuery<AuthorizedAppsData, AuthorizedAppsError, TData>(
+ oauthAppKeys.authorizedApps(slug),
+ ({ signal }) => getAuthorizedApps({ slug }, signal),
+ {
+ enabled: enabled && typeof slug !== 'undefined',
+ ...options,
+ }
+ )
+
+export const useAuthorizedAppsPrefetch = ({ slug }: AuthorizedAppsVariables) => {
+ const client = useQueryClient()
+
+ return useCallback(() => {
+ if (slug) {
+ client.prefetchQuery(oauthAppKeys.authorizedApps(slug), ({ signal }) =>
+ getAuthorizedApps({ slug }, signal)
+ )
+ }
+ }, [slug])
+}
diff --git a/studio/data/oauth/keys.ts b/studio/data/oauth/keys.ts
index 8f37c6edff0..53b02310942 100644
--- a/studio/data/oauth/keys.ts
+++ b/studio/data/oauth/keys.ts
@@ -1,4 +1,4 @@
export const oauthAppKeys = {
- list: (slug: string | undefined, type: 'published' | 'authorized' | undefined) =>
- ['oauth-apps', slug, type] as const,
+ oauthApps: (slug: string | undefined) => [slug, 'oauth-apps'] as const,
+ authorizedApps: (slug: string | undefined) => [slug, 'authorized-apps'] as const,
}
diff --git a/studio/data/oauth/oauth-app-create-mutation.ts b/studio/data/oauth/oauth-app-create-mutation.ts
index 33f29020105..14152e5472f 100644
--- a/studio/data/oauth/oauth-app-create-mutation.ts
+++ b/studio/data/oauth/oauth-app-create-mutation.ts
@@ -54,7 +54,7 @@ export const useOAuthAppCreateMutation = ({
{
async onSuccess(data, variables, context) {
const { slug } = variables
- await queryClient.invalidateQueries(oauthAppKeys.list(slug, 'published'))
+ await queryClient.invalidateQueries(oauthAppKeys.oauthApps(slug))
await onSuccess?.(data, variables, context)
},
...options,
diff --git a/studio/data/oauth/oauth-app-delete-mutation.ts b/studio/data/oauth/oauth-app-delete-mutation.ts
index ea97b3eddcb..28592be674b 100644
--- a/studio/data/oauth/oauth-app-delete-mutation.ts
+++ b/studio/data/oauth/oauth-app-delete-mutation.ts
@@ -12,7 +12,9 @@ export async function deleteOAuthApp({ id, slug }: OAuthAppDeleteVariables) {
if (!id) throw new Error('OAuth app ID is required')
if (!slug) throw new Error('Organization slug is required')
- const response = await delete_(`${API_ADMIN_URL}/organizations/${slug}/oauth/apps/${id}`)
+ const response = await delete_(
+ `${API_ADMIN_URL}/organizations/${slug}/oauth/apps/${id}?type=published`
+ )
if (response.error) throw response.error
return response
}
@@ -33,7 +35,7 @@ export const useOAuthAppDeleteMutation = ({
{
async onSuccess(data, variables, context) {
const { slug } = variables
- await queryClient.invalidateQueries(oauthAppKeys.list(slug, 'published'))
+ await queryClient.invalidateQueries(oauthAppKeys.oauthApps(slug))
await onSuccess?.(data, variables, context)
},
...options,
diff --git a/studio/data/oauth/oauth-app-update-mutation.ts b/studio/data/oauth/oauth-app-update-mutation.ts
index 612c1437dd6..acc4ab79f3b 100644
--- a/studio/data/oauth/oauth-app-update-mutation.ts
+++ b/studio/data/oauth/oauth-app-update-mutation.ts
@@ -52,7 +52,7 @@ export const useOAuthAppUpdateMutation = ({
{
async onSuccess(data, variables, context) {
const { slug } = variables
- await queryClient.invalidateQueries(oauthAppKeys.list(slug, 'published'))
+ await queryClient.invalidateQueries(oauthAppKeys.oauthApps(slug))
await onSuccess?.(data, variables, context)
},
...options,
diff --git a/studio/data/oauth/oauth-apps-query.ts b/studio/data/oauth/oauth-apps-query.ts
index 2ca77192e17..5dc6a229ce1 100644
--- a/studio/data/oauth/oauth-apps-query.ts
+++ b/studio/data/oauth/oauth-apps-query.ts
@@ -7,7 +7,6 @@ import { ResponseError } from 'types'
export type OAuthAppsVariables = {
slug?: string
- type?: 'published' | 'authorized'
}
export type OAuthApp = {
@@ -21,11 +20,10 @@ export type OAuthApp = {
redirect_uris: string[]
}
-export async function getOAuthApps({ slug, type }: OAuthAppsVariables, signal?: AbortSignal) {
+export async function getOAuthApps({ slug }: OAuthAppsVariables, signal?: AbortSignal) {
if (!slug) throw new Error('Organization slug is required')
- if (!type) throw new Error('App type is required')
- const response = await get(`${API_ADMIN_URL}/organizations/${slug}/oauth/apps?type=${type}`, {
+ const response = await get(`${API_ADMIN_URL}/organizations/${slug}/oauth/apps?type=published`, {
signal,
})
if (response.error) throw response.error
@@ -36,25 +34,25 @@ export type OAuthAppsData = Awaited<ReturnType<typeof getOAuthApps>>
export type OAuthAppsError = ResponseError
export const useOAuthAppsQuery = <TData = OAuthAppsData>(
- { slug, type }: OAuthAppsVariables,
+ { slug }: OAuthAppsVariables,
{ enabled = true, ...options }: UseQueryOptions<OAuthAppsData, OAuthAppsError, TData> = {}
) =>
useQuery<OAuthAppsData, OAuthAppsError, TData>(
- oauthAppKeys.list(slug, type),
- ({ signal }) => getOAuthApps({ slug, type }, signal),
+ oauthAppKeys.oauthApps(slug),
+ ({ signal }) => getOAuthApps({ slug }, signal),
{
- enabled: enabled && typeof slug !== 'undefined' && typeof type !== 'undefined',
+ enabled: enabled && typeof slug !== 'undefined',
...options,
}
)
-export const useOAuthAppsPrefetch = ({ slug, type }: OAuthAppsVariables) => {
+export const useOAuthAppsPrefetch = ({ slug }: OAuthAppsVariables) => {
const client = useQueryClient()
return useCallback(() => {
- if (slug && type) {
- client.prefetchQuery(oauthAppKeys.list(slug, type), ({ signal }) =>
- getOAuthApps({ slug, type }, signal)
+ if (slug) {
+ client.prefetchQuery(oauthAppKeys.oauthApps(slug), ({ signal }) =>
+ getOAuthApps({ slug }, signal)
)
}
}, [slug])