I’m trying to create google sso popup flow in my react/Nextjs web app.
However, by following the guidelines by Google with the Google Identity Services for Web, all I can I use is a bare minimum branded button provided by Google:
QUESTION: I wonder how can I use my own custom button to trigger the same popup for selecting account?
I’m welcome to any other options if not deprecated.
2
Answers
I found a solution for myself:
use
initTokenClient
andrequestAccessToken
to retrieve access token with the prompt.https://developers.google.com/identity/oauth2/web/guides/use-token-model#how_to_handle_consent
And then retrieve email (usually in server) by requesting google api
'https://www.googleapis.com/oauth2/v1/userinfo'
with the bearer access token .You can use Google OAuth2.0 url for your custom button.
First, get a client_id for your web application- https://console.cloud.google.com
Replace window url on button click.
There are many ways to do that, you can use window.location.replace(‘your_google_auth_url’)
Above is the oauth2 url. You need to provide client_id, redirect_url and scope you need.
Scope is a permission, if you need users email info you need to add
https://www.googleapis.com/auth/userinfo.email
this scope, etc.You can find all the scopes here: https://developers.google.com/identity/protocols/oauth2/scopes
Example: Google OAuth2 API v2