-
Notifications
You must be signed in to change notification settings - Fork 82
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
Update LockCallback and AuthenticationCallback [SDK-2480] #621
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -25,12 +25,10 @@ | |
package com.auth0.android.lock; | ||
|
||
import android.content.Intent; | ||
import android.util.Log; | ||
|
||
import androidx.annotation.NonNull; | ||
|
||
import com.auth0.android.authentication.AuthenticationException; | ||
import com.auth0.android.lock.utils.LockException; | ||
import com.auth0.android.result.Credentials; | ||
|
||
import java.util.Date; | ||
|
@@ -42,32 +40,24 @@ | |
*/ | ||
public abstract class AuthenticationCallback implements LockCallback { | ||
|
||
private static final String TAG = AuthenticationCallback.class.getSimpleName(); | ||
|
||
/** | ||
* Called when the authentication flow finished successfully. | ||
* | ||
* @param credentials with the tokens. | ||
*/ | ||
public abstract void onAuthentication(@NonNull Credentials credentials); | ||
|
||
/** | ||
* Called when the user goes back and closes the activity, without using an Authentication flow. | ||
*/ | ||
public abstract void onCanceled(); | ||
|
||
@Override | ||
public void onEvent(@LockEvent int event, @NonNull Intent data) { | ||
switch (event) { | ||
case LockEvent.CANCELED: | ||
onCanceled(); | ||
break; | ||
case LockEvent.AUTHENTICATION: | ||
parseAuthentication(data); | ||
break; | ||
case LockEvent.RESET_PASSWORD: | ||
case LockEvent.SIGN_UP: | ||
break; | ||
if (data.hasExtra(Constants.EXCEPTION_EXTRA)) { | ||
onError((AuthenticationException) data.getSerializableExtra(Constants.EXCEPTION_EXTRA)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. any exception available would be raised through |
||
return; | ||
} | ||
if (event == LockEvent.AUTHENTICATION) { | ||
Credentials credentials = extractCredentials(data); | ||
onAuthentication(credentials); | ||
} else if (event == LockEvent.CANCELED) { | ||
onError(new AuthenticationException("a0.authentication_canceled", "The user pressed back")); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If the event is "canceled", then raise an exception with the specific code that represents this. This is only for |
||
} | ||
} | ||
|
||
|
@@ -76,21 +66,13 @@ public void onEvent(@LockEvent int event, @NonNull Intent data) { | |
* | ||
* @param data the intent received at the end of the login process. | ||
*/ | ||
private void parseAuthentication(Intent data) { | ||
if (data.hasExtra(Constants.EXCEPTION_EXTRA)) { | ||
AuthenticationException error = (AuthenticationException) data.getSerializableExtra(Constants.EXCEPTION_EXTRA); | ||
onError(new LockException(error)); | ||
return; | ||
} | ||
private Credentials extractCredentials(Intent data) { | ||
String idToken = data.getStringExtra(Constants.ID_TOKEN_EXTRA); | ||
String accessToken = data.getStringExtra(Constants.ACCESS_TOKEN_EXTRA); | ||
String tokenType = data.getStringExtra(Constants.TOKEN_TYPE_EXTRA); | ||
String refreshToken = data.getStringExtra(Constants.REFRESH_TOKEN_EXTRA); | ||
Date expiresAt = (Date) data.getSerializableExtra(Constants.EXPIRES_AT_EXTRA); | ||
String scope = data.getStringExtra(Constants.SCOPE_EXTRA); | ||
Credentials credentials = new Credentials(idToken, accessToken, tokenType, refreshToken, expiresAt, scope); | ||
|
||
Log.d(TAG, "User authenticated!"); | ||
onAuthentication(credentials); | ||
return new Credentials(idToken, accessToken, tokenType, refreshToken, expiresAt, scope); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -41,7 +41,6 @@ | |
import com.auth0.android.lock.internal.configuration.Options; | ||
import com.auth0.android.lock.internal.configuration.Theme; | ||
import com.auth0.android.lock.provider.AuthResolver; | ||
import com.auth0.android.lock.utils.LockException; | ||
import com.auth0.android.lock.utils.SignUpField; | ||
import com.auth0.android.provider.AuthHandler; | ||
import com.auth0.android.provider.CustomTabsOptions; | ||
|
@@ -148,15 +147,15 @@ private void initialize(@NonNull Context context) { | |
} | ||
|
||
private void processEvent(@NonNull Intent data) { | ||
if (data.hasExtra(Constants.EXCEPTION_EXTRA)) { | ||
callback.onError((AuthenticationException) data.getSerializableExtra(Constants.EXCEPTION_EXTRA)); | ||
return; | ||
} | ||
String action = data.getAction(); | ||
switch (action) { | ||
case Constants.AUTHENTICATION_ACTION: | ||
Log.v(TAG, "AUTHENTICATION action received in our BroadcastReceiver"); | ||
if (data.hasExtra(Constants.EXCEPTION_EXTRA)) { | ||
callback.onError(new LockException((AuthenticationException) data.getSerializableExtra(Constants.EXCEPTION_EXTRA))); | ||
} else { | ||
callback.onEvent(LockEvent.AUTHENTICATION, data); | ||
} | ||
callback.onEvent(LockEvent.AUTHENTICATION, data); | ||
break; | ||
case Constants.SIGN_UP_ACTION: | ||
Log.v(TAG, "SIGN_UP action received in our BroadcastReceiver"); | ||
|
@@ -168,7 +167,7 @@ private void processEvent(@NonNull Intent data) { | |
break; | ||
case Constants.INVALID_CONFIGURATION_ACTION: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We don't have a "public" event type for this. So we raise an exception instead. |
||
Log.v(TAG, "INVALID_CONFIGURATION_ACTION action received in our BroadcastReceiver"); | ||
callback.onError(new LockException(data.getStringExtra(Constants.ERROR_EXTRA))); | ||
callback.onError(new AuthenticationException("a0.invalid_configuration", data.getStringExtra(Constants.ERROR_EXTRA))); | ||
break; | ||
} | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -25,10 +25,11 @@ | |
package com.auth0.android.lock; | ||
|
||
import android.content.Intent; | ||
|
||
import androidx.annotation.IntDef; | ||
import androidx.annotation.NonNull; | ||
|
||
import com.auth0.android.lock.utils.LockException; | ||
import com.auth0.android.authentication.AuthenticationException; | ||
|
||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
|
@@ -68,5 +69,5 @@ public interface LockCallback { | |
* | ||
* @param error describing what happened. | ||
*/ | ||
void onError(@NonNull LockException error); | ||
void onError(@NonNull AuthenticationException error); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Changes the type of exception. Helps to deliver the actual exception and not wrapping everything again. |
||
} |
This file was deleted.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
AuthenticationCallback
is a subclass ofLockCallback
. TheonCanceled
is now merged intoonError
for those developers passing anAuthenticationCallback
implementation (what we document and recommend).