I’ve implemented Google authentication in my mobile app and everything works fine on the frontend side. The login process completes successfully and I get the ID token like this:
var userAccount = await googleAuthService.authenticate();
var credentials = await userAccount.authData;
var idToken = credentials.token;
The token gets sent to my Node.js backend via HTTP request without any issues. However, when I try to validate this token on the server side, I keep running into problems. My backend validation code looks something like this:
let serviceConfig = require('./google-config.json');
let GoogleAuth = require('google-auth-library').OAuth2Client;
let authClient = new GoogleAuth(serviceConfig.client[0].oauth_client[0].client_id);
let validationResult = await authClient.verifyIdToken({
idToken: idToken,
audience: serviceConfig.client[0].oauth_client[0].client_id
});
let userInfo = validationResult.getPayload();
The verification always fails with the error message saying “Wrong recipient, payload audience != requiredAudience”. I’m using the same google-services.json configuration file on both the mobile app and the backend server.
I even tried creating new credentials through the Google Cloud Platform console and using those client IDs instead, but I get the exact same audience mismatch error. What’s the correct way to find the right client ID that matches the token’s audience for proper verification?