Skip to main content

Helpers

import {
getGoogleSignInErrorCode,
getIdToken,
isSignInSuccess,
signInWithGoogle,
} from 'nitro-google-signin'

signInWithGoogle​

function signInWithGoogle(): Promise<GoogleSession | undefined>

Signs the user in with one call: restores a returning user through signIn, and shows presentExplicitSignIn only when there is no saved credential.

Resolves the session, or undefined when the user cancels. Rejects like the methods it calls. Call configure first.

const session = await signInWithGoogle()
if (session !== undefined) await sendToBackend(session.idToken)

getIdToken​

function getIdToken(): Promise<string | undefined>

Returns an ID token of the signed-in user, valid for at least five more minutes, to send to your backend. Resolves undefined when no user is signed in (SIGN_IN_REQUIRED); rejects like getTokens otherwise.

const idToken = await getIdToken()
const headers = idToken === undefined ? {} : { Authorization: `Bearer ${idToken}` }

isSignInSuccess​

function isSignInSuccess(
result: SignInResult,
): result is SignInResult & { type: 'success'; session: GoogleSession }

Narrows a SignInResult to a successful sign-in with its session.

const result = await GoogleSignIn.presentExplicitSignIn()
if (isSignInSuccess(result)) console.log(result.session.user.email)

getGoogleSignInErrorCode​

function getGoogleSignInErrorCode(error: unknown): GoogleSignInErrorCode | undefined

Returns the GoogleSignInErrorCode of an error thrown or rejected by GoogleSignIn, or undefined for any other value, including non-Error values and errors without a known [CODE] tag.

try {
await GoogleSignIn.getTokens()
} catch (error) {
if (getGoogleSignInErrorCode(error) === 'SIGN_IN_REQUIRED') showSignIn()
else throw error
}