This project demonstrates a unified authentication system in SvelteKit, powered by Supabase Auth. It supports both individual users and organizations, enabling Single Sign-On (SSO) via Google Workspace. The login flow intelligently adapts based on the user's email domain—automatically routing organization users to SSO and allowing standard login for individuals. This approach provides seamless onboarding for SaaS and enterprise use cases, and highlights best practices for handling identity management and account duplication challenges when integrating SSO with Supabase.
Note: In this demo, I utilize a domain to determine if they are SSO. This could be adjusted to check if a user is part of a team or other conditional logic.
User Type | Login Options | Org Detection Logic |
---|---|---|
Individual user | Email or Google login | Email domain is not in registry |
Org member (no IdP) | Email or Google login | Email domain is in registry; usesSSO = false |
Org member (Google SSO) | Google login (prebuilt) | Email domain is in registry; usesSSO = true |
usesSSO
is true, the user is redirected to the SSO flow.⚠️ Google OAuth does not expose the email until after the user is created, which prevents domain-based pre-checks. This requires a separate post-auth hook if you want to enforce domain restrictions.
⚠️ User accounts and identities created via SSO differ from regular (email, phone, password, social login...) accounts in these ways:
No automatic linking. Each user account verified using a SSO identity provider will not be automatically linked to existing user accounts in the system. That is, if a user valid.email@supabase.io had signed up with a password, and then uses their company SSO login with your project, there will be two valid.email@supabase.io user accounts in the system.
Emails are not necessarily unique. Given the behavior with no automatic linking, email addresses are no longer a unique identifier for a user account. Always use the user's UUID to correctly reference user accounts.
+layout.server.ts
picks up the session.This will enable SSO for your project and is the starting point for setting up IdP-initiated SSO logins. This assumes you have properly followed the instructions for Setting up Server-Side Auth for SvelteKit
This will outline how to setup your google workspace as an IdP and add a custom app to integrate with your supabase project. Note: all routing will be handled in your project, primarily hooks.server.ts.
Field | Value |
---|---|
ACS URL | Supabase ACS URL |
Entity ID | Supabase EntityID |
Name ID format | |
Name ID | Basic Information > Primary email |
Google Workspace Attribute Mapping: Note: Google Workspace SAML SSO does not support passing profile image url as an attribute like you can obtain when using google Oauth. This would require a second request via another workspace service to obtain.
{
"keys": {
"email": {
"name": "http://schemas.xmlsoap.org/ws/2005/05/identity/claims/emailaddress"
},
"first_name": {
"name": "http://schemas.xmlsoap.org/ws/2005/05/identity/claims/givenname"
},
"last_name": {
"name": "http://schemas.xmlsoap.org/ws/2005/05/identity/claims/surname"
},
"phone": {
"name": "http://schemas.xmlsoap.org/ws/2005/05/identity/claims/telephoneNumber"
}
}
}
This will enable SSO for your project and is the starting point for setting up IdP-initiated SSO logins.
SAML Settings
Okta Field | Supabase Value |
---|---|
Single sign-on URL | Supabase ACS URL |
Audience URI (SP Entity ID) | Supabase EntityID |
Name ID format | EmailAddress |
Application username | |
Update application username on | Create and update |
Attribute Statements
Name | Name format (optional) | Value |
---|---|---|
Basic | user.email | |
first_name | Basic | user.firstName |
last_name | Basic | user.lastName |
phone | Basic | user.mobilePhone |
Okta Attribute Mapping: Note: You can map to many fields, anything that doesn't directly map will be contained as an object in the auth/users/raw_user_meta_data. This could be useful for building a profile which would ideally maintain the same id as the users uuid in the auth table.
{
"keys": {
"email": {
"name": "email"
},
"first_name": {
"name": "first_name"
},
"last_name": {
"name": "last_name"
},
"phone": {
"name": "phone"
}
}
}
$COMMAND supabase sso list --project-ref <project id, found in project/settings>
This list will only include basic information about each provider.
$COMMAND supabase sso show 123857fd-4f15-45bc-99aa-8f8315bc7214 \ --project-ref <project id, found in project/settings>
This command will pull the sso_config, and outline the Attribute Mapping and SAML Metadata, note you can only utilize Supabase CLI to config your project for SSO. This is helpful troubleshooting mapping issues.
$COMMAND supabase sso update <sso_provider (sso provider id, found in tables/schema:auth/sso_providers/id)> \ --project-ref <project id, found in project/settings> \ --attribute-mapping-file <file path>
this command will update the attribute mappings based on a .json mapping file you specify if you need to make changes.
MIT