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 the following lines.
01dependencies {
02 implementation 'link.magic:magic-android:4.0.0'
03 implementation 'org.web3j:core:4.8.8-android'
04 implementation 'link.magic:magic-ext-oauth:3.0.0'
05}
- 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
01<!-- OAuth Activity -->
02 <activity android:name="link.magic.android.extension.oauth.customTab.CustomTabMainActivity"
03 android:configChanges=
04 "keyboard|keyboardHidden|screenLayout|screenSize|orientation"
05 android:label="@string/app_name" />
06 <activity
07 android:name="link.magic.android.extension.oauth.customTab.CustomTabActivity"
08 android:exported="true">
09 <intent-filter>
10 <action android:name="android.intent.action.VIEW" />
11 <category android:name="android.intent.category.DEFAULT" />
12 <category android:name="android.intent.category.BROWSABLE" />
13 <data android:scheme="link.magic.demo" android:host="callback"/>
14 </intent-filter>
15 </activity>
- Invoke OAuth 2.0 flow, the redirection link shall match the one you registered in the
AndroidManifest.xml
01fun loginWithOAuth(view: View) {
02 val configuration = OAuthConfiguration(OAuthProvider.GITHUB, "link.magic.demo://callback")
03 magic.oauth.loginWithPopup(this, configuration)
04 }
- In
onActivityResult
method, pass the data intent tomagic.oauth.getResult
method to get the authentication result .
01override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
02 val result = magic.oauth.getResult(data)
03
04 result.whenComplete { response: OAuthResponse?, error: Throwable? ->
05 if (error != null) {
06 Log.d("error", error.localizedMessage)
07 }
08 if (response != null && !response.hasError()) {
09 response.result.magic.idToken?.let { Log.d("login", it) }
10 startLoggedInActivity()
11 } else {
12 Log.d("login", "Not Logged in")
13 }
14 }
15 super.onActivityResult(requestCode, resultCode, data)
16 }
#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
01class OAuthResponseClass {
02 lateinit var magic: MagicPartialResult
03 lateinit var oauth: OAuthPartialResult
04}
05
06class OAuthPartialResult {
07 var provider: String? = null
08 var scope: List<String>? = null
09 var accessToken: String? = null
10 var userHandle: String? = null
11 lateinit var userInfo: OpenIDConnectProfile
12}
13
14class MagicPartialResult {
15 var idToken: String? = null
16 lateinit var userMetadata: UserMetadataResponse;
17}
18
19class OpenIDConnectProfile {
20 var name: String? = null
21 var familyName: String? = null
22 var givenName: String? = null
23 var middleName: String? = null
24 var nickname: String? = null
25 var preferredUsername: String? = null
26 var profile: String? = null
27 var picture: String? = null
28 var website: String? = null
29 var gender: String? = null
30 var birthdate: String? = null
31 var zoneinfo: String? = null
32 var locale: String? = null
33 var updatedAt: Int? = null
34
35 // OpenIDConnectEmail
36 var email: String? = null
37 var emailVerified: Boolean? = null
38
39 // OpenIDConnectPhone
40 var phoneNumber: String? = null
41 var phoneNumberVerified: Boolean? = null
42
43 // OpenIDConnectAddress
44 var address: OIDAddress? = null
45
46 // OIDAddress
47 class OIDAddress {
48 lateinit var formatted: String;
49 lateinit var streetAddress: String;
50 lateinit var locality: String;
51 lateinit var region: String;
52 lateinit var postalCode: String;
53 lateinit var country: String;
54 }
55}
#Configuration Class
OAuthConfiguration(provider: OAuthProvider, redirectURI: String, scope: List<String>? , loginHint: String?)
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