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.
pod 'MagicSDK'
pod 'MagicExt-OAuth'
- run
pod install
- 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
}
- 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
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;
}
}
#Example
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
: Please check all supported providers in the social-login sectionredirectURI
: "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