# Social sign-in
Source: https://sovaria.in/docs/social-sign-in

Let users sign in with Google, GitHub, Microsoft, Apple, Discord and other providers, or any OpenID Connect identity provider: setup at each provider, the sign-in flow on web and mobile, account linking and errors.

Social sign-in lets people use an account they already have. Your app sends them to the provider; they approve; the provider sends them back through Sovaria, which creates or finds their user and gives your app a normal session. The first sign-in with a provider **creates the user**; later ones sign the same user in.

## Supported providers

| Provider | Protocol | Notes |
|---|---|---|
| Google, GitLab, LinkedIn, Slack, Twitch | OpenID Connect | The ID token is verified against the provider’s published keys. |
| Microsoft | OpenID Connect | Personal and work or school accounts. To allow only one Azure AD (Entra ID) tenant, use **Custom OIDC** with that tenant’s discovery URL. |
| Apple | OpenID Connect | The client ID is your **Services ID**; the secret is a JWT you sign with your Sign in with Apple key (valid for at most 6 months). Sovaria reads its expiry when you save it (a `.p8` key or an expired JWT is refused), shows a warning on the provider from 30 days before, and emails owners and admins (and posts to security alert channels) 30, 7 and 1 days before and on the day. Paste a new JWT before then, or Sign in with Apple stops. |
| GitHub, Bitbucket | OAuth 2.0 | The user’s primary **verified** email is read from the provider. |
| Discord, Facebook, Spotify, Notion, Zoom, Figma, Kakao | OAuth 2.0 | The profile is read from the provider’s user-info endpoint. |
| X (Twitter) | OAuth 2.0 | X doesn’t share email addresses with standard apps, so X users are never linked to an existing account by email. |
| Custom OIDC | OpenID Connect | Any OpenID Connect provider by its discovery URL: Okta, Auth0, Keycloak, an Entra ID tenant, Google Workspace. |
| Sign in with Sovaria | OAuth 2.0 | Another Sovaria project acts as the identity provider (see the last section). |

Each project uses its **own** OAuth app at the provider, so the consent screen shows your app’s name and logo; Sovaria never signs your users in with shared credentials. Client secrets are encrypted at rest and never shown again after you save them.

## How the flow works

1. **Your app starts sign-in** — It asks Sovaria for the provider’s sign-in URL with a PKCE challenge, then opens it.
2. **The user approves at the provider** — The provider redirects to Sovaria’s callback URL for your project with a code. Sovaria exchanges it, verifies the ID token or reads the profile, and finds or creates the user.
3. **Sovaria returns to your app** — It redirects to your `redirectTo` with `?code=…&type=sso`. That code works once, for 2 minutes, and only together with the app’s PKCE verifier.
4. **Your app exchanges the code** — The SDK swaps the code (and verifier) for a session. Two-factor authentication, session rules and the monthly active user count apply as for every other sign-in.

A sign-in must be finished within 10 minutes of starting it; after that the user sees “This sign-in link expired. Start again.”

## Connect a provider

1. **Copy the callback URL** — Open **Users › Sign in / Providers** and pick the provider. Copy the **callback URL** shown there; it ends in `/api/v1/auth/sso/<provider>/callback`.
2. **Create an OAuth app at the provider** — Paste the callback URL as the authorized redirect URI (exactly, with no trailing slash), then copy the **client ID** and **client secret** the provider gives you. Each provider’s steps are below.
3. **Save them in Sovaria** — Paste the client ID and secret, optionally limit sign-in to some email domains, and turn the provider on.
4. **Allow your app’s return address** — The page your app returns to (for example `https://app.example.com/auth/callback`, or `com.example.app://auth-callback` in a mobile app) must be your **Site URL**, under it, or listed under **Users › URL configuration**.

## Provider setup

### Google

1. In the Google Cloud console, open **APIs & Services › OAuth consent screen** and set up the consent screen (app name, support email, logo). Choose **External** unless only your Google Workspace may sign in.
2. Open **APIs & Services › Credentials › Create credentials › OAuth client ID**, application type **Web application**.
3. Under **Authorized redirect URIs**, add the callback URL from Sovaria. Create, then copy the client ID and secret into Sovaria.
4. Publish the consent screen when you go live; while it is in testing, only the test users you list can sign in.

### GitHub

1. On GitHub, open **Settings › Developer settings › OAuth Apps › New OAuth App** (or the same under your organization’s settings).
2. Set **Homepage URL** to your site and **Authorization callback URL** to the callback URL from Sovaria.
3. Register, then **Generate a new client secret**. Copy the client ID and secret into Sovaria.
4. Users need a verified primary email on GitHub; Sovaria reads it with the `user:email` scope.

### Microsoft

1. In the Azure portal, open **Microsoft Entra ID › App registrations › New registration**.
2. For **Supported account types**, choose work or school accounts and personal Microsoft accounts to allow everyone. Add the callback URL from Sovaria as a **Web** redirect URI.
3. Copy the **Application (client) ID**, then create a secret under **Certificates & secrets › New client secret** and copy its **value** (not its ID). Secrets expire; set a reminder to rotate it.
4. To allow only your company, use **Custom OIDC** with `https://login.microsoftonline.com/<tenant-id>/v2.0/.well-known/openid-configuration` instead.

### Apple

