Skip to content

Commit

Permalink
Close change password page on success authgear#183
Browse files Browse the repository at this point in the history
  • Loading branch information
IniZio committed Feb 22, 2024
1 parent c555fe7 commit 8a93cbe
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ public class MainActivity extends AppCompatActivity {
private View mDisableBiometric;
private View mAuthenticateBiometric;
private View mOpenSettings;
private View mChangePassword;
private View mFetchUserInfo;
private View mShowAuthTime;
private View mLogout;
Expand Down Expand Up @@ -87,6 +88,7 @@ protected void onCreate(Bundle savedInstanceState) {
mDisableBiometric = findViewById(R.id.disableBiometric);
mAuthenticateBiometric = findViewById(R.id.authenticateBiometric);
mOpenSettings = findViewById(R.id.openSettings);
mChangePassword = findViewById(R.id.changePassword);
mFetchUserInfo = findViewById(R.id.fetchUserInfo);
mShowAuthTime = findViewById(R.id.showAuthTime);
mLogout = findViewById(R.id.logout);
Expand Down Expand Up @@ -149,6 +151,7 @@ protected void onCreate(Bundle savedInstanceState) {
mDisableBiometric.setOnClickListener(view -> viewModel.disableBiometric());
mAuthenticateBiometric.setOnClickListener(view -> viewModel.authenticateBiometric(this));
mOpenSettings.setOnClickListener(view -> viewModel.openSettings());
mChangePassword.setOnClickListener(view -> viewModel.openChangePassword());
mFetchUserInfo.setOnClickListener(view -> viewModel.fetchUserInfo());
mShowAuthTime.setOnClickListener(view -> viewModel.showAuthTime(this));
mLogout.setOnClickListener(view -> viewModel.logout());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -660,6 +660,28 @@ public void onFailed(Throwable throwable) {
});
}

public void openChangePassword() {
mIsLoading.setValue(true);
SettingOptions options = new SettingOptions();
options.setColorScheme(getColorScheme());
options.setWechatRedirectURI(MainApplication.AUTHGEAR_WECHAT_REDIRECT_URI);
options.setCloseOnSuccess(true);
mAuthgear.open(Page.CHANGE_PASSWORD, options, new OnOpenURLListener() {
@Override
public void onClosed() {
mIsLoading.setValue(false);
Log.d(TAG, "chnagePassword closed");
}

@Override
public void onFailed(Throwable throwable) {
Log.d(TAG, throwable.toString());
mIsLoading.setValue(false);
setError(throwable);
}
});
}

public void promoteAnonymousUser() {
mIsLoading.setValue(true);
PromoteOptions options = new PromoteOptions(MainApplication.AUTHGEAR_REDIRECT_URI);
Expand Down
9 changes: 9 additions & 0 deletions javasample/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -447,6 +447,15 @@
android:text="Open settings"
tools:ignore="HardcodedText" />

<Button
android:id="@+id/changePassword"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="16dp"
android:layout_gravity="center_horizontal"
android:text="Change password"
tools:ignore="HardcodedText" />

<Button
android:id="@+id/fetchUserInfo"
android:layout_width="wrap_content"
Expand Down
14 changes: 14 additions & 0 deletions sdk/src/main/java/com/oursky/authgear/AuthgearCore.kt
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ internal class AuthgearCore(
const val KEY_OAUTH_BOARDCAST_TYPE = "boardcastType"
const val KEY_REDIRECT_URL = "redirectUrl"
const val CODE_CHALLENGE_METHOD = "S256"
const val SDK_REDIRECT_URL = "authgearsdk://host/path"

/**
* Check and handle wehchat redirect uri and trigger delegate function if needed
Expand Down Expand Up @@ -124,6 +125,15 @@ internal class AuthgearCore(
return true
}

fun handleAuthgearDeepLink(deepLink: String): Boolean {
val deepLinkWithoutQuery = getURLWithoutQuery(deepLink)
if (deepLinkWithoutQuery != SDK_REDIRECT_URL) {
return false
}

throw CancelException()
}

private fun getURLWithoutQuery(input: String): String {
val uri = Uri.parse(input)
var builder = uri.buildUpon().clearQuery()
Expand Down Expand Up @@ -381,6 +391,9 @@ internal class AuthgearCore(
options?.uiLocales?.let {
builder.appendQueryParameter("ui_locales", it.joinToString(" "))
}
if (options?.closeOnSuccess == true) {
builder.appendQueryParameter("redirect_uri", SDK_REDIRECT_URL)
}
val redirectUri = builder.build()
return generateUrl(redirectUri.toString(), options)
}
Expand Down Expand Up @@ -453,6 +466,7 @@ internal class AuthgearCore(
openUrl(
when (page) {
Page.SETTINGS -> "/settings"
Page.CHANGE_PASSWORD -> "/settings/change_password"
Page.IDENTITY -> "/settings/identities"
},
options
Expand Down
1 change: 1 addition & 0 deletions sdk/src/main/java/com/oursky/authgear/Page.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@ package com.oursky.authgear
*/
enum class Page {
SETTINGS,
CHANGE_PASSWORD,
IDENTITY
}
6 changes: 5 additions & 1 deletion sdk/src/main/java/com/oursky/authgear/SettingOptions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,9 @@ data class SettingOptions @JvmOverloads constructor(
* Theme override
*/
var colorScheme: ColorScheme? = null,
var uiLocales: List<String>? = null
var uiLocales: List<String>? = null,
/**
* Close webview on success
*/
var closeOnSuccess: Boolean? = false,
)
3 changes: 3 additions & 0 deletions sdk/src/main/java/com/oursky/authgear/WebViewActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ internal class WebViewActivity : AppCompatActivity() {
if (AuthgearCore.handleWechatRedirectDeepLink(deepLink)) {
return true
}
if (AuthgearCore.handleAuthgearDeepLink(deepLink)) {
return true
}
return super.shouldOverrideUrlLoading(view, request)
}
})
Expand Down

0 comments on commit 8a93cbe

Please sign in to comment.