简体   繁体   English

在Google的Actions中使用Express Middleware

[英]Using Express Middleware in Actions on Google

As mentioned in other newbie question ( Google Assistant - Account linking with Google Sign-In ) I have an Express app which supports Google authentication and authorization via Passport and now with the help of @prisoner my Google Action (which runs off the same Express app) supports Google login in this way https://developers.google.com/actions/identity/google-sign-in . 正如其他新手问题( Google助手-与Google登录帐户关联)中提到的那样,我有一个Express应用程序,该应用程序支持通过Passport进行Google身份验证和授权,现在借助@prisoner我的Google Action(该应用程序运行在同一Express应用程序上) )以这种方式支持Google登录https://developers.google.com/actions/identity/google-sign-in

My question now is how can I use the varous middlewares that my Express app has as part of the Google Assistant intent fullfillments? 我现在的问题是,如何将我的Express应用拥有的各种中间件用作Google Assistant意向实现的一部分? A couple of examples: 几个例子:

1) I have an intent 1)我有意图

// Handle the Dialogflow intent named 'ask_for_sign_in_confirmation'.
gapp.intent('Get Signin', (conv, params, signin) => {
  if (signin.status !== 'OK') {
    return conv.ask('You need to sign in before using the app.');
  }
  const payload = conv.user.profile.payload
  console.log(payload);
  conv.ask(`I got your account details, ${payload.name}. What do you want to do next?`)
});

Now just because the user is signed in to Google in my action presumably doesn't mean that they have authenticated (via the Google Passport strategy) into my Express app generally? 现在仅仅是因为用户大概是按照我的操作登录到Google的,并不意味着他们通常已经(通过Google Passport策略)通过了我的Express应用程序的身份验证? However from the above I do have access to payload.email which would enable me to use my site Google login function 但是从上面我确实可以访问payload.email,这使我可以使用自己的网站Google登录功能

passportGoogle.authenticate('google', 
    { scope: ['profile', 'email'] }));'  

which essentially uses Mongoose to look for a user with the same details 实际上使用Mongoose查找具有相同详细信息的用户

User.findOne({ 'google.id': profile.id }, function(err, user) {
    if (err)
    return done(err);

    // if the user is found, then log them in
    if (user) {
      return done(null, user);
     ....

ok, I would need to modify it to check the value of payload.email against google.email in my DB. 好的,我需要对其进行修改,以针对我数据库中的google.email检查payload.email的值。 But how do I associate this functionality from the Express app into the intent fullfillment? 但是,如何将Express应用程序中的此功能关联到意图填充中?

2) Given the above Get Signin intent how could I exectute an Express middleware just to console.log('hello world') for now? 2)鉴于以上“获取登录”的意图,我现在如何才能仅将console.log('hello world')扩展为Express中间件? For example: 例如:

gapp.intent('Get Signin', (conv, params, signin) => {
  if (signin.status !== 'OK') {
    return conv.ask('You need to sign in before using the app.');
  }
  authController.assistantTest;
  const payload = conv.user.profile.payload
  console.log(payload);
  conv.ask(`I got your account details, ${payload.name}. What do you want to do next?`)
});

Here authController.assistantTest; 这里authController.assistantTest; is

exports.assistantTest = (req, res) => {
    console.log('hello world');
};

Any help / links to docs really appreciated! 任何帮助/文档链接真的很感激!

It looks like you're trying to add a piece of functionality that runs before your intent handler. 看来您正在尝试添加要在意图处理程序之前运行的功能。 In your case, it's comparing user's email obtained via Sign In versus what's stored in your database. 在您的情况下,它会将通过登录获取的用户电子邮件与数据库中存储的电子邮件进行比较。

This is a good use case for a middleware from Node.js client library (scroll down to "Scaling with plugins and middleware " section). 对于Node.js客户端库中的中间件来说 ,这是一个很好的用例(向下滚动至“使用插件和中间件缩放”部分)。 The middleware layer consists of a function you define that the client library automatically runs before the IntentHandler. 中间件层由一个函数定义,该函数定义客户端库在IntentHandler之前自动运行。 Using a middleware layer lets you modify the Conversation instance and add additional functionality. 使用中间件层可让您修改“对话”实例并添加其他功能。

Applying this to your example gives: 将其应用于您的示例将得到:

gapp.middleware(conv => {
  // will print hello world before running the intent handler
  console.log('hello world');
});

gapp.intent('Get Signin', (conv, params, signin) => {
  if (signin.status !== 'OK') {
    return conv.ask('You need to sign in before using the app.');
  }
  authController.assistantTest;
  const payload = conv.user.profile.payload
  console.log(payload);
  conv.ask(`I got your account details, ${payload.name}. What do you want to do next?`)
});

You could perform the authentication logic in the middleware, and potentially utilize conv.data by keeping track if user's email matched records from your database. 您可以在中间件中执行身份验证逻辑,并通过跟踪用户的电子邮件是否匹配数据库中的记录来潜在地利用conv.data数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM