Skip to content

Commit 0f12a66

Browse files
authored
Change WeChat integration API #363
ref DEV-2809
2 parents 3e4f95c + eaee3f8 commit 0f12a66

File tree

6 files changed

+41
-55
lines changed

6 files changed

+41
-55
lines changed

example/reactnative/ios/reactNativeExample/Info.plist

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,16 +43,18 @@
4343
</array>
4444
<key>CFBundleVersion</key>
4545
<string>$(CURRENT_PROJECT_VERSION)</string>
46+
<key>ITSAppUsesNonExemptEncryption</key>
47+
<false/>
4648
<key>LSApplicationQueriesSchemes</key>
4749
<array>
4850
<string>weixin</string>
4951
<string>weixinULAPI</string>
52+
<string>weixinURLParamsAPI</string>
5053
</array>
5154
<key>LSRequiresIPhoneOS</key>
5255
<true/>
5356
<key>NSAppTransportSecurity</key>
5457
<dict>
55-
<!-- Do not change NSAllowsArbitraryLoads to true, or you will risk app rejection! -->
5658
<key>NSAllowsArbitraryLoads</key>
5759
<false/>
5860
<key>NSAllowsLocalNetworking</key>
@@ -78,7 +80,5 @@
7880
<string>Light</string>
7981
<key>UIViewControllerBasedStatusBarAppearance</key>
8082
<false/>
81-
<key>ITSAppUsesNonExemptEncryption</key>
82-
<false/>
8383
</dict>
8484
</plist>

example/reactnative/package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@
3434
"@typescript-eslint/parser": "^5.29.0",
3535
"babel-jest": "^29.6.3",
3636
"eslint": "^8.19.0",
37+
"//": "For unknown reason, we need to install eslint-plugin-jest oursevles so that it is found by eslint",
38+
"eslint-plugin-jest": "^27.9.0",
3739
"jest": "^29.6.3",
3840
"react-test-renderer": "18.3.1",
3941
"typescript": "5.0.4"

example/reactnative/src/screens/MainScreen.tsx

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,25 @@ const HomeScreen: React.FC = () => {
294294
});
295295
}, []);
296296

297+
const sendWechatAuthRequest = useCallback(
298+
(state: string) => {
299+
console.log('user click login with wechat, open wechat sdk');
300+
const {WechatAuth} = NativeModules;
301+
WechatAuth.sendWechatAuthRequest(state)
302+
.then((result: {code: string; state: string}) => {
303+
console.log('sending wechat auth callback');
304+
return authgear.wechatAuthCallback(result.code, result.state);
305+
})
306+
.then(() => {
307+
console.log('send wechat auth callback successfully');
308+
})
309+
.catch((err: Error) => {
310+
showError(err);
311+
});
312+
},
313+
[showError],
314+
);
315+
297316
const delegate: ReactNativeContainerDelegate = useMemo(() => {
298317
const d: ReactNativeContainerDelegate = {
299318
onSessionStateChange: (
@@ -305,24 +324,9 @@ const HomeScreen: React.FC = () => {
305324
setUserInfo(null);
306325
}
307326
},
308-
sendWechatAuthRequest: state => {
309-
console.log('user click login with wechat, open wechat sdk');
310-
const {WechatAuth} = NativeModules;
311-
WechatAuth.sendWechatAuthRequest(state)
312-
.then((result: {code: string; state: string}) => {
313-
console.log('sending wechat auth callback');
314-
return authgear.wechatAuthCallback(result.code, result.state);
315-
})
316-
.then(() => {
317-
console.log('send wechat auth callback successfully');
318-
})
319-
.catch((err: Error) => {
320-
showError(err);
321-
});
322-
},
323327
};
324328
return d;
325-
}, [setSessionState, setUserInfo, showError]);
329+
}, [setSessionState, setUserInfo]);
326330

327331
useEffect(() => {
328332
authgear.delegate = delegate;
@@ -374,6 +378,7 @@ const HomeScreen: React.FC = () => {
374378
android: {
375379
wechatRedirectURI,
376380
},
381+
sendWechatAuthRequest,
377382
})
378383
: undefined,
379384
isSSOEnabled,
@@ -398,6 +403,7 @@ const HomeScreen: React.FC = () => {
398403
useWebKitWebView,
399404
postConfigure,
400405
showError,
406+
sendWechatAuthRequest,
401407
]);
402408

