oAuth
Add oAuth provider
supastarter comes with two example providers out of the box: GitHub and Google. You can find a list of all available providers in the artic documentation (which is part of Lucia).
As an example, we will add Facebook as an oAuth provider.
First go to the Facebook developer console and create a new app.
Add the credentials to your .env.local
file:
Then create a new file packages/auth/oauth/facebook.ts
with the following content:
In this file we define two handlers:
-
facebookRouteHandler
: This handler is responsible for redirecting the user to the Facebook login page. -
facebookCallbackRouteHandler
: This handler is responsible for handling the callback from Facebook. It validates the authorization code and retrieves the user information. If the user already exists in the database, it creates a session for the user. If the user does not exist, it creates a new user and a session for the user.
You can more about how to get the authorization code, how to validate it and how to fetch the user data for each provider in the artic documentation.
Now we only need to create to route handlers in our SvelteKit app.
Create one file for the redirect handler apps/web/app/api/oauth/facebook/route.ts
:
And another file for the callback handler apps/web/app/api/oauth/facebook/callback/route.ts
:
The last thing you need to the oAuthProviders object in the apps/web/modules/saas/auth/components/SocialSigninButton.tsx
: