@hyperse-hub/hps-plugin-auth-google
Google OAuth authentication for Vendure Shop API.
Features
- Google OAuth sign-in / sign-up: uses Vendure
authenticate + google strategy (creates a Customer/User if not exists)
- Avatar sync: downloads Google
picture as a Vendure Asset and stores it in Customer.customFields.avatar
- Cross-strategy email conflict protection (optional): prevents the same email being used across different auth strategies
- Set native password: provides Shop GraphQL
setGooglePassword(email, password) to add a native auth method after Google login
Install
yarn add @hyperse-hub/hps-plugin-auth-google
Vendure configuration
Register the plugin in vendure-config.ts and configure Google OAuth credentials:
Options
- googleClientId: Google OAuth Client ID
- googleClientSecret: Client secret
- redirectUri: OAuth redirect URI (used when exchanging the authorization code for tokens)
- preventCrossStrategies: enforce unique email across all Shop auth strategies (default
false)
- If enabled, the login will be rejected when the Google email is already used by another strategy
Note: the strategy is only added to config.authOptions.shopAuthenticationStrategy when googleClientId is non-empty.
Google provider setup (OAuth client)
Create an OAuth 2.0 Client in Google Cloud Console and configure:
- Authorized redirect URIs: must include the configured
redirectUri
- Scopes: recommend at least
openid email profile
The plugin exchanges the authorization code for tokens and verifies the id_token (not the access token).
Shop API usage
1) Obtain the Google code in your frontend
Let your frontend complete the Google OAuth flow and receive the authorization code.
2) Call Vendure authenticate (strategy: google)
Send the code to Vendure authenticate (this is the input shape used in this repo's e2e tests):
Note: the input type is defined by the strategy defineInputType() (GoogleAuthInput: code).
3) (Optional) Set a native password: setGooglePassword
After the user signs in with Google (must be authenticated), you can call:
- This mutation requires authentication (
Permission.Authenticated)
- It adds a native authentication method to the current user, enabling email+password login
email must match the current user's identifier, otherwise it throws Unauthorized
Data model change: Customer.customFields.avatar
The plugin ensures Customer.customFields.avatar exists (relation to Asset) on bootstrap:
- Field name:
avatar
- Type: relation ->
Asset
- Readonly:
readonly: true
When a user is created via Google for the first time, the plugin tries to download the avatar and store it in this field.
FAQ
What is redirectUri and why must it match exactly?
When exchanging the authorization code, Google validates redirect_uri. It must exactly match the Authorized redirect URIs in Google Console and the plugin redirectUri.
What does preventCrossStrategies check?
It checks whether the Google-provided email is already associated with a user via any other Shop auth strategy. If so, it rejects the Google login to avoid one email being used across multiple strategies.