Skip to main content

Scopes and server auth codes

Sign-in always grants openid, email and profile. Ask for more scopes up front with configure({ scopes }), or later, when the user reaches a feature that needs them, with requestScopes.

import { getGoogleSignInErrorCode, GoogleSignIn } from 'nitro-google-signin'

try {
const { accessToken, scopes } = await GoogleSignIn.requestScopes([
'https://www.googleapis.com/auth/drive.readonly',
])
} catch (error) {
if (getGoogleSignInErrorCode(error) === 'SIGN_IN_CANCELLED') {
// The user declined consent.
}
}

requestScopes shows consent only when needed and resolves an AuthorizationResult: an access token, every scope now granted and, with requestOfflineAccess, a server auth code.

Check GoogleSession.scopes first to skip the call when the scope is already granted:

const drive = 'https://www.googleapis.com/auth/drive.readonly'
const granted = GoogleSignIn.getCurrentUser()?.scopes.includes(drive) ?? false
RejectionWhen
SIGN_IN_REQUIREDNo user is signed in.
SIGN_IN_CANCELLEDThe user declined the consent screen.
IN_PROGRESSAnother sign-in or consent flow runs.

Server auth codes​

Enable requestOfflineAccess to receive a one-time serverAuthCode your backend exchanges for a refresh token with the Web application client:

GoogleSignIn.configure({ requestOfflineAccess: true })

const result = await GoogleSignIn.presentExplicitSignIn()
if (isSignInSuccess(result) && result.session.serverAuthCode !== undefined) {
await exchangeOnBackend(result.session.serverAuthCode)
}

Server auth codes are one-time. They are returned only by interactive sign-in and requestScopes, never stored, and never returned by getCurrentUser().

Workspace domains​

hostedDomain restricts sign-in to one Google Workspace domain:

GoogleSignIn.configure({ hostedDomain: 'example.com' })

It filters the account picker on Android and is passed to Google on iOS. Always verify the hd claim of the ID token on your backend as well.