Skip to content

Simplify library set up by declaring activities internally #620

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Apr 30, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions MIGRATION_GUIDE.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ The new version makes use of the latest Auth0.Android SDK, bringing improvements
- Open ID Connect compliant practices.
- ID token verification for Web Authentication flows.
- A new customizable networking stack.
- Simpler Android app set up.

Some of these features were previously available, but only enforced when the "OIDC" flag was explicitly enabled.

Expand Down Expand Up @@ -38,6 +39,15 @@ As part of removing legacy APIs or authentication flows no longer recommended fo

Continue reading for the detail of classes and methods that were impacted.

## Changes to the AndroidManifest file
In the previous version you had to declare the Lock activities you planned to use. These activities are now declared internally by the library with intent filters configured using the Manifest Placeholders that you provide for the Domain and Scheme. The Manifest Merger tool will process these and include them as part of your Android application.

If your `AndroidManifest.xml` file includes declarations for `LockActivity`, `PasswordlessLockActivity` or `CountryCodeActivity`, you should remove them to avoid duplicated intent filter declarations.

If you are using a custom style for the theme or need to override the intent-filter declarations in any of these activities, you will have to declare an activity with the same component name and annotate it with `tools:node="replace"`.

Find details about the merging rules that will be used in the [Android Manifest Merger article](https://developer.android.com/studio/build/manifest-merge).

### Removed classes
- `VoidCallback` is no longer available. Please, use `Callback<Void, AuthenticationException>` instead.

Expand Down
98 changes: 29 additions & 69 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,22 @@ Since June 2017 new Applications no longer have the **Password Grant Type** enab

## Requirements

Android API Level 21+ is required in order to use Lock's UI.
Android API Level 21+ & Java version 8 or above is required in order to use Lock's UI.

Here’s what you need in build.gradle to target Java 8 byte code for the Android and Kotlin plugins respectively.

```groovy
android {
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}

kotlinOptions {
jvmTarget = '1.8'
}
}
```

## Install

Expand All @@ -40,6 +55,14 @@ If you haven't done yet, go to [Auth0](https://auth0.com) and create an Account,
https://{YOUR_AUTH0_DOMAIN}/android/{YOUR_APP_PACKAGE_NAME}/callback
```

If you plan to use Passwordless authentication with SMS or Email connections, the Callback URL you will register looks like this:

```
https://{YOUR_AUTH0_DOMAIN}/android/{YOUR_APP_PACKAGE_NAME}/sms
// or
https://{YOUR_AUTH0_DOMAIN}/android/{YOUR_APP_PACKAGE_NAME}/email
```

The *package name* value required in the Callback URL can be found in your app's `build.gradle` file in the `applicationId` property. Both the *domain* and *client id* values can be found at the top of your Auth0 Application's settings. You're going to use them to setup the SDK. It's good practice to add them to the `strings.xml` file as string resources that you can reference later from the code. This guide will follow that practice.

```xml
Expand All @@ -49,7 +72,7 @@ The *package name* value required in the Callback URL can be found in your app's
</resources>
```

In your `app/build.gradle` file add the **Manifest Placeholders** for the Auth0 Domain and Auth0 Scheme properties, which are going to be used internally by the library to register an **intent-filter** that will capture the authentication result.
In your `app/build.gradle` file add the **Manifest Placeholders** for the Auth0 Domain and Auth0 Scheme properties, which are going to be used internally by the library to declare the Lock activities and register **intent-filters** that will capture the authentication result.

```groovy
apply plugin: 'com.android.application'
Expand Down Expand Up @@ -91,27 +114,7 @@ val account = Auth0("{YOUR_AUTH0_CLIENT_ID}", "{YOUR_AUTH0_DOMAIN}", "{THE_CONFI

### Email/Password, Enterprise & Social authentication

Modify the `AndroidManifest.xml` file, to include the Internet permission:

```xml
<uses-permission android:name="android.permission.INTERNET" />
```

Next, add the `LockActivity` inside the `application` tag:

```xml
<activity
android:name="com.auth0.android.lock.LockActivity"
android:label="@string/app_name"
android:launchMode="singleTask"
android:screenOrientation="portrait"
android:theme="@style/Lock.Theme"/>
```

Make sure the Activity's `launchMode` is declared as `singleTask` or the authentication result won't come back into your application.


Then, in any of your Activities, you need to initialize **Lock** and handle the release of its resources appropriately after you're done using it.
Initialize **Lock** and handle the release of its resources appropriately after you're done using it.

```kotlin
// This activity will show Lock
Expand Down Expand Up @@ -151,7 +154,7 @@ class MyActivity : AppCompatActivity() {
}
```

To start `LockActivity` from inside your `Activity`, create a new intent and launch it.
Start `LockActivity` from inside your `Activity`. For this, create a new intent from the Lock instance and launch it.

```kotlin
startActivity(lock.newIntent(this))
Expand All @@ -163,50 +166,7 @@ The Passwordless feature requires your Application to have the *Passwordless OTP

`PasswordlessLockActivity` authenticates users by sending them an Email or SMS (similar to how WhatsApp authenticates you). In order to be able to authenticate the user, your application must have the SMS/Email connection enabled and configured in your [dashboard](https://manage.auth0.com/#/connections/passwordless).


Modify the `AndroidManifest.xml` file, to include the Internet permission:

```xml
<uses-permission android:name="android.permission.INTERNET" />
```

Next, add the `PasswordlessLockActivity` inside the `application` tag. Note that this time, you must define an intent-filter that matches the passwordless callback URL:

```xml
<activity
android:name="com.auth0.android.lock.PasswordlessLockActivity"
android:label="@string/app_name"
android:launchMode="singleTask"
android:screenOrientation="portrait"
android:theme="@style/Lock.Theme">
<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:host="@string/com_auth0_domain"
android:pathPrefix="/android/${applicationId}/email"
android:scheme="https" />
</intent-filter>
</activity>
```

The `data` attribute of the intent-filter defines which format of "Callback URL" your app is going to capture. In the above case, it's going to capture calls from `email` passwordless connections. In case you're using the `sms` passwordless connection, the `pathPrefix` should end in `sms` instead.

Make sure the Activity's `launchMode` is declared as `singleTask` or the result won't come back in the authentication.

When the Passwordless connection is SMS you must also add the `CountryCodeActivity` to allow the user to change the **Country Code** prefix of the phone number.

```xml
<activity
android:name="com.auth0.android.lock.CountryCodeActivity"
android:theme="@style/Lock.Theme.ActionBar" />
```


Then, in any of your Activities, you need to initialize **PasswordlessLock** and handle the release of its resources appropriately after you're doing using it.
Initialize **PasswordlessLock** and handle the release of its resources appropriately after you're doing using it.

```kotlin
// This activity will show PasswordlessLock
Expand Down Expand Up @@ -246,7 +206,7 @@ class MyActivity : AppCompatActivity() {
}
```

To start `PasswordlessLockActivity` from inside your `Activity`, create a new intent and launch it.
Start `PasswordlessLockActivity` from inside your `Activity`. For this, create a new intent and launch it.

```kotlin
startActivity(lock.newIntent(this))
Expand Down
48 changes: 3 additions & 45 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,65 +2,23 @@
xmlns:tools="http://schemas.android.com/tools"
package="com.auth0.android.lock.app">

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />

<application
android:allowBackup="true"
android:allowBackup="false"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme"
tools:ignore="AllowBackup">
tools:ignore="GoogleAppIndexingWarning">

<activity
android:name=".DemoActivity"
android:label="@string/app_name"
android:screenOrientation="portrait">
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

<!--Auth0 Lock-->
<activity
android:name="com.auth0.android.lock.LockActivity"
android:label="Classic Lock"
android:launchMode="singleTask"
android:screenOrientation="portrait"
android:theme="@style/MyLock.Theme"/>
<!--Auth0 Lock End-->

<!--Auth0 PasswordlessLock-->
<activity
android:name="com.auth0.android.lock.PasswordlessLockActivity"
android:label="Passwordless Lock"
android:launchMode="singleTask"
android:screenOrientation="portrait"
android:theme="@style/MyLock.Theme">
<intent-filter>
<action android:name="android.intent.action.VIEW" />

<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />

<!--suppress AndroidDomInspection -->
<data
android:host="@string/com_auth0_domain"
android:pathPrefix="/android/${applicationId}/email"
android:scheme="https" />
</intent-filter>
</activity>
<!--Auth0 PasswordlessLock End-->

<!--Auth0 Lock Passwordless SMS Country Code Selection-->
<activity
android:name="com.auth0.android.lock.CountryCodeActivity"
android:theme="@style/Lock.Theme.ActionBar" />
<!--Auth0 Lock Passwordless SMS Country Code Selection-->

</application>

</manifest>
12 changes: 0 additions & 12 deletions app/src/main/res/values/styles.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,4 @@
<item name="colorAccent">@color/colorAccent</item>
</style>

<style name="MyLock.Theme" parent="Lock.Theme" />

<!-- Change to this Theme if you want to customize attrs -->
<style name="MyLock.Theme.Customized">
<item name="Auth0.HeaderTitle">@string/com_auth0_domain</item>
<item name="Auth0.HeaderTitleColor">@android:color/holo_orange_light</item>
<item name="Auth0.HeaderLogo">@mipmap/ic_launcher</item>
<item name="Auth0.HeaderBackground">@android:color/holo_red_light</item>
<item name="Auth0.DarkPrimaryColor">@android:color/holo_blue_dark</item>
<item name="Auth0.PrimaryColor">@android:color/holo_blue_light</item>
</style>

</resources>
7 changes: 7 additions & 0 deletions lib/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,13 @@ android {
}
}

unitTestVariants.all {
it.mergedFlavor.manifestPlaceholders += [
auth0Domain: "johndoe.auth0.com",
auth0Scheme: "test"
]
}

compileOptions {
sourceCompatibility 1.8
targetCompatibility 1.8
Expand Down
41 changes: 41 additions & 0 deletions lib/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
<uses-permission android:name="android.permission.INTERNET" />

<application>
<!--Auth0 WebAuthProvider-->
<activity
android:name="com.auth0.android.provider.RedirectActivity"
tools:node="replace">
Expand All @@ -20,6 +21,46 @@
android:scheme="${auth0Scheme}" />
</intent-filter>
</activity>
<!--Auth0 WebAuthProvider End-->

<!--Auth0 Lock-->
<activity
android:name="com.auth0.android.lock.LockActivity"
android:launchMode="singleTask"
android:screenOrientation="portrait"
android:theme="@style/Lock.Theme"/>
<!--Auth0 Lock End-->

<!--Auth0 PasswordlessLock-->
<activity
android:name="com.auth0.android.lock.PasswordlessLockActivity"
android:launchMode="singleTask"
android:screenOrientation="portrait"
android:theme="@style/Lock.Theme">
<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:host="${auth0Domain}"
android:pathPrefix="/android/${applicationId}/email"
android:scheme="${auth0Scheme}" />

<data
android:host="${auth0Domain}"
android:pathPrefix="/android/${applicationId}/sms"
android:scheme="${auth0Scheme}" />
</intent-filter>
</activity>
<!--Auth0 PasswordlessLock End-->

<!--Auth0 Lock Passwordless SMS Country Code Selection-->
<activity
android:name="com.auth0.android.lock.CountryCodeActivity"
android:theme="@style/Lock.Theme.ActionBar" />
<!--Auth0 Lock Passwordless SMS Country Code Selection-->
</application>

</manifest>
25 changes: 0 additions & 25 deletions lib/src/test/AndroidManifest.xml

This file was deleted.