Skip to content

Commit 2bace6f

Browse files
committed
invertase#166 Implement apple web based authentication using react native WebView.
1 parent b3962c3 commit 2bace6f

File tree

3 files changed

+391
-43
lines changed

3 files changed

+391
-43
lines changed

README.md

+103-43
Original file line numberDiff line numberDiff line change
@@ -221,52 +221,110 @@ async function onAppleButtonPress() {
221221
}
222222
```
223223

224-
225-
### Web (not react-native-web, but that may come as a follow-on, this is pure web at the moment)
226-
224+
### WebView
227225
#### 1. Initial set-up
228-
- Ensure you follow the android steps above.
229-
- Install the [web counterpart](https://github.com/A-Tokyo/react-apple-signin-auth) `yarn add react-apple-signin-auth` in your web project.
226+
- Make sure to correctly configure your Apple developer account to allow for proper web based authentication.
227+
- Install the [React Native WebView](https://github.com/react-native-webview/react-native-webview) `yarn add react-native-webview` (or) `npm i react-native-webview` in your project. [Link native dependencies](https://github.com/react-native-webview/react-native-webview/blob/master/docs/Getting-Started.md#2-link-native-dependencies).
228+
- Your backend needs to implement web based authentification
230229

231-
#### 2. Implement the login process on web
230+
#### 2. Implement the login process
232231
```js
233-
import AppleSignin from 'react-apple-signin-auth';
234-
235-
/** Apple Signin button */
236-
const MyAppleSigninButton = ({ ...rest }) => (
237-
<AppleSignin
238-
/** Auth options passed to AppleID.auth.init() */
239-
authOptions={{
240-
clientId: 'SAME AS ANDROID',
241-
redirectURI: 'SAME AS ANDROID',
242-
scope: 'email name',
243-
state: 'state',
244-
/** sha256 nonce before sending to apple to unify with native firebase behavior - https://github.com/invertase/react-native-apple-authentication/issues/28 */
245-
nonce: sha256('nonce'),
246-
/** We have to usePopup since we need clientSide authentication */
247-
usePopup: true,
248-
}}
249-
onSuccess={(response) => {
250-
console.log(response);
251-
// {
252-
// "authorization": {
253-
// "state": "[STATE]",
254-
// "code": "[CODE]",
255-
// "id_token": "[ID_TOKEN]"
256-
// },
257-
// "user": {
258-
// "email": "[EMAIL]",
259-
// "name": {
260-
// "firstName": "[FIRST_NAME]",
261-
// "lastName": "[LAST_NAME]"
262-
// }
263-
// }
264-
// }
265-
}}
266-
/>
267-
);
268-
269-
export default MyAppleSigninButton;
232+
233+
// App.js
234+
import React from 'react';
235+
import {
236+
View,
237+
TouchableWithoutFeedback
238+
Text
239+
} from 'react-native';
240+
import {
241+
appleAuth,
242+
appleAuthAndroid,
243+
AppleAuthWebView // Internaly using WebView
244+
} from "@invertase/react-native-apple-authentication";
245+
246+
function onAppleLoginWebViewButtonPress() {
247+
248+
// https://developer.apple.com/documentation/sign_in_with_apple/sign_in_with_apple_js/incorporating_sign_in_with_apple_into_other_platforms
249+
const appleAuthConfig = {
250+
// The Service ID you registered with Apple
251+
clientId: "com.example.client-web",
252+
253+
// Return URL added to your Apple dev console. It must still match the URL you provided to Apple.
254+
redirectUri: "https://example.com/auth/callback",
255+
256+
// The type of response requested - code, id_token, or both.
257+
responseType: "code id_token",
258+
259+
// The amount of user information requested from Apple.
260+
scope: "name email"
261+
262+
// Random nonce value that will be SHA256 hashed before sending to Apple.
263+
// nonce: nonce,
264+
265+
// Unique state value used to prevent CSRF attacks. A UUID will be generated if nothing is provided.
266+
// state: state
267+
};
268+
269+
this.setState(
270+
{
271+
appleAuthConfig: appleAuthConfig
272+
}
273+
);
274+
}
275+
276+
function onAppleAuthResponse(responseContent) {
277+
278+
// Handle your server response (after login - apple redirects to your server url)
279+
console.log("onAppleAuthResponse responseContent", responseContent);
280+
}
281+
282+
// Apple authentication requires API 19+, so we check before showing the login button
283+
// If no iOS or Android is supported than we use webView fallback with custom button
284+
function App() {
285+
286+
render() {
287+
const appleAuthConfig = this.state.appleAuthConfig;
288+
289+
if (appleAuthConfig) {
290+
return (
291+
<AppleAuthWebView
292+
config={appleAuthConfig}
293+
// optional loadingIndicator property
294+
// loadingIndicator={
295+
// () => {
296+
// return (
297+
// <Loading />
298+
// );
299+
// }
300+
// }
301+
onResponse={this.onAppleAuthResponse}
302+
/>
303+
);
304+
}
305+
}
306+
307+
return (
308+
<View>
309+
310+
{
311+
// (appleAuth.isSupported || appleAuthAndroid.isSupported) ? (
312+
// <AppleButton
313+
// buttonStyle={AppleButton.Style.BLACK}
314+
// buttonType={AppleButton.Type.SIGN_IN}
315+
// onPress={this.onAppleLoginButtonPress}
316+
// />
317+
// ) // else add webView view button
318+
}
319+
320+
<TouchableWithoutFeedback onPress={this.onAppleLoginWebViewButtonPress}>
321+
<Text>
322+
Sign in with Apple
323+
</Text>
324+
</TouchableWithoutFeedback>
325+
</View>
326+
);
327+
}
270328
```
271329

272330
#### 3. Verify serverside
@@ -322,6 +380,8 @@ export default MyAppleSigninButton;
322380
- [AndroidResponseType](docs/enums/_lib_index_d_.androidresponsetype.md)
323381
- [AndroidScope](docs/enums/_lib_index_d_.androidscope.md)
324382

383+
### WebView Config
384+
- https://developer.apple.com/documentation/sign_in_with_apple/sign_in_with_apple_js/incorporating_sign_in_with_apple_into_other_platforms
325385

326386
## FAQs
327387

0 commit comments

Comments
 (0)