OAuth consent and connected agents
Serve the consent page for the Supabase Auth OAuth server from your app, and let users see and disconnect the agents they approved.
When the Supabase Auth OAuth server is on, an MCP client or another OAuth
client signs a user in by sending them to a consent page in your app. The
user approves or denies, and Auth redirects back to the client with a code or
an access_denied error. The Next.js example serves that page at
/oauth/consent and lists the approved clients under Settings, Security.
Both run in the browser with the Supabase client the app already has.
Configuration
Auth sends users to authorization_url_path, relative to the Auth Site URL:
[auth.oauth_server]
enabled = true
authorization_url_path = "/oauth/consent"The page needs a signed-in user. Leave it out of the proxy's public paths:
a signed-out visitor goes to sign-in with the consent URL (including
authorization_id) in next, and the login form returns them to it through
safeNext. The MCP blocks guide
covers the rest of the OAuth server setup.
The consent page
useOAuthConsent (createOAuthConsentController in
better-supabase/blocks/sessions) reads the request for
authorization_id. The React hook is a registry item: install it with
npx shadcn add @better-supabase/use-sessions, which writes
hooks/use-sessions.ts into the app. Vue, Svelte and Solid run the
controller with useBlockController from better-supabase/vue, /svelte
and /solid (see
Block controllers).
Auth answers with the client, the user and the requested scopes, or with a
redirect_url when the user approved this client before; the controller
then leaves at once:
"use client";
import { useOAuthConsent } from "@/hooks/use-sessions";
const { request, deciding, error, approve, deny } = useOAuthConsent({
authorizationId, // from the URL
redirect: (url) => window.location.replace(url),
});
// request.client.name, request.user.email, request.scopes, request.redirectHostFor a client the user already approved, the first read uses up the request,
and a second read (React Strict Mode runs effects twice in development)
fails with "authorization request cannot be processed". The controller
shares the read per authorization_id.
approve() and deny() call Auth with skipBrowserRedirect, then go to
the redirect_url it returns. deciding stays set while the browser
leaves, so keep the buttons disabled on it. A failed decision keeps the
request and sets error.
The browser only ever goes to a redirect_url that Auth returned, which
points at a redirect URI the client registered. Show redirectHost on the
page so the user sees where approving sends them.
The approved client acts as the user: its tokens carry the user's sub, so
RLS and every repository call see the same rows the user does. The page says
so next to the scopes.
In Vue, Svelte and Solid, pass oauth to the controller yourself:
<script setup lang="ts">
import { useBlockController, useSupabase } from "better-supabase/vue";
import { createOAuthConsentController } from "better-supabase/blocks/sessions";
const props = defineProps<{ authorizationId: string }>();
const { state, actions } = useBlockController(createOAuthConsentController, {
oauth: useSupabase().auth.oauth,
authorizationId: props.authorizationId,
});
</script>
<template>
<p v-if="state.request">Connect {{ state.request.client.name }}?</p>
<button :disabled="state.deciding !== undefined" @click="actions.approve()">
Approve
</button>
</template><script lang="ts">
import { betterSupabase, useBlockController } from "better-supabase/svelte";
import { createOAuthGrantsController } from "better-supabase/blocks/sessions";
const { supabase } = betterSupabase();
const grants = useBlockController(createOAuthGrantsController, {
oauth: supabase.auth.oauth,
});
</script>
<p>{$grants.grants?.length ?? 0} connected agents</p>import { useBlockController, useSupabase } from "better-supabase/solid";
import { createOAuthGrantsController } from "better-supabase/blocks/sessions";
export function Grants() {
const { state } = useBlockController(createOAuthGrantsController, {
oauth: useSupabase().auth.oauth,
});
return <p>{state().grants?.length ?? 0} connected agents</p>;
}Without a framework, createOAuthConsentController and
createOAuthGrantsController take the same options plus auth:
import { createOAuthGrantsController } from "better-supabase/blocks/sessions";
const grants = createOAuthGrantsController({
oauth: supabase.auth.oauth,
auth: bsb.auth,
});Connected agents
useOAuthGrants() (createOAuthGrantsController) lists every client the
user approved, newest first, with its scopes and grantedAt, and
revoke(clientId) disconnects one. On the server, or without the hook,
listGrants and revokeGrant do the same:
const { data: grants } = await supabase.auth.oauth.listGrants();
const { error } = await supabase.auth.oauth.revokeGrant({
clientId: grant.client.id,
});On the server, ctx.supabase from bsb.context() holds the caller's access
token but no Auth session, so its auth.oauth.listGrants and revokeGrant
send that token to Supabase Auth themselves instead of answering
AuthSessionMissingError.
Revoking ends the client's sessions and invalidates its refresh tokens. An
access token it already holds is verified locally and stays valid until it
expires, one hour by default, so say that in the UI. If that window matters,
add the session_active() policy from
Ended sessions: the revoked
client's session is gone, so the policy refuses its writes right away.
A denied request creates no grant, so the client never appears in the list.
Last updated on