Choosing user ID in application that relies on Aut

2019-07-28 04:22发布

问题:

I'm creating a TODO list application using Angular and PHP.

I use Auth0 to allow Facebook authentication and after the process completes the user is redirected to add TODO page. However, I'm confused on how to save that TODO item in my MySQL database. Which user ID should I use to save the TODO item?

I'm getting Facebook user ID in return; can I save it using the FB user ID?

回答1:

You can, but if you use the Facebook ID as your internal way to identify users in your MySQL database you're making your application less flexible to change.

For example, you save the Facebook ID in a column with a data type that supports the Facebook ID format. Now imagine, that in two months you also want to support Google users.

The Google ID may be in a format that your chosen data type does not support, so you'll need to make changes. Additionally, it could even the case that Google and Facebook identifiers are not globally unique so the same identifier can be used by Google to represent user A while at Facebook it's used for user B.

Your best bet is to save the data in association with an internal identifier that you manage and them mapped it to a globally unique way to identify your users; for example an email address.

Given you are using Auth0 you can take advantage of the fact that it will provide you with a globally unique identifier for each user. This means you can later support new ways to authenticate your users and Auth0 will always provide your application with a globally unique ID, even in scenarios that users authenticate without having to provide email addresses.