Magic OAuth for Android

Magic OAuth for Android

#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 the build.gradle, add following lines.
dependencies {
    implementation 'link.magic:magic-android:2.1.0'
    implementation 'org.web3j:core:4.8.8-android' 
    implementation 'link.magic:magic-ext-oauth:1.0.0'
}
  • Build your project
  • Add the following activities and intent filter for Chrome Custom Tabs inside your application element. You may configure the scheme and the host for the oauth redirection
<!-- OAuth Activity -->
        <activity android:name="link.magic.android.extension.oauth.customTab.CustomTabMainActivity"
            android:configChanges=
                "keyboard|keyboardHidden|screenLayout|screenSize|orientation"
            android:label="@string/app_name" />
        <activity
            android:name="link.magic.android.extension.oauth.customTab.CustomTabActivity"
            android:exported="true">
            <intent-filter>
                <action android:name="android.intent.action.VIEW" />
                <category android:name="android.intent.category.DEFAULT" />
                <category android:name="android.intent.category.BROWSABLE" />
                <data android:scheme="link.magic.demo" android:host="callback"/>
            </intent-filter>
        </activity>
  • Invoke OAuth 2.0 flow, the redirection link shall match the one you registered in the AndroidManifest.xml
fun loginWithOAuth(view: View) {
        val configuration = OAuthConfiguration(OAuthProvider.GITHUB, "link.magic.demo://callback")
        magic.oauth.loginWithPopup(configuration)
    }
  • In onActivityResult method, pass the data intent to magic.oauth.getResult method to get the authentication result .
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        val result = magic.oauth.getResult(data)

        result.whenComplete { response: OAuthResponse?, error: Throwable? ->
            if (error != null) {
                Log.d("error", error.localizedMessage)
            }
            if (response != null && !response.hasError()) {
                response.result.magic.idToken?.let { Log.d("login", it) }
                startLoggedInActivity()
            } else {
                Log.d("login", "Not Logged in")
            }
        }
        super.onActivityResult(requestCode, resultCode, data)
    }

#Public Methods

Methods
fun loginWithPopup(configuration: OAuthConfiguration): Boolean
fun getResult(data: Intent?): CompletableFuture

#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

class OAuthResponseClass {
    lateinit var magic: MagicPartialResult
    lateinit var oauth: OAuthPartialResult
}

class OAuthPartialResult {
    var provider: String? = null
    var scope: List<String>? = null
    var accessToken: String? = null
    var userHandle: String? = null
    lateinit var userInfo: OpenIDConnectProfile
}

class MagicPartialResult {
    var idToken: String? = null
    lateinit var userMetadata: UserMetadataResponse;
}

class OpenIDConnectProfile {
    var name: String? = null
    var familyName: String? = null
    var givenName: String? = null
    var middleName: String? = null
    var nickname: String? = null
    var preferredUsername: String? = null
    var profile: String? = null
    var picture: String? = null
    var website: String? = null
    var gender: String? = null
    var birthdate: String? = null
    var zoneinfo: String? = null
    var locale: String? = null
    var updatedAt: Int? = null

    // OpenIDConnectEmail
    var email: String? = null
    var emailVerified: Boolean? = null

    // OpenIDConnectPhone
    var phoneNumber: String? = null
    var phoneNumberVerified: Boolean? = null

    // OpenIDConnectAddress
    var address: OIDAddress? = null

    // OIDAddress
    class OIDAddress {
        lateinit var formatted: String;
        lateinit var streetAddress: String;
        lateinit var locality: String;
        lateinit var region: String;
        lateinit var postalCode: String;
        lateinit var country: String;
    }
}

#Configuration Class

OAuthConfiguration(provider: OAuthProvider, redirectURI: String, scope: List<String>? , loginHint: String?)

  • 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?

Magic OAuth for Android

Did you find what you were looking for?