Magic OAuth for iOS

Magic OAuth for iOS

#Swift Package Manager Installation

1. In Xcode, select File > Add Packages... and enter `github.com/magiclabs/magic-ios-ext.git` as the repository URL.

2. Select `Up to Next Major Version 1.0.0`the

3. Don't forget to add the libraries to your Package Dependencies

4. When it's been added to your dependencies, you're all set!

#Cocoapods Installation

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:

  • In your Podfile, add following lines.
Swift
01pod 'MagicSDK'
02  pod 'MagicExt-OAuth'
  • run pod install
  • Create your Magic SDK instance in your AppDelegate.swift
Swift
01import UIKit
02import MagicSDK
03
04@UIApplicationMain
05class AppDelegate: UIResponder, UIApplicationDelegate {
06
07    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
08
09        // assign the newly created Magic instance to shared property
10        Magic.shared = Magic(apiKey: "API_KEY")
11
12        // do any other necessary launch configuration
13    }
14    return true
15}
  • Start the OAuth 2.0 login flow:

#Public Methods

Methods
loginWithPopup( _ configuration: OAuthConfiguration, response: (_ resp: Response<OAuthRepsonse>) -> Void )
loginWithPopup(_ configuration: OAuthConfiguration) -> Promise <OAuthRepsonse>

#Returns

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: https://openid.net/specs/openid-connect-basic-1_0.html#StandardClaims

Swift
01public struct OAuthResponse: MagicResponse {
02      public let oauth: OauthPartialResult
03      public let magic: MagicPartialResult
04}
05
06public struct OauthPartialResult: Codable {
07      public let provider: String;
08      public let scope: [String];
09      public let accessToken: String;
10      public let userHandle: String;
11      public let userInfo: OpenIDConnectProfile;
12}
13
14public struct MagicPartialResult: Codable {
15      public let idToken: String;
16      public let userMetadata: UserMetadata;
17}
18
19public struct OpenIDConnectProfile: Codable {
20    public let name: String?
21    public let familyName: String?
22    public let givenName: String?
23    public let middleName: String?
24    public let nickname: String?
25    public let preferredUsername: String?
26    public let profile: String?
27    public let picture: String?
28    public let website: String?
29    public let gender: String?
30    public let birthdate: String?
31    public let zoneinfo: String?
32    public let locale: String?
33    public let updatedAt: Int?
34
35    // OpenIDConnectEmail
36    public let email: String?
37    public let emailVerified: Bool?
38
39    // OpenIDConnectPhone
40    public let phoneNumber: String?
41    public let phoneNumberVerified: Bool?
42
43    // OpenIDConnectAddress
44    public let address: OIDAddress?
45
46    // OIDAddress
47    public struct OIDAddress: Codable {
48        let formatted: String;
49        let streetAddress: String;
50        let locality: String;
51        let region: String;
52        let postalCode: String;
53        let country: String;
54    }
55}

#Example

Swift
01magic.oauth.loginWithPopup(config, response: {res in
02    guard let result = response.result else { return print("Error:", response.error.debugDescription) }
03    print("DIDToken", result.magic.idToken)
04    })
05}

#Configuration Class

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

  • provider: Please check all supported providers in the social-login section
  • 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

Server-side SDKs?

Integrate Magic with your existing backend.

Did you find what you were looking for?

Did you find what you were looking for?