OAuth Providers
Svelte Guardian supports multiple OAuth providers to allow users to sign in with their existing accounts on other platforms. This guide will walk you through integrating various OAuth providers with your SvelteKit application.
Supported OAuth Providers
Svelte Guardian supports the following OAuth providers:
- GitHub
- Microsoft
- Discord
- And more (any provider supported by Auth.js)
General Configuration
OAuth providers are configured in the providers
section of your Svelte Guardian configuration:
Provider Setup Guide
- Go to the Google Cloud Console
- Create a new project or select an existing one
- Navigate to “APIs & Services” > “Credentials”
- Click “Create Credentials” > “OAuth client ID”
- Select “Web application” as the application type
- Add your domain to “Authorized JavaScript origins” (e.g.,
https://yourdomain.com
) - Add your redirect URI to “Authorized redirect URIs” (e.g.,
https://yourdomain.com/auth/callback/google
) - Click “Create” and note your Client ID and Client Secret
GitHub
- Go to GitHub Developer Settings
- Click “New OAuth App”
- Fill in your application details
- Set the Authorization callback URL to your redirect URI (e.g.,
https://yourdomain.com/auth/callback/github
) - Click “Register application” and note your Client ID
- Generate a new client secret and note it
Microsoft
- Go to the Microsoft Azure Portal
- Navigate to “Azure Active Directory” > “App registrations”
- Click “New registration”
- Enter a name for your application
- Set the redirect URI to your callback URL (e.g.,
https://yourdomain.com/auth/callback/microsoft
) - Click “Register”
- Note your Application (client) ID
- Under “Certificates & secrets”, create a new client secret and note it
- Go to Facebook for Developers
- Create a new app or select an existing one
- Navigate to “Settings” > “Basic”
- Note your App ID and App Secret
- Under “Facebook Login” > “Settings”, add your redirect URI (e.g.,
https://yourdomain.com/auth/callback/facebook
)
Client-Side Implementation
Sign-in Button
Add OAuth sign-in buttons to your sign-in page:
Custom Styling
Style your OAuth buttons to match the provider’s branding:
Account Linking
Svelte Guardian supports linking multiple OAuth providers to a single user account.
Linking Accounts
To enable account linking, add a method to your profile or settings page:
Connected Accounts
Customizing User Profiles
When users sign in with OAuth providers, their profile information (name, email, avatar) is automatically fetched. You can customize how this information is handled:
Handling OAuth Callbacks
Svelte Guardian automatically handles OAuth callbacks, but you can customize the behavior:
Environment Variables
Store your OAuth credentials as environment variables:
Testing OAuth Providers
During development, you may want to test OAuth providers locally:
- Use
http://localhost:5173
(or your dev server URL) as an allowed origin in provider settings - Add
http://localhost:5173/auth/callback/[provider]
as a redirect URI - Use development credentials separate from production
Troubleshooting
Common Issues:
Redirect URI Mismatch: Ensure the redirect URI in your provider settings exactly matches your application’s callback URL.
Scope Issues: Some providers require specific scopes to access user information. Check the provider’s documentation.
CORS Errors: During development, you might encounter CORS issues. Make sure your development URL is allowed in the provider settings.
Session Not Persisting: Ensure your application is configured to handle cookies properly, especially in development.
Security Considerations
Store Credentials Securely: Never commit OAuth credentials to your repository. Use environment variables.
Use HTTPS: Always use HTTPS in production to secure OAuth redirects and tokens.
Validate Email Domains: If your application is for a specific organization, consider validating email domains before allowing sign-in.
Review Scopes: Only request the OAuth scopes you actually need to minimize security risks and privacy concerns.