1. In **Certificates, Identifiers & Profiles**, make sure your App ID has **Sign in with Apple** enabled.
2. Create a **Services ID** (this is the client ID), enable Sign in with Apple on it, and add your domain and the callback URL from Sovaria as a return URL.
3. Create a **key** with Sign in with Apple enabled and download it (`.p8`). Note the key ID and your team ID.
4. Generate the client secret: a JWT signed with that key (ES256), with your team ID as issuer, the Services ID as subject and `https://appleid.apple.com` as audience. It can be valid for at most 6 months; generate a new one before it expires.
5. Apple shares the user’s name only on the first sign-in, and may give a private relay address (`…@privaterelay.appleid.com`) instead of the real one.

### Discord

1. In the Discord Developer Portal, create an application and open **OAuth2**.
2. Add the callback URL from Sovaria under **Redirects**, then copy the client ID and reset or copy the client secret.

### Okta, Auth0, Keycloak and other OIDC providers

Choose **Custom OIDC**, give it a name for the button, and paste the provider’s **discovery URL** (the one ending in `/.well-known/openid-configuration`), with a client ID and secret from an app you create there using the callback URL from Sovaria. Sovaria asks for the `openid`, `email` and `profile` scopes.

## Sign in from your app

Show a button for each enabled provider, start the flow, and finish it on the page or deep link your app returns to.

JavaScript:
```js
// Buttons for the providers that are turned on
const providers = await sovaria.auth.listProviders()

// Send the browser to the provider (a PKCE verifier is kept in sessionStorage)
await sovaria.auth.signInWithProvider('google', {
  redirectTo: 'https://app.example.com/auth/callback',
})

// On /auth/callback
const result = await sovaria.auth.handleEmailLink()   // exchanges ?code=…&type=sso
if (result?.type === 'sso' && result.session) router.replace('/dashboard')
if (result?.type === 'sso' && result.error) showError(result.description ?? result.error)
```

Dart:
```dart
final start = await sovaria.auth.getOAuthSignInUrl('google',
    redirectTo: 'com.example.app://auth-callback');
final result = await FlutterWebAuth2.authenticate(
    url: start.url.toString(), callbackUrlScheme: 'com.example.app');
await sovaria.auth.handleDeepLink(Uri.parse(result));   // signs in
```

Swift:
```swift
let start = try await sovaria.auth.getOAuthSignInURL("google",
                                                    redirectTo: "com.example.app://auth-callback")
let session = ASWebAuthenticationSession(url: URL(string: start.url)!,
                                         callbackURLScheme: "com.example.app") { url, _ in
  guard let url, let code = URLComponents(url: url, resolvingAgainstBaseURL: false)?
    .queryItems?.first(where: { $0.name == "code" })?.value else { return }
  Task { try await sovaria.auth.exchangeCodeForSession(code, codeVerifier: start.codeVerifier) }
}
session.presentationContextProvider = self
session.start()
```

Kotlin:
```kotlin
val start = sovaria.auth.getOAuthSignInUrl("google", redirectTo = "com.example.app://auth-callback")
pendingVerifier = start.codeVerifier
CustomTabsIntent.Builder().build().launchUrl(this, Uri.parse(start.url))

// In the activity that handles com.example.app://auth-callback
val code = intent.data?.getQueryParameter("code") ?: return
lifecycleScope.launch { sovaria.auth.exchangeCodeForSession(code, pendingVerifier) }
```

Mobile apps must return through a custom scheme or a registered app link, and PKCE is always required for those ([Sign-in for mobile apps](https://sovaria.in/docs/mobile-auth)).

## Accounts and linking

- The first sign-in with a provider creates a user with the provider’s name, email and avatar. Later sign-ins match the same provider account, even if the user changes their email there.
- If a user with the **same email** already exists in the project, the provider is **linked** to that user, but only when the provider confirms the email is verified. Otherwise sign-in is refused with `409`: “An account with this email already exists. Sign in with it first.” This stops someone from claiming an account with an unverified address.
- A provider that shares no email (some Twitch or Discord accounts) still creates a user, without a usable email address.
- One user can have several providers linked, plus a password or passkeys.

## Allow only your company’s domains

Each provider can be limited to some email domains, for example only `skolist.com` for an internal tool on Google Workspace. Fill in **Allowed email domains** when you connect the provider (for example `skolist.com, skolist.io`); people from other domains are refused.

## When it doesn’t work

| What you see | Why | Fix |
|---|---|---|
| `redirect_uri_mismatch` (or similar) on the provider’s page | The callback URL saved at the provider isn’t exactly the one Sovaria shows. | Copy it again from **Sign in / Providers**; check the scheme, path and trailing slash. |
| You land on your Site URL instead of the page you asked for | `redirectTo` isn’t allowed. | Add it under **Users › URL configuration**. |
| “This sign-in link expired. Start again.” | More than 10 minutes passed, or the code was already used. | Start again from your app. |
| `409` “An account with this email already exists” | The provider didn’t confirm the email. | Sign in the usual way first, or ask the user to verify the email at the provider. |
| `400 PKCE_REQUIRED` in a mobile app | An app redirect without a PKCE challenge. | Use the SDK’s `getOAuthSignInUrl`, or send `code_challenge` yourself. |

## Your project as an identity provider

Under **Users › OAuth apps** you can let other apps “Sign in with” your project. The other app receives an identity-only token and can read the user’s profile from `/oauth/userinfo`; it never gets access to your data.
