Supabase & Astro
このコンテンツはまだ日本語訳がありません。
Supabase is an open source Firebase alternative. It provides a Postgres database, authentication, edge functions, realtime subscriptions, and storage.
Initializing Supabase in Astro
Section titled Initializing Supabase in AstroPrerequisites
Section titled Prerequisites- A Supabase project. If you don’t have one, you can sign up for free at supabase.com and create a new project.
- An Astro project with server-side rendering (SSR) enabled.
- Supabase credentials for your project. You can find these in the Settings > API tab of your Supabase project.
SUPABASE_URL
: The URL of your Supabase project.SUPABASE_ANON_KEY
: The anonymous key for your Supabase project.
Adding Supabase credentials
Section titled Adding Supabase credentialsTo add your Supabase credentials to your Astro project, add the following to your .env
file:
Now, these environment variables are available in your project.
If you would like to have IntelliSense for your environment variables, edit or create the env.d.ts
in your src/
directory and add the following:
Read more about environment variables and .env
files in Astro.
Your project should now include these files:
ディレクトリsrc/
- env.d.ts
- .env
- astro.config.mjs
- package.json
Installing dependencies
Section titled Installing dependenciesTo connect to Supabase, you will need to install @supabase/supabase-js
in your project.
Next, create a folder named lib
in your src/
directory. This is where you will add your Supabase client.
In supabase.ts
, add the following to initialize your Supabase client:
Now, your project should include these files:
ディレクトリsrc/
ディレクトリlib/
- supabase.ts
- env.d.ts
- .env
- astro.config.mjs
- package.json
Adding authentication with Supabase
Section titled Adding authentication with SupabaseSupabase provides authentication out of the box. It supports email/password authentication and OAuth authentication with many providers including GitHub, Google, and several others.
Prerequisites
Section titled Prerequisites- An Astro project initialized with Supabase.
- A Supabase project with email/password authentication enabled. You can enable this in the Authentication > Providers tab of your Supabase project.
Creating auth server endpoints
Section titled Creating auth server endpointsTo add authentication to your project, you will need to create a few server endpoints. These endpoints will be used to register, sign in, and sign out users.
POST /api/auth/register
: to register a new user.POST /api/auth/signin
: to sign in a user.GET /api/auth/signout
: to sign out a user.
Create these endpoints in the src/pages/api/auth
directory of your project. If you are using static
rendering mode, you must specify export const prerender = false
at the top of each file to render these endpoints on demand. Your project should now include these new files:
ディレクトリsrc/
ディレクトリlib/
- supabase.ts
ディレクトリpages/
ディレクトリapi/
ディレクトリauth/
- signin.ts
- signout.ts
- register.ts
- env.d.ts
- .env
- astro.config.mjs
- package.json
register.ts
creates a new user in Supabase. It accepts a POST
request with the an email and password. It then uses the Supabase SDK to create a new user.
signin.ts
signs in a user. It accepts a POST
request with the an email and password. It then uses the Supabase SDK to sign in the user.
signout.ts
signs out a user. It accepts a GET
request and removes the user’s access and refresh tokens.
Creating auth pages
Section titled Creating auth pagesNow that you have created your server endpoints, create the pages that will use them.
src/pages/register
: contains a form to register a new user.src/pages/signin
: contains a form to sign in a user.src/pages/dashboard
: contains a page that is only accessible to authenticated users.
Create these pages in the src/pages
directory. Your project should now include these new files:
ディレクトリsrc/
ディレクトリlib/
- supabase.ts
ディレクトリpages/
ディレクトリapi/
ディレクトリauth/
- signin.ts
- signout.ts
- register.ts
- register.astro
- signin.astro
- dashboard.astro
- env.d.ts
- .env
- astro.config.mjs
- package.json
register.astro
contains a form to register a new user. It accepts an email and password and sends a POST
request to /api/auth/register
.
signin.astro
contains a form to sign in a user. It accepts an email and password and sends a POST
request to /api/auth/signin
. It also checks for the presence of the access and refresh tokens. If they are present, it redirects to the dashboard.
dashboard.astro
contains a page that is only accessible to authenticated users. It checks for the presence of the access and refresh tokens. If they are not present or are invalid, it redirects to the sign in page.
Adding OAuth authentication
Section titled Adding OAuth authenticationTo add OAuth authentication to your project, you will need to edit your Supabase client to enable authentication flow with "pkce"
. You can read more about authentication flows in the Supabase documentation.
Next, in the Supabase dashboard, enable the OAuth provider you would like to use. You can find the list of supported providers in the Authentication > Providers tab of your Supabase project.
The following example uses GitHub as the OAuth provider. To connect your project to GitHub, follow the steps in the Supabase documentation.
Then, create a new server endpoint to handle the OAuth callback at src/pages/api/auth/callback.ts
. This endpoint will be used to exchange the OAuth code for an access and refresh token.
Next, edit the sign in page to include a new button to sign in with the OAuth provider. This button should send a POST
request to /api/auth/signin
with the provider
set to the name of the OAuth provider.
Finally, edit the sign in server endpoint to handle the OAuth provider. If the provider
is present, it will redirect to the OAuth provider. Otherwise, it will sign in the user with the email and password.
After creating the OAuth callback endpoint and editing the sign in page and server endpoint, your project should have the following file structure:
ディレクトリsrc/
ディレクトリlib/
- supabase.ts
ディレクトリpages/
ディレクトリapi/
ディレクトリauth/
- signin.ts
- signout.ts
- register.ts
- callback.ts
- register.astro
- signin.astro
- dashboard.astro
- env.d.ts
- .env
- astro.config.mjs
- package.json