How to Add OAuth2 Social Login in Android Apps with Magic

How to Add OAuth2 Social Login in Android Apps with Magic

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:

  1. In the build.gradle, add following lines.
dependencies { implementation 'link.magic:magic-android:1.0.0' implementation 'link.magic:magic-ext-oauth:0.1.1' }
  1. Build your project

  2. 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>
  1. 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) }
  1. 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
How to Add OAuth2 Social Login in Android Apps with Magic