Overview

Social authentication allows users to sign in to your app using their existing social media accounts. The Dynamic Swift SDK supports multiple providers including Apple, Google, Twitter, Discord, GitHub, Twitch, Facebook, and Farcaster.

Prerequisites

1. Dynamic Dashboard Configuration

Before implementing social authentication, you must enable and configure the desired providers in your Dynamic dashboard:
  1. Log in to Dynamic Dashboard
  2. Navigate to Log in and User ProfileSocial
  3. Enable the providers you want to support
  4. Configure OAuth settings for each provider
Register your app’s redirect URL in the Dynamic dashboard:
  1. Go to Dynamic Dashboard
  2. Navigate to SecurityWhitelist Mobile Deeplink
  3. Add your app’s deep link URL (e.g., myapp://auth/callback)
  4. Save the configuration

3. Info.plist Configuration

Add the following to your Info.plist to ensure Safari authentication works properly:
<key>CFBundleURLTypes</key>
<array>
    <dict>
        <key>CFBundleTypeRole</key>
        <string>Editor</string>
        <key>CFBundleURLName</key>
        <string>com.yourcompany.yourapp</string>
        <key>CFBundleURLSchemes</key>
        <array>
            <string>yourapp</string>
        </array>
    </dict>
</array>

Implementation

Get Available Providers

First, fetch the list of enabled providers for your environment:
import DynamicSwiftSDK

let dynamicClient: DynamicClient

// Fetch enabled providers
let providers: [ProviderInfo] = try await getEnabledSocialProviders(client: dynamicClient)

// Check available providers
for provider in providers {
    print("Available: \(provider.provider)")
}

Implement Social Login

import DynamicSwiftSDK

let dynamicClient: DynamicClient
let provider: ProviderType = .google // or any supported provider
let deepLinkUrl = "myapp://auth/callback" // Your app's deep link URL (must be whitelisted in Dynamic dashboard)

do {
    let authenticatedUser: SdkUser = try await socialLogin(
        client: dynamicClient,
        with: provider,
        deepLinkUrl: deepLinkUrl
    )

    print("Welcome \(authenticatedUser.email ?? "")")
    print("User ID: \(authenticatedUser.id)")

    // User is now authenticated
    // Navigate to your app's main screen
} catch {
    print("Social login failed: \(error)")
    // Handle error appropriately
}

Provider Types

public enum ProviderType: String {
    case apple = "apple"
    case google = "google"
    case twitter = "twitter"
    case discord = "discord"
    case github = "github"
    case twitch = "twitch"
    case facebook = "facebook"
    case farcaster = "farcaster"
}

Complete Social Authentication Example

Here’s a complete example showing how to implement social authentication in a SwiftUI view:
import DynamicSwiftSDK
import SwiftUI

struct SocialLoginView: View {
    let client: DynamicClient
    @Binding var message: String

    var body: some View {
        VStack(spacing: 12) {
            // Google button (prominent)
            Button {
                Task {
                    await handleSocialAuth(provider: .google)
                }
            } label: {
                HStack {
                    Image("google-logo")
                        .resizable()
                        .frame(width: 20, height: 20)
                    Text("Continue with Google")
                        .fontWeight(.medium)
                }
                .frame(maxWidth: .infinity)
                .padding()
                .background(Color.white)
                .foregroundColor(.primary)
                .cornerRadius(8)
                .overlay(
                    RoundedRectangle(cornerRadius: 8).stroke(
                        Color.secondary.opacity(0.3), lineWidth: 1))
            }

            // Other social providers in a grid
            LazyVGrid(
                columns: Array(repeating: GridItem(.flexible(), spacing: 12), count: 3),
                spacing: 12
            ) {
                ForEach([
                    ProviderType.github, ProviderType.twitter, ProviderType.apple,
                    ProviderType.discord, ProviderType.facebook, ProviderType.farcaster,
                ], id: \.self) { provider in
                    Button {
                        Task {
                            await handleSocialAuth(provider: provider)
                        }
                    } label: {
                        Image(providerImageName(provider))
                            .resizable()
                            .frame(width: 24, height: 24)
                            .frame(width: 48, height: 48)
                            .background(Color.gray.opacity(0.1))
                            .cornerRadius(8)
                    }
                }
            }
        }
    }

    private func providerImageName(_ provider: ProviderType) -> String {
        switch provider {
        case .google: return "google-logo"
        case .github: return "github-logo"
        case .twitter: return "x-logo"
        case .apple: return "apple-logo"
        case .discord: return "discord-logo"
        case .facebook: return "facebook-logo"
        case .farcaster: return "farcaster-logo"
        default: return "questionmark.circle"
        }
    }

    private func handleSocialAuth(provider: ProviderType) async {
        message = "Preparing authentication..."

        do {
            let user = try await socialLogin(
                client: client,
                with: provider,
                deepLinkUrl: "myapp://"
            )
            message = "✅ Successfully logged in: \(user.email ?? "Unknown")"
        } catch {
            message = "❌ Social auth failed: \(error.localizedDescription)"
        }
    }
}

Best Practices

  • Use a consistent deep link URL format
  • Ensure the URL is whitelisted in your Dynamic dashboard
  • Test deep link handling in your app

2. User Experience

  • Show loading states during authentication
  • Provide clear error messages
  • Handle user cancellation gracefully

3. Security

  • Never store sensitive authentication data
  • Use secure network connections
  • Implement proper session management

Troubleshooting

Callback Not Working

  • Verify your app’s URL scheme is configured correctly in Dynamic dashboard
  • Check that redirect URLs match in provider and Dynamic settings
  • Ensure your Info.plist has the correct URL scheme configuration

Provider Not Showing

  • Confirm provider is enabled in Dynamic dashboard
  • Check that getEnabledSocialProviders is called successfully
  • Verify provider configuration in the respective developer console

Authentication Fails

  • Check network connectivity
  • Verify provider credentials in Dynamic dashboard
  • Ensure deep link URL is correctly configured