forked from juliandramirez/react-native-user-identity
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
71 lines (64 loc) · 2.54 KB
/
index.js
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
import { Platform, NativeModules, Alert } from 'react-native'
const { RNUserIdentity } = NativeModules
export const ICLOUD_ACCESS_ERROR = 'ICLOUD_ACCESS_ERROR'
export default {
getUserId: async ({ androidAccountSelectionMessage, iosUserConfirmation } = {
androidAccountSelectionMessage: null,
iosUserConfirmation: null
}) => {
if (Platform.OS === 'ios') {
const showUserConfirmation = () => new Promise((resolve, reject) => {
const {
title = 'Sign in with iCloud',
message = 'Your iCloud account will be used to sign in',
signInButtonText = 'Sign In',
cancelButtonText = 'Cancel'
} = iosUserConfirmation
Alert.alert(
title,
message,
[{
text: signInButtonText,
onPress: () => {
RNUserIdentity.getUserIdentity()
.then(value => resolve(value))
.catch(error => {
reject(error)
})
}
}, {
text: cancelButtonText,
onPress: () => resolve(null),
style: 'cancel',
}]
)
})
try {
if (iosUserConfirmation) {
return await showUserConfirmation()
} else {
return await RNUserIdentity.getUserIdentity()
}
} catch (error) {
if (error && error.code == 'NO_ACCOUNT_ACCESS_ERROR') {
// there is no account configured...
throw ICLOUD_ACCESS_ERROR
} else {
throw error
}
}
} else if (Platform.OS === 'android') {
try {
return await RNUserIdentity.triggerAccountSelection(androidAccountSelectionMessage, 'com.google')
} catch (error) {
if (error && error.code == 'USER_CANCELED_ACCOUNT_SELECTION') {
// user cancelled the account selection process...
return null
} else {
// should not happen as long as an activity is available...
throw error
}
}
}
},
};