Skip to content

Commit

Permalink
Add deleteAccount()
Browse files Browse the repository at this point in the history
ref DEV-1525
  • Loading branch information
louischan-oursky committed Jul 17, 2024
2 parents 59600e8 + 635cfe0 commit 4d4b3ab
Show file tree
Hide file tree
Showing 7 changed files with 91 additions and 4 deletions.
4 changes: 2 additions & 2 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ plugins {
// https://developer.android.com/build/releases/gradle-plugin#8-1-0
// If you ever update Android Gradle Plugin, please also update
// BUILD_TOOLS_VERSION in ./.github/workflows/ci.yaml
id("com.android.library") version "8.5.0" apply false
id("com.android.application") version "8.5.0" apply false
id("com.android.library") version "8.5.1" apply false
id("com.android.application") version "8.5.1" apply false
id("org.jetbrains.kotlin.android") version "1.9.23" apply false
kotlin("plugin.serialization") version "1.9.20" apply false
id("org.jetbrains.dokka") version "1.9.10"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ public class MainActivity extends AppCompatActivity {
private View mAuthenticateBiometric;
private View mOpenSettings;
private View mChangePassword;
private View mDeleteAccount;
private View mFetchUserInfo;
private View mShowAuthTime;
private View mLogout;
Expand Down Expand Up @@ -92,6 +93,7 @@ protected void onCreate(Bundle savedInstanceState) {
mAuthenticateBiometric = findViewById(R.id.authenticateBiometric);
mOpenSettings = findViewById(R.id.openSettings);
mChangePassword = findViewById(R.id.changePassword);
mDeleteAccount = findViewById(R.id.deleteAccount);
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) {
mAuthenticateBiometric.setOnClickListener(view -> viewModel.authenticateBiometric(this));
mOpenSettings.setOnClickListener(view -> viewModel.openSettings());
mChangePassword.setOnClickListener(view -> viewModel.openChangePassword());
mDeleteAccount.setOnClickListener(view -> viewModel.openDeleteAccount());
mFetchUserInfo.setOnClickListener(view -> viewModel.fetchUserInfo());
mShowAuthTime.setOnClickListener(view -> viewModel.showAuthTime(this));
mLogout.setOnClickListener(view -> viewModel.logout());
Expand Down Expand Up @@ -318,6 +321,8 @@ private void updateButtonDisabledState(MainViewModel viewModel) {
mDisableBiometric.setEnabled(!isLoading && isConfigured && isBiometricEnabled);
mAuthenticateBiometric.setEnabled(!isLoading && isConfigured && !isLoggedIn && isBiometricEnabled);
mOpenSettings.setEnabled(!isLoading && isConfigured && isLoggedIn);
mChangePassword.setEnabled(!isLoading && isConfigured && isLoggedIn);
mDeleteAccount.setEnabled(!isLoading && isConfigured && isLoggedIn);
mFetchUserInfo.setEnabled(!isLoading && isConfigured && isLoggedIn);
mShowAuthTime.setEnabled(!isLoading && isConfigured && isLoggedIn);
mLogout.setEnabled(!isLoading && isConfigured && isLoggedIn);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -715,6 +715,43 @@ public void onFailed(Throwable throwable) {
});
}

public void openDeleteAccount() {
mIsLoading.setValue(true);

mAuthgear.refreshIDToken(new OnRefreshIDTokenListener() {
@Override
public void onFinished() {
SettingsActionOptions options = new SettingsActionOptions(
MainApplication.AUTHGEAR_REDIRECT_URI
);
options.setColorScheme(getColorScheme());
options.setWechatRedirectURI(MainApplication.AUTHGEAR_WECHAT_REDIRECT_URI);
mAuthgear.deleteAccount(options, new OnOpenSettingsActionListener() {
@Override
public void onFinished() {
mIsLoading.setValue(false);
Log.d(TAG, "deleteAccount finished");
}

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

@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 @@ -464,6 +464,15 @@
android:text="Change password"
tools:ignore="HardcodedText" />

<Button
android:id="@+id/deleteAccount"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginBottom="16dp"
android:text="Delete account"
tools:ignore="HardcodedText" />

<Button
android:id="@+id/fetchUserInfo"
android:layout_width="wrap_content"
Expand Down
35 changes: 35 additions & 0 deletions sdk/src/main/java/com/oursky/authgear/Authgear.kt
Original file line number Diff line number Diff line change
Expand Up @@ -508,6 +508,41 @@ constructor(
}
}

/**
* Open delete account page in webview
*
* @param options Setting options.
* @param listener The listener.
* @param handler The handler of the thread on which the listener is called.
*/
@MainThread
@JvmOverloads
public fun deleteAccount(
options: SettingsActionOptions,
listener: OnOpenSettingsActionListener? = null,
handler: Handler = Handler(Looper.getMainLooper())
) {
scope.launch {
try {
core.settingsAction(
SettingsAction.DELETE_ACCOUNT,
options
)
core.clearSession(SessionStateChangeReason.INVALID)
handler.post {
listener?.onFinished()
}
} catch (e: Throwable) {
e.printStackTrace()
handler.post {
listener?.onFailed(e)
}
} finally {
AuthgearCore.unregisteredWechatRedirectURI()
}
}
}

/**
* Promote the current anonymous user. Note that this must not be called before there is an
* anonymous user.
Expand Down
2 changes: 1 addition & 1 deletion sdk/src/main/java/com/oursky/authgear/AuthgearCore.kt
Original file line number Diff line number Diff line change
Expand Up @@ -745,7 +745,7 @@ internal class AuthgearCore(
}
}

private fun clearSession(changeReason: SessionStateChangeReason) {
internal fun clearSession(changeReason: SessionStateChangeReason) {
tokenStorage.deleteRefreshToken(name)
storage.deleteApp2AppDeviceKeyId(name)
synchronized(this) {
Expand Down
3 changes: 2 additions & 1 deletion sdk/src/main/java/com/oursky/authgear/SettingsAction.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.oursky.authgear

enum class SettingsAction(val raw: String) {
CHANGE_PASSWORD("change_password")
CHANGE_PASSWORD("change_password"),
DELETE_ACCOUNT("delete_account"),
}

0 comments on commit 4d4b3ab

Please sign in to comment.