使用 oAuth 登录,我应该存储/使用什么来识别用户? [英] sign in with oAuth, what should i store/use to identify the user?

查看:25
本文介绍了使用 oAuth 登录,我应该存储/使用什么来识别用户?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在我的应用程序中使用 facebook/twitter 功能实现登录,我阅读了一些关于 oAuth 的指南,我想我理解了一些基本概念,这就是我所理解的(如果我是,请纠正我错):

im trying to implement a login with facebook/twitter functionality in my app, i read some guides on oAuth, and i think i understood some of the basic concept, and here is what i understood (please correct me if i'm wrong):

  1. myApp 向 oAuth 提供者发送请求,获取 (A) 请求令牌.
  2. 发送用户对 (A) 进行身份验证,返回 (B) 已通过身份验证的请求令牌(这就是所谓的 oAuth 令牌吗?)
  3. 使用 (B) 获取 (C) 访问令牌.
  4. 使用 C 访问用户信息.

这是我无法理解的内容,我应该使用/存储其中哪一个来识别用户?我考虑过使用其中每一个的可能性,但我总是坚持如何检查用户之前是否已登录...

and here is what i can't get around my head, which one of these that i should use/store to identify the user? i thought about the possibility of using each one of those, but im always stuck on how to check if the user has signed in before...

推荐答案

如果你需要的只是身份验证,那么只存储 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.

这篇关于使用 oAuth 登录,我应该存储/使用什么来识别用户?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