Skip to content

Commit

Permalink
Close change password page on success for react-native authgear#265
Browse files Browse the repository at this point in the history
  • Loading branch information
IniZio committed Feb 22, 2024
1 parent c3f0fa2 commit dafd090
Show file tree
Hide file tree
Showing 8 changed files with 50 additions and 2 deletions.
17 changes: 17 additions & 0 deletions example/reactnative/src/screens/MainScreen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -560,6 +560,16 @@ const HomeScreen: React.FC = () => {
.catch(err => showError(err));
}, [showError, colorScheme]);

const changePassword = useCallback(() => {
authgear
.open(Page.ChangePassword, {
colorScheme: colorScheme as ColorScheme,
wechatRedirectURI,
closeOnSuccess: true,
})
.catch(err => showError(err));
}, [showError, colorScheme]);

const fetchUserInfo = useCallback(() => {
setLoading(true);
authgear
Expand Down Expand Up @@ -750,6 +760,13 @@ const HomeScreen: React.FC = () => {
disabled={!initialized || !loggedIn}
/>
</View>
<View style={styles.button}>
<Button
title="Change Password"
onPress={changePassword}
disabled={!initialized || !loggedIn}
/>
</View>
<View style={styles.button}>
<Button
title="Fetch User Info"
Expand Down
1 change: 1 addition & 0 deletions packages/authgear-core/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -337,5 +337,6 @@ export enum SessionStateChangeReason {
*/
export enum Page {
Settings = "/settings",
ChangePassword = "/settings/change_password",
Identities = "/settings/identities",
}
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@ public class AuthgearReactNativeModule extends ReactContextBaseJavaModule implem

private static final int ACTIVITY_PROMISE_TAG_CODE_AUTHORIZATION = 1;
private static final int ACTIVITY_PROMISE_TAG_OPEN_URL = 2;

private static final String SDK_REDIRECT_URI = "authgearsdk://host/path";

private StartActivityHandles mHandles;

private final ReactApplicationContext reactContext;
Expand Down Expand Up @@ -100,6 +103,15 @@ public static Boolean handleWechatRedirectDeepLink(Uri deepLink) {
return false;
}

public static Boolean handleSDKRedirectDeepLink(Uri deepLink, Activity activity) {
String deepLinkWithoutQuery = getURLWithoutQuery(deepLink);
if (SDK_REDIRECT_URI.equals(deepLinkWithoutQuery)) {
activity.finish();
return true;
}
return false;
}

@Override
public String getName() {
return "AuthgearReactNative";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,9 @@ private boolean shouldOverrideUrlLoading(Uri uri) {
if (AuthgearReactNativeModule.handleWechatRedirectDeepLink(uri)) {
return true;
}
if (AuthgearReactNativeModule.handleSDKRedirectDeepLink(uri, this.activity)) {
return true;
};
if (this.checkRedirectURI(uri)) {
return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

String url = this.getIntent().getStringExtra(KEY_URL);
Activity activity = this;
this.webView = new WebView(this);
this.setContentView(this.webView);
this.webView.setWebViewClient(new WebViewClient(){
Expand All @@ -42,6 +43,10 @@ public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request
if (AuthgearReactNativeModule.handleWechatRedirectDeepLink(uri)) {
return true;
};
if (AuthgearReactNativeModule.handleSDKRedirectDeepLink(uri, activity)) {
return true;
};

return super.shouldOverrideUrlLoading(view, request);
}

Expand All @@ -52,6 +57,9 @@ public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (AuthgearReactNativeModule.handleWechatRedirectDeepLink(uri)) {
return true;
};
if (AuthgearReactNativeModule.handleSDKRedirectDeepLink(uri, activity)) {
return true;
};
return super.shouldOverrideUrlLoading(view, url);
}
});
Expand All @@ -69,7 +77,7 @@ public boolean onShowFileChooser(WebView webView, ValueCallback<Uri[]> filePathC
webSettings.setJavaScriptEnabled(true);
this.webView.loadUrl(url);
}

@Override
public void onBackPressed() {
if (this.webView.canGoBack()) {
Expand Down
2 changes: 1 addition & 1 deletion packages/authgear-react-native/ios/AGAuthgearReactNative.m
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ + (BOOL)application:(UIApplication *)application
resolve:(RCTPromiseResolveBlock)resolve
reject:(RCTPromiseRejectBlock)reject)
{
NSString *scheme = @"nocallback";
NSString *scheme = @"authgearsdk";
if (@available(iOS 12.0, *)) {
self.asSession = [[ASWebAuthenticationSession alloc] initWithURL:url
callbackURLScheme:scheme
Expand Down
3 changes: 3 additions & 0 deletions packages/authgear-react-native/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -476,6 +476,9 @@ export class ReactNativeContainer {
if (options?.colorScheme != null) {
q.set("x_color_scheme", options.colorScheme);
}
if (options?.closeOnSuccess === true) {
q.set("redirect_uri", "authgearsdk://host/path");
}
u.search = "?" + q.toString();

let targetURL = u.toString();
Expand Down
4 changes: 4 additions & 0 deletions packages/authgear-react-native/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,10 @@ export interface SettingOptions {
* The wechatRedirectURI will be called when user click the login with WeChat button
*/
wechatRedirectURI?: string;
/**
* Close the settings page when the user successfully updates the settings.
*/
closeOnSuccess?: boolean;
}

/**
Expand Down

0 comments on commit dafd090

Please sign in to comment.