Google Sign-In JS Bridge
The Google Sign-In bridge lets your website open the native Google account picker on the device and receive the signed-in user's profile and a Google ID token back. Your backend verifies that token to log the user in. It works the same way on Android and iOS.
The app does not take over your website's Google login automatically — inside the app, your "Sign in with Google" button must trigger this bridge.
For a general introduction to the JavaScript Bridge, see the Overview article.
Prerequisites
Before the bridge can trigger Google Sign-In, configure the Google Sign-In module in your Appilix dashboard:
- Go to your app's Integration Modules and enable Google Sign-In.
- Enter the Android Client ID and/or iOS Client ID generated in Google Cloud Console for your app.
- Enter the Web App Client ID — your website's Web application OAuth Client ID from the same Google Cloud project.
- Rebuild your app so the new settings are included.
See the Google Sign-In article for how to create these Client IDs.
How It Works
- The user taps your website's "Sign in with Google" button inside the app.
- Your website sends a
google_sign_inmessage to the app via the bridge. - The app opens the native Google account picker and the user selects an account.
- The app sends the user's profile and ID token back to your website.
- Your website sends the ID token to its backend, which verifies it and logs the user in.
Triggering Sign-In
appilix.postMessage(JSON.stringify({
type: "google_sign_in",
props: {
response_type: "code"
}
}));props is optional — { type: "google_sign_in" } on its own is a valid request.
Parameters
| Parameter | Required | Description |
|---|---|---|
response_type | No | Set to "code" to also receive a server auth code your backend can exchange for Google access tokens. Leave it out if the ID token is enough to log the user in |
The Web App Client ID and scopes always come from the Google Sign-In module settings and cannot be set from the bridge. Scopes default to email profile when the Scopes field is empty.
Handling the Response
Set up the listener before triggering sign-in:
appilix.onmessage = function(event) {
const data = JSON.parse(event.data);
if (!data || data.type !== "google_sign_in") return;
if (!data.response.error) {
// Send the ID token to your backend to verify it and log the user in
fetch("/auth/google/app", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ id_token: data.response.id_token })
});
} else {
// Sign-in failed or was cancelled
console.log("Sign-in failed:", data.response.error);
}
appilix.onmessage = null;
};Response Fields
| Field | Description |
|---|---|
error | null on success, otherwise the reason sign-in failed (e.g. the user closed the account picker) |
email | User's Google account email address |
name | User's display name. Empty string if the account has none |
picture | URL of the user's Google profile photo. Empty string if the account has none |
id | Stable Google account ID — the same value as the sub claim in the ID token |
id_token | Signed Google ID token (JWT) issued for your Web App Client ID. Always present on success |
code | Server auth code, only present when response_type is "code". Otherwise null |
On failure, every field except error is null, so the response always has the same shape.
Verifying the ID Token on Your Backend
The email, name, and other fields are returned for convenience — they are not proof of identity on their own. Before logging the user in, your backend must verify id_token and check that:
- The token's signature is valid against Google's public keys.
audequals your Web App Client ID.issisaccounts.google.comorhttps://accounts.google.com.exphas not passed.
Google's official libraries perform all of these checks for you.
Node.js (google-auth-library):
const { OAuth2Client } = require("google-auth-library");
const WEB_CLIENT_ID = "YOUR_WEB_CLIENT_ID";
const client = new OAuth2Client(WEB_CLIENT_ID);
async function verifyGoogleIdToken(idToken) {
const ticket = await client.verifyIdToken({ idToken, audience: WEB_CLIENT_ID });
const payload = ticket.getPayload();
return { google_id: payload.sub, email: payload.email, name: payload.name };
}PHP (google/apiclient):
$client = new Google_Client(['client_id' => 'YOUR_WEB_CLIENT_ID']);
$payload = $client->verifyIdToken($id_token);
if ($payload) {
$google_id = $payload['sub'];
$email = $payload['email'];
} else {
// Invalid ID token — do not log the user in
}Identify the user by sub (the same value as id) rather than by email — it never changes, while a user's email can.
Exchanging the Auth Code
Only needed when you send response_type: "code" and your backend calls Google APIs on the user's behalf. Requesting a code may show the user an extra consent screen for the configured scopes.
From your backend, send a POST request to https://oauth2.googleapis.com/token with:
| Parameter | Value |
|---|---|
code | The code from the response |
client_id | Your Web App Client ID |
client_secret | Your Web App Client Secret |
grant_type | authorization_code |
redirect_uri | An empty string — the code was issued to the app, not through a browser redirect |
Google responds with an access token for the requested scopes. Auth codes are single-use and expire quickly, so exchange them as soon as your backend receives them.
Complete Example
function signInWithGoogle() {
// In a regular browser, keep your existing web Google login
if (typeof appilix === 'undefined') {
startWebGoogleLogin();
return;
}
appilix.onmessage = function(event) {
const data = JSON.parse(event.data);
if (!data || data.type !== "google_sign_in") return;
appilix.onmessage = null;
if (data.response.error) {
console.error("Sign-in error:", data.response.error);
return;
}
// Send the ID token to your backend for verification
fetch("/auth/google/app", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ id_token: data.response.id_token })
})
.then(res => res.json())
.then(result => {
if (result.success) window.location.href = "/dashboard";
});
};
appilix.postMessage(JSON.stringify({
type: "google_sign_in"
}));
}startWebGoogleLogin() stands for whatever your website already does to sign users in with Google in a browser.
Important Notes
- Always verify
id_tokenon your backend. Never log a user in based on the client-side response alone. - If the user closes the account picker,
erroris set and no token is returned. - On Android, sign-in fails for Play Store installs until the Play App Signing SHA-1 is added to your Android Client ID — see After Publishing to Google Play.
- Implementing this requires changing your website's sign-in button logic and adding a backend endpoint that verifies the token. Involve a developer if needed.