Auth0 Lock 中的用户注册事件 [英] User signup event in Auth0 Lock
问题描述
'authenticated'
事件在认证成功后发出.
The 'authenticated'
event is emitted after a successful authentication.
lock.on('authenticated', function(authResult) { });
但是有没有办法检测新用户何时注册您的应用程序,或者我是否必须将用户存储在我的数据库中并在每次用户进行身份验证时进行检查?
But is there any way to detect when a new user signs up to your application or do I have to store the user in my database and check it each time a user authenticates?
推荐答案
Auth0 Lock 不会触发用户注册的特定事件.
The Auth0 Lock does not trigger a specific event for user signup.
但是,您可以在自定义规则 上检测到这一点,并使用此元数据丰富用户配置文件.有一个注册示例规则说明了这种可能性
You can however detect this on a custom rule and enrich the user profile with this metadata. There's a signup sample rule that illustrates this possibility
function (user, context, callback) {
user.app_metadata = user.app_metadata || {};
// short-circuit if the user signed up already
if (user.app_metadata.signed_up) return callback(null, user, context);
// execute first time login/signup logic here
// ...
// update application metadata so that signup logic is skipped on subsequent logins
user.app_metadata.signed_up = true;
auth0.users.updateAppMetadata(user.user_id, user.app_metadata)
.then(function () {
callback(null, user, context);
})
.catch(function (err) {
callback(err);
});
}
这使用 app_metadata
来存储与用户关联的信息,以便您可以跟踪已执行其附加首次注册逻辑的用户.
This uses app_metadata
to store information associated to the user so that you can keep track for which users you already executed their additional first-time signup logic.
请记住,规则将在身份验证管道的服务器端执行,因此如果您要实现的逻辑需要用户交互,您可以通过执行以下步骤来实现类似的效果:
Have in mind that rules will execute on the server-side of the authentication pipeline, so if the logic you want to implement requires user interaction you could achieve something similar by doing these set of steps:
- 登录应用程序后获取用户个人资料
- 如果没有设置标志,则假设用户刚刚注册并执行您的自定义逻辑
- 完成自定义逻辑后,更新用户
app_metadata
以设置注册标志(您可以通过 Auth0 管理 API)
- Upon login to the application get the user profile
- If there's no flag set assume the user just signed up and do your custom logic
- After doing your custom logic update the user
app_metadata
to set a signup flag (you can do this on your server-side application logic through Auth0 Management API)
这篇关于Auth0 Lock 中的用户注册事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!