403409
const login = useCallback(() => {

packages/authgear-react-native/src/index.ts

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -450,7 +450,6 @@ export class ReactNativeContainer {
450450
url: authorizeURL,
451451
redirectURI: options.redirectURI,
452452
shareCookiesWithDeviceBrowser: this._shareCookiesWithDeviceBrowser(),
453-
containerDelegate: this.delegate,
454453
});
455454
const xDeviceInfo = await getXDeviceInfo();
456455
const result = await this.baseContainer._finishAuthentication(
@@ -508,7 +507,6 @@ export class ReactNativeContainer {
508507
url: authorizeURL,
509508
redirectURI: options.redirectURI,
510509
shareCookiesWithDeviceBrowser: this._shareCookiesWithDeviceBrowser(),
511-
containerDelegate: this.delegate,
512510
});
513511
const xDeviceInfo = await getXDeviceInfo();
514512
await this.baseContainer._finishSettingsAction(redirectURL, {
@@ -583,7 +581,6 @@ export class ReactNativeContainer {
583581
url: endpoint,
584582
redirectURI: options.redirectURI,
585583
shareCookiesWithDeviceBrowser: this._shareCookiesWithDeviceBrowser(),
586-
containerDelegate: this.delegate,
587584
});
588585
const xDeviceInfo = await getXDeviceInfo();
589586
const result = await this.baseContainer._finishReauthentication(
@@ -794,7 +791,6 @@ export class ReactNativeContainer {
794791
url: authorizeURL,
795792
redirectURI: options.redirectURI,
796793
shareCookiesWithDeviceBrowser: this._shareCookiesWithDeviceBrowser(),
797-
containerDelegate: this.delegate,
798794
});
799795
const result = await this.baseContainer._finishAuthentication(
800796
redirectURL,
@@ -893,18 +889,6 @@ export class ReactNativeContainer {
893889
);
894890
}
895891

896-
/**
897-
* @internal
898-
*/
899-
_sendWechatRedirectURIToDelegate(deepLink: string): void {
900-
const u = new URL(deepLink);
901-
const params = u.searchParams;
902-
const state = params.get("state");
903-
if (state) {
904-
this.delegate?.sendWechatAuthRequest(state);
905-
}
906-
}
907-
908892
/**
909893
* Check whether biometric is supported on the current device.
910894
* If biometric is not supported, then a platform specific error is thrown.

packages/authgear-react-native/src/types.ts

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -26,19 +26,6 @@ export interface ReactNativeContainerDelegate {
2626
container: ReactNativeContainer,
2727
reason: SessionStateChangeReason
2828
) => void;
29-
30-
/**
31-
* This callback will be called when user click login with WeChat in
32-
* react-native.
33-
*
34-
* Developer should implement this function to use WeChat SDK to
35-
* obtain WeChat authentication code. After obtaining the code, developer
36-
* should call wechatAuthCallback with code and state to complete the
37-
* WeChat login.
38-
*
39-
* @public
40-
*/
41-
sendWechatAuthRequest(state: string): void;
4229
}
4330

4431
/**

packages/authgear-react-native/src/ui_implementation.ts

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import URL from "core-js-pure/features/url";
22
import { openAuthorizeURL, openAuthorizeURLWithWebView } from "./nativemodule";
3-
import { ReactNativeContainerDelegate } from "./types";
43
import eventEmitter from "./eventEmitter";
54

65
/**
@@ -24,11 +23,6 @@ export interface OpenAuthorizationURLOptions {
2423
* such as those underlying implementations are based on ASWebAuthenticationSession, or CustomTabs.
2524
*/
2625
shareCookiesWithDeviceBrowser: boolean;
27-
28-
/**
29-
* @internal
30-
*/
31-
containerDelegate?: ReactNativeContainerDelegate;
3226
}
3327

3428
/**
@@ -134,6 +128,19 @@ export interface WebKitWebViewUIImplementationOptionsAndroid {
134128
export interface WebKitWebViewUIImplementationOptions {
135129
ios?: WebKitWebViewUIImplementationOptionsIOS;
136130
android?: WebKitWebViewUIImplementationOptionsAndroid;
131+
132+
/**
133+
* This callback will be called when user click login with WeChat in
134+
* react-native.
135+
*
136+
* Developer should implement this function to use WeChat SDK to
137+
* obtain WeChat authentication code. After obtaining the code, developer
138+
* should call wechatAuthCallback with code and state to complete the
139+
* WeChat login.
140+
*
141+
* @public
142+
*/
143+
sendWechatAuthRequest?: (state: string) => void;
137144
}
138145

139146
/**
@@ -166,7 +173,7 @@ export class WebKitWebViewUIImplementation implements UIImplementation {
166173
const url = new URL(args.url);
167174
const state = url.searchParams.get("state");
168175
if (state != null) {
169-
options.containerDelegate?.sendWechatAuthRequest(state);
176+
this.options?.sendWechatAuthRequest?.(state);
170177
}
171178
}
172179
}

0 commit comments

Comments
 (0)