Created
July 10, 2018 22:00
-
-
Save thomasw/0590e727f8cdef46f67efbf55a1a3ce8 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { GoogleSignin as GS } from 'react-native-google-signin' | |
import configuration from '../configuration_public.json' | |
async function checkForPlayServices() { | |
let hasPlayServices = false | |
try { | |
hasPlayServices = await GS.hasPlayServices({autoResolve: true}) | |
} catch(e) { | |
console.warn('Play service availability check failed', e) | |
} | |
console.debug(`Play services availaible: ${hasPlayServices}`) | |
return hasPlayServices | |
} | |
async function configure(configOverrides={}) { | |
await checkForPlayServices() | |
const googleSSOConfig = { | |
...configuration.common.google.sso_config, | |
webClientId: configuration.common.google.web.id | |
} | |
return await GS.configure({...googleSSOConfig, ...configOverrides}) | |
} | |
export async function login(configuration={}) { | |
let signInAborted = false, currentUser | |
await configure(configuration) | |
try { | |
currentUser = await GS.currentUserAsync() || await GS.signIn() | |
} catch(e) { | |
signInAborted = e.code == 12501 || e.code == -5 | |
if (!signInAborted) { | |
throw e | |
} | |
} | |
signInAborted && console.log('Google Sign In aborted') | |
return currentUser | |
} | |
export async function logout() { | |
await configure() | |
if (!await GS.currentUserAsync()) return true | |
return await GS.signOut() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment