UNPKG

4.62 kBJavaScriptView Raw
1import { UnavailabilityError } from '@unimodules/core';
2import Constants from 'expo-constants';
3import invariant from 'invariant';
4import ExpoGoogleSignIn from './ExpoGoogleSignIn';
5import GoogleUser from './GoogleUser';
6export const { ERRORS, SCOPES, TYPES } = ExpoGoogleSignIn;
7const DEFAULT_SCOPES = [SCOPES.PROFILE, SCOPES.EMAIL];
8let _initialization;
9let _options;
10let _currentUser = null;
11let _isClientUsageEnabled = false;
12function setCurrentUser(currentUser) {
13 _currentUser = currentUser;
14 return _currentUser;
15}
16function 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}
30function 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}
33async function ensureGoogleIsInitializedAsync(options) {
34 if (_initialization == null) {
35 return initAsync(options);
36 }
37 return _initialization;
38}
39async 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}
48export function allowInClient() {
49 _isClientUsageEnabled = true;
50}
51export function getCurrentUser() {
52 return _currentUser;
53}
54export async function askForPlayServicesAsync() {
55 return await getPlayServiceAvailability(true);
56}
57export 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}
66export 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}
78export async function isSignedInAsync() {
79 const user = await getCurrentUserAsync();
80 return user != null;
81}
82export async function isConnectedAsync() {
83 return await ExpoGoogleSignIn.isConnectedAsync();
84}
85export 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 /* Return null to create parity with Android */
94 if (error.code === ERRORS.SIGN_IN_REQUIRED) {
95 return null;
96 }
97 throw error;
98 }
99 }
100 return null;
101}
102export 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}
114export async function signOutAsync() {
115 await invokeAuthMethod('signOutAsync');
116}
117export async function disconnectAsync() {
118 await invokeAuthMethod('disconnectAsync');
119}
120export async function getCurrentUserAsync() {
121 return await invokeAuthMethod('getCurrentUserAsync');
122}
123export 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}
130export { default as GoogleAuthData } from './GoogleAuthData';
131export { default as GoogleAuthentication } from './GoogleAuthentication';
132export { default as GoogleIdentity } from './GoogleIdentity';
133export { default as GoogleUser } from './GoogleUser';
134//# sourceMappingURL=GoogleSignIn.js.map
\No newline at end of file