Skip to content

Handle Social Authentication Error

namnh-0652 edited this page Feb 27, 2023 · 1 revision

In social authentication, all libraries follow these predefined exceptions below:

Method/field Purpose
originalThrowable: Throwable The original error which is threw from Social Login process

This wrapper class for all error which is threw from Social Login process. Maybe it from Provider service (Ex: Google, Facebook) or from Firebase service.

This exception is occurred when the Provider can not generate authentication token from the given credentials.

This exception is occurred when user cancel social signIn process.

This exception is occurred when device's time or the given credentials is incorrect.

Handle Social Authentication error

Basically, it depends on you decision, however, we provides you a simple example to handle them

// Just example, you should handle more detail.
fun Context.handleSocialAuthError(error: Throwable) {
    val message = when (error) {
        is CancellationAuthException -> "User cancel signIn process!"
        is InvalidCredentialsException -> "Device's time or provided credentials is incorrect!"
        is UnexpectedAuthException -> "Provider can not verify this user, try later!"
        is SocialAuthException -> {
            error.originalThrowable?.message.orEmpty()
        }
        else -> "An unknown error occurs, try later!"
    }
    Toast.makeText(this, message, Toast.LENGTH_SHORT).show()
}