0
votes

I have a deployed Action that has Google Sign-in Account Linking enabled. This Action uses a cloud function as fullfilment. We extract the user from the DialogFlow call using this method:

function userFromRequest(request) {
  return request.body.originalDetectIntentRequest.payload.user;
}

This function returns this user data:

   { 
     "idToken": "eyJhbGciOiJSU...",
     "lastSeen": "2018-11-29T16:58:22Z",
     "locale": "en-US",
     "userId": "ABwpp..."
   }

My question is: how can I get the user information such as email, name, etc, from outside the DialogFlow app.

All the documentation examples have a conv object available:

app.intent('Default Welcome Intent', async (conv) => {
  const {payload} = conv.user.profile;
  const name = payload ? ` ${payload.given_name}` : '';
}

In our case, we want to simply take the userId or idToken and retrieve the user info. It could be something like this:

const dialogflow = require("actions-on-google");    
const app = dialogflow({clientId: '[email protected]'});

app.getUserData(idToken); //this does not exists, how to have something equivalent?
2

2 Answers

1
votes

The idToken is just a normal JWT (JSON Web Token) that has been signed by one of Google's keys (which rotate very frequently). Although you should verify the signature, you don't need to.

You can use any JWT library. Since it looks like you're using node.js, you can use something like the jsonwebtoken package to decode it with something like this:

const jwt = require('jsonwebtoken');
// get the decoded payload ignoring signature, no secretOrPrivateKey needed
const decoded = jwt.decode(token);

You really should verify the signature, however, so you'll need to get the keys in a format that is useful. The part of the multivocal library that does this uses the JWK version of the keys from Google and converts them into PEM format to verify.

1
votes

you can use "google-auth-library" to verify the token and get the payload. here is the link to the documentation