简体   繁体   English

使用Auth0 Lock时如何管理后端用户?

[英]How to manage users on backend when using Auth0 Lock?

I am creating a frontend with React and a backend with Node. 我正在用React创建一个前端,并用Node创建一个后端。 I would like to manage user information using Auth0 Lock - sending a JWT with each request to the API. 我想使用Auth0 Lock管理用户信息-将每个请求的JWT发送到API。

What if I need to do one of the following things? 如果我需要执行以下操作之一怎么办?

  • Store a blog post with an author ID 存储具有作者ID的博客文章

The Auth0 unique identifier is user_id , which is not an integer and therefore cannot be used as an ID/key. Auth0唯一标识符是user_id ,它不是整数,因此不能用作ID /密钥。 How would I handle this on a server-side user store? 我将如何在服务器端用户存储上处理此问题?

  • Have a users table to store a "profile about" or other similar information 有一个用户表来存储“个人资料”或其他类似信息

Do I read the JWT on each API request, determine if that user exists, and then create a new user if it doesn't, or relate it to a pre-existing user if it does. 我是否在每个API请求中都读取了JWT,确定该用户是否存在,如果不存在,则创建一个新用户,如果存在,则将其与先前存在的用户相关联。 Is it performant to check the user database on every single API request? 在每个单个API请求中检查用户数据库是否有效?

I am unsure how to handle general flow with JWT-based API and Auth0. 我不确定如何使用基于JWT的API和Auth0处理一般流程。

Edit: 编辑:

My thoughts after some research and reflection: Auth0's unique user identifier is provided by them as user_id . 经过一些研究和思考,我的想法是:Auth0的唯一用户标识符由它们提供为user_id The problem here is that it is not an integer. 这里的问题是它不是整数。 Therefore it should not be used as the key for the users table in a database. 因此,不应将其用作数据库中users表的键。

It seems as if you shouldn't check the user database on each request, but this may be incorrect. 似乎您不应该在每个请求中都检查用户数据库,但这可能是不正确的。 One idea would be to callback to the backend on initial login, if the account doesn't exist, create it, if the account does exist, move on. 一种想法是在初始登录时回调到后端,如果该帐户不存在,则创建它,如果该帐户确实存在,则继续。 Then just trust the Auth0 JWT (if it verifies on the backend) on each following request after the user has logged in on the frontend. 然后,在用户登录到前端后,仅对以下每个请求信任Auth0 JWT(如果它在后端进行验证)。

From the very few descriptions of this process I have seen online, it seems like the way I described was the normal way. 从我在网上看到的对该过程的很少描述,看来我描述的方式是正常的方式。 But there are some situations where it doesn't make sense. 但是在某些情况下,这是没有意义的。 What if a user was to be banned? 如果要禁止用户怎么办? They could still access the server functionality with their active JWT until it expires due to time. 他们仍然可以使用活动的JWT来访问服务器功能,直到由于时间而到期。

So, if it is normal/performant to check the user store on each API request, how do I relate Auth0's string id, user_id , to an integer ID in the datastore to do queries? 因此,如果在每个API请求中检查用户存储是否正常/性能良好,如何将Auth0的字符串ID user_id与数据存储中的整数ID相关联以进行查询? I am using a SQL variant. 我正在使用SQL变体。

How to identify users 如何识别用户

You're not explicit about which database technology you use, but in general you should be able to use regular strings as identifiers/keys. 您不确定要使用哪种数据库技术,但通常您应该能够使用常规字符串作为标识符/键。 You do mention that you're using SQL variant so that may be the source of the issue; 您确实提到您使用的是SQL变体,所以这可能是问题的根源。 you should probably use a more specific text-based data type with a fixed length enough. 您可能应该使用足够固定长度的更特定的基于文本的数据类型。

The user_id is the result of concatenating the Auth0 identity provider identifier with the user identifier within that provider so we could argue that reaching a definitive max length is a little trickier. user_id是将Auth0身份提供程序标识符与该提供程序内的用户标识符串联在一起的结果,因此我们可以争辩说,确定的最大长度会有些棘手。 However, you can decide on arbitrary value, for example, something like 640 character ought to be enough for anyone. 但是,您可以决定任意值,例如,对于任何人来说,大约640个字符就足够了。

You can also identify your users by email; 您还可以通过电子邮件标识您的用户; this works if every authentication provider being used by your application requires users to provide their email and you also don't intend to support different accounts with the same email address. 如果您的应用程序使用的每个身份验证提供程序都要求用户提供其电子邮件,并且您也不想使用相同的电子邮件地址来支持不同的帐户,则此方法有效。

A final alternative is for you to assign each user your own unique identifier that is better suited for how you intend to use it. 最后一种选择是让您为每个用户分配自己的唯一标识符,该标识符更适合您打算如何使用它。 You can achieve this by having an Auth0 rule update your user metadata with this new attribute and then request this attribute to be included in the generated token upon user authentication by the means of scopes . 您可以通过使Auth0规则使用此新属性更新用户元数据 ,然后在通过范围进行用户身份验证时请求将此属性包括在生成的令牌中来实现此目的

Depending on the approach you would neither need a simple lookup table mapping one form of identifier to your internal one or in the case you update the user metadata with your internal identifier you could skip that lookup table entirely and just the value coming from the JWT. 根据方法的不同,您将不需要一个简单的查找表即可将一种形式的标识符映射到您的内部标识符,或者如果您用内部标识符更新用户元数据,则可以完全跳过该查找表,而只需跳过JWT的值即可。

How to handle first-time users 如何处理初次使用者

Like you mentioned, you could at each API request make sure that if this is the first request issued by a new user then you create your notion of application profile before processing the request. 就像您提到的那样,您可以在每个API请求中确保如果这是新用户发出的第一个请求,则可以在处理请求之前创建应用程序配置文件的概念。

The alternative to this would be triggering this application profile creation from within Auth0 when you detect that the user signup for the first time and then on the API always assume the profile exists. 替代方法是,当您首次检测到用户注册然后在API上始终假设该配置文件存在时,从Auth0内部触发此应用程序配置文件的创建。

Both approaches are valid; 两种方法都是有效的。 I would go with the one that would leave you with a simpler implementation and still meets your requirements. 我会选择一种可以使您实现更简单并且仍然满足您要求的方法。

How to handle users being banned 如何处理被禁止的用户

If you do need to support the ability to immediately ban a user and don't allow any other request to the API then you'll always have to have some kind of query at each API request to see if the user was banned or not. 如果确实需要支持立即禁止用户的功能并且不允许对API的任何其他请求,那么您将始终必须对每个API请求进行某种查询,以查看是否禁止了该用户。 This increases the complexity significantly so do consider that you can tolerate a solution where the lifetime of a token is shorter and banned users may still call your API within that short time frame. 这极大地增加了复杂性,因此请考虑考虑您可以容忍这样的解决方案:令牌的生存期较短,被禁止的用户仍可以在较短的时间内调用您的API。

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

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