I’m creating app with React Native and nodeJS. I already have my auth system. Now I try to implement Google Sign In. So :
- Created a Google application in Firebase.
- Generated the .keystore file and obtained the SHA1 key.
- Updated the build.gradle to install the SDK on the client.
- Placed the file in the client and the Google service file in my Node.js app.
- Implemented the code in React Native and Node.js.
- Retrieved the token from the client.
- Encountered an error while testing with Postman.
I get this error testing on Postman :
Message : “Firebase ID token has incorrect “aud” (audience) claim.
Expected “tak-muscu” but got
“XXXX-ruib6t1s7lochabens3f3ep67pa411nc.apps.googleusercontent.com”.
Make sure the ID token comes from the same Firebase project as the
service account used to authenticate this SDK. See
https://firebase.google.com/docs/auth/admin/verify-id-tokens for
details on how to retrieve an ID token.”
I get the token from my client :
GoogleSignin.configure({
webClientId: 'XXXXX-ruib6t1s7lochabens3f3ep67pa411nc.apps.googleusercontent.com',
androidClientId: 'XXXXX-fvv7nndd0q53hvoht9cldt82jm5a9306.apps.googleusercontent.com',
scopes: ['profile', 'email']
});
const signIn = async () => {
try {
await GoogleSignin.hasPlayServices();
const userInfo = await GoogleSignin.signIn();
const idToken = userInfo.idToken;
console.log('ID Token:', idToken);
} catch (error) {
console.error('Google Sign-In error', error.message);
}
};
I Pass it to the route with Postman :
router.post('/google-signin', async (req, res) => {
const { idToken } = req.body;
const result = await verifyGoogleToken(idToken);
if (result.status === 'success') {
res.send({ message: 'Authentication successful', user: result.decodedToken });
} else {
res.status(401).send({ message: 'Authentication failed', error: result.message });
}
});
Function verifyGoogleToken :
const admin = require('firebase-admin');
const verifyGoogleToken = async (idToken) => {
try {
const decodedToken = await firebase.auth().verifyIdToken(idToken);
console.log("Token validé avec succès", decodedToken);
return { status: 'success', uid: decodedToken.uid };
} catch (error) {
console.error('Erreur lors de la vérification du token', error);
return { status: 'error', message: error.message };
}
};
module.exports = { verifyGoogleToken };
App.js server :
const admin = require('firebase-admin');
const serviceAccount = require('./secrets/service-account-file.json');
admin.initializeApp({
credential: admin.credential.cert(serviceAccount)
});
I tried it 4 times… and I get the same error every time