In my react app I retrieve an authorization token from google and send it to my back end to verify it. I followed to achieve this, but I need to use the data I get in the callback outside of the callback:
const clientId = '<MyAppId>';
var auth = new GoogleAuth;
var client = new auth.OAuth2(clientId, '', '');
client.verifyIdToken(
input.googleAuthToken,
clientId,
function (e, login){
if (login) {
var payload = login.getPayload();
var googleId = payload['sub'];
console.log(googleId); //correct id is logged
}
}
);
//I need the 'googleId' here
So this is working, but I need the googleId outside of the callback to check it with my database. How would I achieve this?
In my react app I retrieve an authorization token from google and send it to my back end to verify it. I followed https://developers.google./identity/sign-in/web/backend-auth to achieve this, but I need to use the data I get in the callback outside of the callback:
const clientId = '<MyAppId>';
var auth = new GoogleAuth;
var client = new auth.OAuth2(clientId, '', '');
client.verifyIdToken(
input.googleAuthToken,
clientId,
function (e, login){
if (login) {
var payload = login.getPayload();
var googleId = payload['sub'];
console.log(googleId); //correct id is logged
}
}
);
//I need the 'googleId' here
So this is working, but I need the googleId outside of the callback to check it with my database. How would I achieve this?
As you can see, client.verifyIdToken
is async, so there is no way to wait for the return value in javascript in pure synchronous way. But, you can write and execute async functions in a sync manner (but it is still asynchronous under the hood, and the JS execution continues) for instance with promises.
Consider a few changes to your code:
const verifyToken = new Promise(function(resolve, reject){
client.verifyIdToken(
input.googleAuthToken,
clientId,
function (e, login){
if (login) {
var payload = login.getPayload();
var googleId = payload['sub'];
resolve(googleId);
} else {
reject("invalid token");
}
}
)
}).then(function(googleId){
//use googleId here
}).catch(function(err){
//error
})
So we create a promise that take a function with two parameters: resolve, and reject. These are functions that you call when you get your value asynchronously. By doing that, you can use .then
syntax that takes a function that will be executed after the previous function in the chain resolves (with a value you specified, in this case googleId token). Read more about promises here
Go to Google Developer Console and generate an API key, then the verification made easy. (with "google-auth-library": "^1.1.0"
)
const { auth } = require('google-auth-library');
const client = auth.fromAPIKey('<YOUR_API_KEY>');
router.post('/', async (req, res) => {
const { idToken } = req.body; // idtoken from your react app
const res = await client.verifyIdToken({ idToken });
const { email, name, picture, sub: googleid } = res.getPayload();
const user = { email, name, picture, googleid };
// transform to your local user
// ...
res.send(authUser);
});
Don't forget to hande exceptions.
Hope this help.