How to Add OAuth2 Social Login in iOS Apps with Magic

How to Add OAuth2 Social Login in iOS Apps with Magic


Social Logins work as an extension to Magic SDK. To add Social Login to your Magic integration, follow these steps to install the OAuth Extension:

  1. In your Podfile, add following lines.
pod 'MagicSDK' pod 'MagicExt-OAuth'
  1. run pod install

  2. Create your Magic SDK instance in your AppDelegate.swift

import UIKit import MagicSDK @UIApplicationMain class AppDelegate: UIResponder, UIApplicationDelegate { func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { // assign the newly created Magic instance to shared property Magic.shared = Magic(apiKey: "API_KEY") // do any other necessary launch configuration } return true }
  1. Start the OAuth 2.0 login flow:
Public Methods
loginWithPopup( _ configuration: OAuthConfiguration, response: (_ resp: Response<OAuthRepsonse>) -> Void )
loginWithPopup(_ configuration: OAuthConfiguration) -> Promise <OAuthRepsonse>

userInfo contains the OpenID Connect profile information about the user. The schema of this object should match the OpenID spec, except that fields are camelCased instead of snake_cased. The presence of some fields may differ depending on the specific OAuth provider and the user's own privacy settings. See:

public struct OAuthResponse: MagicResponse { public let oauth: OauthPartialResult public let magic: MagicPartialResult } public struct OauthPartialResult: Codable { public let provider: String; public let scope: [String]; public let accessToken: String; public let userHandle: String; public let userInfo: OpenIDConnectProfile; } public struct MagicPartialResult: Codable { public let idToken: String; public let userMetadata: UserMetadata; } public struct OpenIDConnectProfile: Codable { public let name: String? public let familyName: String? public let givenName: String? public let middleName: String? public let nickname: String? public let preferredUsername: String? public let profile: String? public let picture: String? public let website: String? public let gender: String? public let birthdate: String? public let zoneinfo: String? public let locale: String? public let updatedAt: Int? // OpenIDConnectEmail public let email: String? public let emailVerified: Bool? // OpenIDConnectPhone public let phoneNumber: String? public let phoneNumberVerified: Bool? // OpenIDConnectAddress public let address: OIDAddress? // OIDAddress public struct OIDAddress: Codable { let formatted: String; let streetAddress: String; let locality: String; let region: String; let postalCode: String; let country: String; } }
magic.oauth.loginWithPopup(config, response: {res in guard let result = response.result else { return print("Error:", response.error.debugDescription) } print("DIDToken", result.magic.idToken) }) }
Configuration Class

OAuthConfiguration(provider: OAuthProvider, redirectURI: String, scope: [String]? = nil, loginHint: String? = nil)

  • provider: .GOOGLE, .GITHUB, .FACEBOOK, or .APPLE
  • redirectURI: "your_app_scheme://"
  • scope: This field specifies a space-delimited list of access scopes that correspond to the resources that your application could access on the user's behalf
How to Add OAuth2 Social Login in iOS Apps with Magic