1 | import { UnavailabilityError } from '@unimodules/core';
|
2 | import Constants from 'expo-constants';
|
3 | import invariant from 'invariant';
|
4 | import ExpoGoogleSignIn from './ExpoGoogleSignIn';
|
5 | import GoogleUser from './GoogleUser';
|
6 | export const { ERRORS, SCOPES, TYPES } = ExpoGoogleSignIn;
|
7 | const DEFAULT_SCOPES = [SCOPES.PROFILE, SCOPES.EMAIL];
|
8 | let _initialization;
|
9 | let _options;
|
10 | let _currentUser = null;
|
11 | let _isClientUsageEnabled = false;
|
12 | function setCurrentUser(currentUser) {
|
13 | _currentUser = currentUser;
|
14 | return _currentUser;
|
15 | }
|
16 | function validateOptions(options) {
|
17 | if (!options) {
|
18 | return {
|
19 | scopes: DEFAULT_SCOPES,
|
20 | };
|
21 | }
|
22 | if (options.isOfflineEnabled) {
|
23 | invariant(typeof options.webClientId === 'string' && options.webClientId !== '', 'GoogleSignIn: Offline access (isOfflineEnabled: true) requires a valid google server id `webClientId`');
|
24 | }
|
25 | return {
|
26 | ...options,
|
27 | scopes: options.scopes || DEFAULT_SCOPES,
|
28 | };
|
29 | }
|
30 | function validateOwnership() {
|
31 | invariant(_isClientUsageEnabled || Constants.appOwnership !== 'expo', 'expo-google-sign-in is not supported in the Expo Client because a custom URL scheme is required at build time. Please refer to the docs for usage outside of Expo www.npmjs.com/package/expo-google-sign-in');
|
32 | }
|
33 | async function ensureGoogleIsInitializedAsync(options) {
|
34 | if (_initialization == null) {
|
35 | return initAsync(options);
|
36 | }
|
37 | return _initialization;
|
38 | }
|
39 | async function invokeAuthMethod(method) {
|
40 | if (!ExpoGoogleSignIn[method]) {
|
41 | throw new UnavailabilityError('GoogleSignIn', method);
|
42 | }
|
43 | await ensureGoogleIsInitializedAsync();
|
44 | const payload = await ExpoGoogleSignIn[method]();
|
45 | const account = payload != null ? new GoogleUser(payload) : null;
|
46 | return setCurrentUser(account);
|
47 | }
|
48 | export function allowInClient() {
|
49 | _isClientUsageEnabled = true;
|
50 | }
|
51 | export function getCurrentUser() {
|
52 | return _currentUser;
|
53 | }
|
54 | export async function askForPlayServicesAsync() {
|
55 | return await getPlayServiceAvailability(true);
|
56 | }
|
57 | export async function getPlayServiceAvailability(shouldAsk = false) {
|
58 | validateOwnership();
|
59 | if (ExpoGoogleSignIn.arePlayServicesAvailableAsync) {
|
60 | return await ExpoGoogleSignIn.arePlayServicesAvailableAsync(shouldAsk);
|
61 | }
|
62 | else {
|
63 | return true;
|
64 | }
|
65 | }
|
66 | export async function initAsync(options) {
|
67 | if (!ExpoGoogleSignIn.initAsync) {
|
68 | throw new UnavailabilityError('GoogleSignIn', 'initAsync');
|
69 | }
|
70 | _options = validateOptions(options || _options || {});
|
71 | const hasPlayServices = await getPlayServiceAvailability();
|
72 | if (!hasPlayServices) {
|
73 | return;
|
74 | }
|
75 | _initialization = ExpoGoogleSignIn.initAsync(_options || {});
|
76 | return _initialization;
|
77 | }
|
78 | export async function isSignedInAsync() {
|
79 | const user = await getCurrentUserAsync();
|
80 | return user != null;
|
81 | }
|
82 | export async function isConnectedAsync() {
|
83 | return await ExpoGoogleSignIn.isConnectedAsync();
|
84 | }
|
85 | export async function signInSilentlyAsync() {
|
86 | const isConnected = await isConnectedAsync();
|
87 | if (isConnected) {
|
88 | try {
|
89 | const auth = await invokeAuthMethod('signInSilentlyAsync');
|
90 | return auth;
|
91 | }
|
92 | catch (error) {
|
93 |
|
94 | if (error.code === ERRORS.SIGN_IN_REQUIRED) {
|
95 | return null;
|
96 | }
|
97 | throw error;
|
98 | }
|
99 | }
|
100 | return null;
|
101 | }
|
102 | export async function signInAsync() {
|
103 | try {
|
104 | const user = await invokeAuthMethod('signInAsync');
|
105 | return { type: 'success', user };
|
106 | }
|
107 | catch (error) {
|
108 | if (error.code === ERRORS.SIGN_IN_CANCELLED) {
|
109 | return { type: 'cancel', user: null };
|
110 | }
|
111 | throw error;
|
112 | }
|
113 | }
|
114 | export async function signOutAsync() {
|
115 | await invokeAuthMethod('signOutAsync');
|
116 | }
|
117 | export async function disconnectAsync() {
|
118 | await invokeAuthMethod('disconnectAsync');
|
119 | }
|
120 | export async function getCurrentUserAsync() {
|
121 | return await invokeAuthMethod('getCurrentUserAsync');
|
122 | }
|
123 | export async function getPhotoAsync(size = 128) {
|
124 | if (!ExpoGoogleSignIn.getPhotoAsync) {
|
125 | throw new UnavailabilityError('GoogleSignIn', 'getPhotoAsync');
|
126 | }
|
127 | await ensureGoogleIsInitializedAsync();
|
128 | return await ExpoGoogleSignIn.getPhotoAsync(size);
|
129 | }
|
130 | export { default as GoogleAuthData } from './GoogleAuthData';
|
131 | export { default as GoogleAuthentication } from './GoogleAuthentication';
|
132 | export { default as GoogleIdentity } from './GoogleIdentity';
|
133 | export { default as GoogleUser } from './GoogleUser';
|
134 |
|
\ | No newline at end of file |