This is a minimal starter kit to help integrate Astro with Supabase's new SSR package for Auth. I used Azure as the OAuth provider.
Pure Astro with no front-end framework. Also includes minimal Supabase query example.
The Astro docs use the older Supabase client library. I found integrating them a little confusing because of the conflicting guides in the docs, so I hope this helps clear things up for you if you're having trouble.
/
├── src/
│ ├── components/
│ │ └── MSSignIn.astro // Microsoft's required sign in button
│ ├── layouts/
│ │ └── Layout.astro
│ ├── middleware/
│ │ └── index.ts
│ └── pages/
│ └── index.astro
│ └── nextpage.astro // Callback redirects here if auth successful
│ └── api/auth // Auth routes
│ └── callback.ts // Route to redirect to after auth
│ └── signin.ts
│ └── signout.ts
└── package.json
└── astro.config.mjs // output must be 'server'
└── .env
-
Make your own .env file containing your Supabase anon API key and Supabase URL from Dashboard>Settings>API:
- The
PUBLIC
prefix is necessary for Astro and React components. Other frameworks may have different prefix requirements.
- The
PUBLIC_SUPABASE_URL=https://<your-supabase-url>.supabase.co
PUBLIC_SUPABASE_ANON_KEY=<apikey>
PUBLIC_VERCEL_URL=http://localhost:4321
- Create a free Supabase account and start a new project
- Go to the Database tab in the sidebar and create a table named
test
.- Add a column
message
with typetext
and click Save.
- Add a column
- In the Authentication tab, click on Policies and add a new policy.
- Use the 'Enable read access to everyone template'
- Change the name to something like 'Enable access for authenticated users'
- Change the Allowed operation to
ALL
, and the Target roles toauthenticated
- Leave the
USING
expression astrue
, and save the policy.
- Still in Authentication, click on Providers and enable your provider of choice.
- Follow this guide in the Supabase docs to set up Azure or another OAuth provider. Note that you will need the Callback URL from here to setup the provider
- For Azure setup the Platform config as a Web app rather than an SPA, otherwise the PKCE flow won't work.
- Click on URL Configuration
- Set the Site URL to
http://localhost:4321/
if you are using the default Astro dev port. Note the trailing forward slash, apparently it's important. I didn't test that. - If you are deploying to something like Vercel, set the Site URL as
https://<your-vercel-project>.app/
and addhttp://localhost:4321/**
as a Redirect URL so you can continue working with your dev environment.
- Set the Site URL to
- When you create your tables and turn on RLS, make sure you create access policies under Authentication > Policies
- If you are setting target roles to
authenticated
, you must set a SELECT policy for the INSERT policy to work. - If you are setting target roles to
authenticated
and still getting RLS violation errors, make sure if you are querying using the Supabase client you create withcreateServerClient
, in your API, say, that you pass thecontext
object to it so it has the user's session.
- Create new Vercel project and attach your repo from Github.
- Use the deployment URL as your Site URL above.
- You can also set additional Redirect URLs with wildcards to cover deployment-specific URLs
- Under Settings, click on Environment Variables and copy and paste the content of your .env file into the Key Value inputs under Create new. You can paste the whole thing at once and Vercel will parse it for you, or upload the .env file you made.
- Change the value of
PUBLIC_VERCEL_URL
tohttps://<your-project-name>.vercel.app
- Even though Vercel exposes
VERCEL_URL
as a system environment variable, it does not seem to prefix it withPUBLIC
for use by Astro.
- Change the value of
- Redeploy after you change the environment variables.
- If you've missed anything above, the provider redirect will not work and you'll either get redirected to your localhost or the index page. I think Google OAuth requires you to add your deployment URL (the
PUBLIC_VERCEL_URL
) to Authorized redirect URIs in your Google Cloud Console.
- When I installed @types/micromatch as a dependency instead of a dev dependency and forgot to install micromatch itself, typing
import micromatch from "micromatch"
appeared to work but it failed silently and middleware never got triggered. I tried to minimally reproduce this behaviour, but was unable to - the middleware either worked or Astro threw a module not found build error.