SDK

Authentication

This is an add-on feature of the Notificare library. You will need to subscribe to the Users & Authentication add-on and configure this service as described here. With this feature your application can provide creation, authentication and management of user accounts.

In this page we will dive deeper into this functionality. This functionality is built based on the OAuth2 standard, providing your application with the means to create accounts, authenticate users, send email activations, recover and change passwords and refresh access tokens when needed.

In order to be able to handle password reset and account validation links, create one or more activities with the intent-filters as illustrated below:

<application>
    <activity
            android:name=".ResetPasswordActivity"
            android:label="@string/reset_password_title">
            <intent-filter android:autoVerify="true">
                <action android:name="android.intent.action.VIEW" />

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

                <data
                    android:host="${notificareApplicationId}.applinks.notifica.re"
                    android:pathPrefix="/oauth/resetpassword"
                    android:scheme="https" />
            </intent-filter>
        </activity>

    <activity
        android:name=".ValidateAccountActivity"
        android:label="@string/validate_account_title">
        <intent-filter android:autoVerify="true">
            <action android:name="android.intent.action.VIEW" />

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

            <data
                android:host="${notificareApplicationId}.applinks.notifica.re"
                android:pathPrefix="/oauth/validate"
                android:scheme="https" />
        </intent-filter>
    </activity>
</application>

These activities will handle the reset password and account activation process after users click those from an automated email message. Make sure you replace ${notificareApplicationId} with your own ID. You can find it in your app's configuration page, as described here.

Create an Account

You are able to create an account by simply invoking the method below and providing three required parameters: email, password and name.

Notificare.authentication().createAccount(email, password, name, object : NotificareCallback<Unit> {
    override fun onSuccess(result: Unit) {

    }

    override fun onFailure(e: Exception) {

    }
})
NotificareAuthenticationCompat.createAccount(email, password, name, new NotificareCallback<Unit>() {
    @Override
    public void onSuccess(Unit result) {

    }

    @Override
    public void onFailure(@NonNull Exception e) {

    }
});

    // OR

NotificareAuthenticationCompat.createAccount(email, password, new NotificareCallback<Unit>() {
    @Override
    public void onSuccess(Unit result) {

    }

    @Override
    public void onFailure(@NonNull Exception e) {

    }
});

Validate an Account

By default, a newly created account will receive a validation email. This will contain a link to validate the email address. If you've set up the ValidateAccountActivity as described above, the validation token can be parsed from the incoming data in the launch intent.

val token = Notificare.authentication().parseValidateUserToken(intent) ?: return
Notificare.authentication().validateUser(token, object : NotificareCallback<Unit> {
    override fun onSuccess(result: Unit) {

    }

    override fun onFailure(e: Exception) {

    }
})
String token = NotificareAuthenticationCompat.parseValidateUserToken(getIntent());
if (token != null) {
    NotificareAuthenticationCompat.validateUser(token, new NotificareCallback<Unit>() {
        @Override
        public void onSuccess(Unit result) {

        }

        @Override
        public void onFailure(@NonNull Exception e) {

        }
    });
}

Recover an Account

Provide your users with a way of requesting a new password when they forget it. You will have to acquire an email address from your user and call the following method. Upon success, an email with instructions will be sent to your user.

Notificare.authentication().sendPasswordReset(email, object : NotificareCallback<Unit> {
    override fun onSuccess(result: Unit) {

    }

    override fun onFailure(e: Exception) {

    }
})
NotificareAuthenticationCompat.sendPasswordReset(email, new NotificareCallback<Unit>() {
    @Override
    public void onSuccess(Unit result) {

    }

    @Override
    public void onFailure(@NonNull Exception e) {

    }
});

Reset a Password

Provide your users with a way of changing their password upon request. This method will require both a new password and the token received in the email. If you set up the ResetPasswordActivity to handle the correct URL scheme, you can parse the token from the incoming data in the launch intent.

val token = Notificare.authentication().parsePasswordResetToken(intent) ?: return
Notificare.authentication().resetPassword(password, token, object : NotificareCallback<Unit> {
    override fun onSuccess(result: Unit) {

    }

    override fun onFailure(e: Exception) {

    }
})
String token = NotificareAuthenticationCompat.parsePasswordResetToken(getIntent());
if (token != null) {
    NotificareAuthenticationCompat.resetPassword(password, token, new NotificareCallback<Unit>() {
        @Override
        public void onSuccess(Unit result) {

        }

        @Override
        public void onFailure(@NonNull Exception e) {

        }
    });
}

Authenticate an Account

To authenticate the user invoke the following method:

Notificare.authentication().login(email, password, object : NotificareCallback<Unit> {
    override fun onSuccess(result: Unit) {

    }

    override fun onFailure(e: Exception) {

    }
})
NotificareAuthenticationCompat.login(email, password, new NotificareCallback<Unit>() {
    @Override
    public void onSuccess(Unit result) {

    }

    @Override
    public void onFailure(@NonNull Exception e) {

    }
});

User Details

Once the user is signed in you can easily get their data by calling the following method:

Notificare.authentication().fetchUserDetails(object : NotificareCallback<NotificareUser> {
    override fun onSuccess(result: NotificareUser) {

    }

    override fun onFailure(e: Exception) {

    }
})
NotificareAuthenticationCompat.fetchUserDetails(new NotificareCallback<NotificareUser>() {
    @Override
    public void onSuccess(NotificareUser result) {

    }

    @Override
    public void onFailure(@NonNull Exception e) {

    }
});

Push Email Address

At any time after they have signed in, you can generate a push email address. Once you call the method below, you can send remote notifications to your users using an email message. Every time this method is invoked a new token/email will be generated.

Notificare.authentication().generatePushEmailAddress(object : NotificareCallback<NotificareUser> {
    override fun onSuccess(result: NotificareUser) {

    }

    override fun onFailure(e: Exception) {

    }
})
NotificareAuthenticationCompat.generatePushEmailAddress(new NotificareCallback<NotificareUser>() {
    @Override
    public void onSuccess(NotificareUser result) {

    }

    @Override
    public void onFailure(@NonNull Exception e) {

    }
});

Change a Password

To provide your signed-in users with the means to change their password you can easily invoke the following method.

Notificare.authentication().changePassword(newPassword, object : NotificareCallback<Unit> {
    override fun onSuccess(result: Unit) {

    }

    override fun onFailure(e: Exception) {

    }
})
NotificareAuthenticationCompat.changePassword(newPassword, new NotificareCallback<Unit>() {
    @Override
    public void onSuccess(Unit result) {

    }

    @Override
    public void onFailure(@NonNull Exception e) {

    }
});

Sign out

To log out the user invoke the following method:

Notificare.authentication().logout(object : NotificareCallback<Unit> {
    override fun onSuccess(result: Unit) {

    }

    override fun onFailure(e: Exception) {

    }
})
NotificareAuthenticationCompat.logout(new NotificareCallback<Unit>() {
    @Override
    public void onSuccess(Unit result) {

    }

    @Override
    public void onFailure(@NonNull Exception e) {

    }
});