Skip to content

Commit fd8b3da

Browse files
Romick2005mikehardy
authored andcommitted
Implement apple web based authentication using react native WebView.
1 parent 13c7150 commit fd8b3da

File tree

3 files changed

+393
-40
lines changed

3 files changed

+393
-40
lines changed

README.md

Lines changed: 105 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -228,56 +228,121 @@ This library works on MacOS 10.15+ if using in conjunction with [react-native-ma
228228

229229
### Web (not react-native-web, but that may come as a follow-on, this is pure web at the moment)
230230

231+
### WebView
232+
231233
#### 1. Initial set-up
232-
- Ensure you follow the android steps above.
233-
- Install the [web counterpart](https://github.com/A-Tokyo/react-apple-signin-auth) `yarn add react-apple-signin-auth` in your web project.
234+
- Make sure to correctly configure your Apple developer account to allow for proper web based authentication.
235+
- 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).
236+
- Your backend needs to implement web based authentification
234237

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

276338
#### 3. Verify serverside
277339
- Send the apple response to your server.
278340
- See [Serverside Verification](#serverside-verification)
279341
- Ensure that you pass the clientID as the web service ID, not the native app bundle. Since the project utilizes the service ID for authenticating web and android.
280342

343+
### WebView Config
344+
- https://developer.apple.com/documentation/sign_in_with_apple/sign_in_with_apple_js/incorporating_sign_in_with_apple_into_other_platforms
345+
281346
## Serverside verification
282347

283348
#### Nonce

0 commit comments

Comments
 (0)