Search code examples
actions-on-googlegoogle-home

Google home check sign in status before asking the user for sign in


I have been developing actions on google home. When implementing the helper intents to access user's name and email all those stuff implemented sign in helper intent. Now how to check if the user is already signed in without asking for sign in.

const {SignIn} = require('actions-on-google');

module.exports = {

  'ask_for_sign_in': (conv) => {
    conv.ask(new SignIn());
  },

  'ask_for_sign_in_confirmation': (conv, params, signin) => {
    if (signin.status !== 'OK') {
      return conv.ask('You need to sign in before using the app.');
    }
    // const access = conv.user.access.token;
    // possibly do something with access token
    return conv.ask('Great! Thanks for signing in.');
  },
};

Here

conv.ask(new SignIn());

will trigger the signIn helper intent. How to check if user has already signed in before asking for sign In again. I have the same question for

permission helper intent

Instead of asking permission every time when the user invokes the action, how to check if the user already granted the permission.


Solution

  • It depends which of the sign-in backends you're using, but for all of the subsequent requests (both in this session and the next), one of the two following will be true:

    • If you're using your own OAuth servers, then you'll receive an auth token which you can receive in conv.user.access.token. You can use your OAuth server's procedures to verify it and determine the user based on this token.

    • If you're using Google Sign-In for the Assistant, then you'll receive an id token (in conv.user.profile.token) and the profile information for the user (including their Google ID) in conv.user.profile.payload.

    The same is not true of permissions. You need to request permission for name and location each time. However, if you use their name, you can store this in userStorage and it will be retained in the future. Location, however, may change if they're answering on a mobile device, so you need to request this each time.