首页 > 解决方案 > How to get Google UserId from active user session in App Maker?

问题描述

Is there a way to get "User Google Id" from the session in App Maker. In the documentation its only mentioned how to retrieve the email of the logged in user Session.getActiveUser().getEmail() but no where it says how to get the id. I need this because the user email might sometimes changes. So I need the user id to keep track of users and related permission tasks. Or is there something I'm missing out here in how this should be implemented.

标签: google-app-maker

解决方案


然而,一种更简单的方法是使用Directory 模型来查找 Google Id 。虽然它在文档中提到有一种方法可以获取当前登录的用户 ID(即 Google ID),但它没有明确说明如何 - 也许可以在此处改进文档。另一个问题是,在许多情况下,当前活动用户的电子邮件被称为 id,例如在不推荐使用的方法Session.getActiveUser().getUserLoginId()中。无论如何,这是获取 id 的正确方法。

var query = app.models.Directory.newQuery();
query.filters.PrimaryEmail._equals = Session.getActiveUser().getEmail();
var result = query.run();
var GoogleId = result[0]._key;

因此,使用此 GoogleId,您可以安全地将不同的模型相互关联,而不必担心如果已引用的用户电子邮件发生更改,数据库完整性可能会中断。

可以简单地通过创建一个模型来关联不同的模型,该模型充当目录模型的包装模型并将 GoogleId 存储在其中。然后将该模型链接到您想要跟踪用户相关数据的其他模型,因为不幸的是我们不能直接将目录模型链接到其他模型。


推荐阅读