Our Android SDK offers the easiest way to verify email/phone numbers in your android app. You can simply call a function and it does most of the heavy lifting and authentication for you.
Initialize Cotter with your API_KEY_ID . Call the function below in your MainActivity
For example:
Step 3: Register a URL Scheme
You also need to decide a callback URL scheme that you want to use. If your app's package name is com.example.myapplication then you should use something like this for your URL Scheme callback.
Make sure your URL scheme (the front part before ://) doesn't have an underscore or other special characters. To test it out, enter your Redirect URL here: https://jsfiddle.net/omd02jn5/
This URL scheme will be called when Cotter's verification is done and want to go back to your app. Read more about Android deep-linking.
You need to register this URL Scheme to receive the callback. Add the following in your AndroidManifest.xml . Check the example in the example's Github repo.
Step 4: Call Cotter's Login function
To open up Cotter's Login screen, you can either have your user input their email or phone number inside Cotter's screen, or you can provide a text input where your users can enter their email / phone, and you can pass that to Cotter's screen.
a) To login and enter the email or phone number in Cotter's window:
b) To login with the email or phone number that your user entered in your app:
To send code/link via SMS or WhatsApp, you'll need to add some balance to you project in the Dashboard.
You need to provide a class to redirect to when Cotter's email/phone number verification is complete.
Try it now! You can check if it works so far by invoking the login function above on a button click. It should load Cotter's Authentication page on a WebView.
Step 5: Receive the Token
In your CallbackActivity class, receive the token from the intent inside onCreate using this function.
For example, if your CallbackActivity class is called Dashboard:
The resp will consist of the following JSON Object as a string.
This JSON object contains 3 objects, identifier , oauth_token and user .
The identifier object contains information about the user's email or phone number, device type and name, and expiry.
The oauth_token contains an access_token that you can validate in your backend.
The user contains the User object in Cotter, which includes a "Cotter User ID". You should associate your user with this Cotter User ID for reference.
You should include this JSON Object into your call to your backend for Login or Registration. Your backend should then verify that the access token is valid.
Validating Cotter's Access Token
Checkout how to verify the OAuth Tokens from Cotter here:
Since you'll be using your API Key from a front-end website or mobile app, your API_KEY_ID is exposed to anyone inspecting your code. Here are some ways to prevent abuse:
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
...
Cotter.init(this.getApplicationContext(),
"https://www.cotter.app/api/v0",
<API_KEY_ID>);
}
...
com.example.myapplication://auth_callback
AndroidManifest.xml
<!-- ADD THE LINES FROM HERE -->
<activity android:name="com.cotter.app.RedirectUriReceiverActivity" 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"/>
<!-- This example is for: com.example.myapplication://auth_callback -->
<!-- Change it to your own url -->
<data android:scheme="com.example.myapplication"
android:host="auth_callback"/>
</intent-filter>
</activity>
<!-- TO HERE -->
// Provide Context and the URL Scheme
Cotter.newIdentity(this, "com.example.myapplication://auth_callback")
.login(
"EMAIL", // EMAIL or PHONE
this, // Context
Dashboard.class); // Callback Class
// Provide Context and the URL Scheme
Cotter.newIdentity(this, "com.example.myapplication://auth_callback")
.loginWithInput( // Context
"EMAIL", // EMAIL or PHONE
input.getText().toString(), // User's email
this, // Context
CallbackActivity.class); // Callback Class