forked from invertase/react-native-apple-authentication
-
Notifications
You must be signed in to change notification settings - Fork 0
/
type-test.tsx
118 lines (109 loc) · 3.51 KB
/
type-test.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
import React from 'react';
import { appleAuth, appleAuthAndroid, AppleButton} from '.';
import { View } from 'react-native';
/**
* iOS
*/
async function onAppleButtonPress() {
// sign in request
const responseObject = await appleAuth.performRequest({
requestedOperation: appleAuth.Operation.IMPLICIT,
requestedScopes: [appleAuth.Scope.EMAIL, appleAuth.Scope.FULL_NAME],
});
//authorization state request
const credentialState = await appleAuth.getCredentialStateForUser(responseObject.user);
if (credentialState === appleAuth.State.AUTHORIZED) {
// authorized
} else if (credentialState === appleAuth.State.NOT_FOUND) {
// not found
} else if (credentialState === appleAuth.State.REVOKED) {
// revoked
} else if (credentialState === appleAuth.State.TRANSFERRED) {
// transferred
}
// testing types
console.log(responseObject.authorizationCode);
console.log(responseObject.email);
console.log(responseObject.fullName);
console.log(responseObject.fullName?.familyName);
console.log(responseObject.fullName?.givenName);
console.log(responseObject.fullName?.middleName);
console.log(responseObject.fullName?.namePrefix);
console.log(responseObject.fullName?.nameSuffix);
console.log(responseObject.fullName?.nickname);
console.log(responseObject.identityToken);
console.log(responseObject.nonce);
console.log(responseObject.realUserStatus == appleAuth.UserStatus.LIKELY_REAL);
console.log(responseObject.realUserStatus == appleAuth.UserStatus.UNKNOWN);
console.log(responseObject.realUserStatus == appleAuth.UserStatus.UNSUPPORTED);
console.log(responseObject.state);
console.log(responseObject.user);
}
/**
* Android
*/
async function onAppleButtonPressAndroid() {
try {
// configure request
appleAuthAndroid.configure({
clientId: 'Client id',
redirectUri: 'https://example.com/auth/callback',
responseType: appleAuthAndroid.ResponseType.ALL,
scope: appleAuthAndroid.Scope.ALL,
nonce: 'Random nonce value, will be SHA256 hashed before sending to Apple',
state: 'State',
});
const response = await appleAuthAndroid.signIn();
// testing types
console.log(response.code);
console.log(response.id_token);
console.log(response.state);
console.log(response.user?.name?.firstName);
console.log(response.user?.name?.lastName);
console.log(response.user?.email);
} catch (error) {
if (error && error.message) {
switch (error.message) {
case appleAuthAndroid.Error.NOT_CONFIGURED:
console.log("appleAuthAndroid not configured yet.");
break;
case appleAuthAndroid.Error.SIGNIN_FAILED:
console.log("Apple signin failed.");
break;
case appleAuthAndroid.Error.SIGNIN_CANCELLED:
console.log("User cancelled Apple signin.");
break;
default:
break;
}
}
}
}
function App() {
return (
<View>
<AppleButton
cornerRadius={5}
buttonStyle={AppleButton.Style.BLACK}
buttonType={AppleButton.Type.SIGN_IN}
onPress={() => onAppleButtonPress()}
/>
{appleAuthAndroid.isSupported && (
<AppleButton
cornerRadius={5}
buttonStyle={AppleButton.Style.WHITE}
buttonType={AppleButton.Type.CONTINUE}
onPress={() => onAppleButtonPressAndroid()}
style={{
width: 200,
}}
textStyle={{
fontSize: 14,
}}
leftView={<View />}
/>
)}
</View>
);
}
App();