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
Supabase provides authentication out of the box. It supports email/password authentication and OAuth authentication with many providers including GitHub, Google, and several others.
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
To 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. 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
Now 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, it redirects to the sign in page.
Adding OAuth authentication
To 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: