且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使用 oAuth 登录,我应该存储/使用什么来识别用户?

更新时间:2023-12-01 17:59:40

如果你需要的只是身份验证,那么只存储 user_id 就足够了.

If all you need is just authentication, then storing only user_id is enough.

因此创建另一个表,例如:

So create another table like:

id | service_name | user_id | my_user_id

其中 service_nametwitterfacebookuser_id - 是来自 twitter/facebook 的用户 ID 和 my_user_id 是您的身份验证系统中的 user_id.

where service_name is either twitter or facebook, user_id - is user's id from twitter/facebook and my_user_id is a user_id in your authentication system.

所以:

SELECT my_user_id FROM oauths WHERE service_name = 'twitter' AND user_id = 42

将返回您的系统 user_id 或什么都不返回

would return you your system user_id or nothing

PS:service_name 可以(也应该)被规范化,我将它保留为字符串只是为了简化示例

PS: service_name could (and should) be normalized, I kept it as a string just to simplify an example

PPS:正如您在评论中所说,您可能想要发帖/发推文".

PPS: as you said in comments you probably would want "posting/tweeting".

在这种情况下,您需要为 twitter 存储用户的访问令牌,并为 facebook 存储任何其他内容,但在验证用户时请求 publish_stream 权限.

In that case you need to store user's access token for twitter, and store nothing additional for facebook, but request for publish_stream permission when authenticate user.