1 | import { Constants } from 'expo-constants';
|
2 | import { NativeModules, Platform } from 'react-native';
|
3 | const Google = NativeModules.ExponentGoogle;
|
4 | export async function logInAsync(config) {
|
5 | let behavior = config.behavior;
|
6 | if (!behavior) {
|
7 | behavior = 'system';
|
8 | }
|
9 |
|
10 | if (Constants.appOwnership !== 'standalone' && (behavior === 'system' && Platform.OS === "android")) {
|
11 | behavior = 'web';
|
12 | }
|
13 | let scopes = config.scopes;
|
14 | if (!scopes) {
|
15 | scopes = ['profile', 'email'];
|
16 | }
|
17 | const androidClientId = Constants.appOwnership === 'standalone'
|
18 | ? config.androidStandaloneAppClientId
|
19 | : config.androidClientId;
|
20 | const iosClientId = Constants.appOwnership === 'standalone' ? config.iosStandaloneAppClientId : config.iosClientId;
|
21 | const logInResult = await Google.logInAsync({
|
22 | androidClientId: androidClientId || config.clientId,
|
23 | iosClientId: iosClientId || config.clientId,
|
24 | webClientId: config.webClientId,
|
25 | behavior,
|
26 | scopes,
|
27 | });
|
28 | if (behavior === 'web') {
|
29 |
|
30 |
|
31 | let userInfoResponse = await fetch('https://www.googleapis.com/userinfo/v2/me', {
|
32 | headers: { Authorization: `Bearer ${logInResult.accessToken}` },
|
33 | });
|
34 | let userInfo = await userInfoResponse.json();
|
35 | return {
|
36 | ...logInResult,
|
37 | user: {
|
38 | id: userInfo.id,
|
39 | name: userInfo.name,
|
40 | givenName: userInfo.given_name,
|
41 | familyName: userInfo.family_name,
|
42 | photoUrl: userInfo.picture,
|
43 | email: userInfo.email,
|
44 | },
|
45 | };
|
46 | }
|
47 | else {
|
48 | return logInResult;
|
49 | }
|
50 | }
|
51 |
|
\ | No newline at end of file |