使用omniauth来facebook连接具有不同权限的现有用户

发布于 2024-12-10 21:05:46 字数 258 浏览 0 评论 0原文

我正在使用 devise/omniauth 进行 Facebook 身份验证并且效果很好。但是,我想添加一个流程,使现有(非 Facebook)用户能够将其帐户与 Facebook 连接。这需要不同的 Facebook 权限。所以我似乎找不到两件事

  1. 如何使用 devise/omniauth 请求 facebook 连接而不注销当前用户
  2. 请求用户的不同扩展权限(与 devise 配置文件中指定的权限不同)

有什么想法吗?谢谢

I'm using devise/omniauth to do facebook authentication and works great. However, I would like to add a flow where an existing (non-facebook) user has ability to connect his account with facebook. This would require different facebook permissions. so i can't seem to find two things

  1. how to use devise/omniauth to request facebook connect without logging out current user
  2. request different extended permissions from user (different from those specified in the devise configuration file)

any ideas? thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

琉璃梦幻 2024-12-17 21:05:46

1 的答案非常简单:只需将 if 路径添加到omniauth_callbacks_controller::process_callback 方法中,如下所示

  # If a user is signed in then he is trying to link a new account
    if user_signed_in?
      if authentication.persisted? # This was a linking operation so send back the user to the account edit page  
        flash[:success] = I18n.t "controllers.omniauth_callbacks.process_callback.success.link_account", 
                                :provider => registration_hash[:provider].capitalize, 
                                :account => registration_hash[:email]
      else
        flash[:error] = I18n.t "controllers.omniauth_callbacks.process_callback.error.link_account", 
                               :provider => registration_hash[:provider].capitalize, 
                               :account => registration_hash[:email],
                               :errors =>authentication.errors
      end  
      redirect_to edit_user_account_path(current_user)

这就是我在应用程序中所做的,并且工作正常。

关于问题 2,我不知道如何支持 2 种不同的 facebook 身份验证配置,但是我很难看出这对用户有什么用,因为他们需要在两条路径上获得一致的体验:“使用 facebook 登录”和“将您的帐户链接到 facebook ”。
(如果您仍然想走这条路,我会探索的一个想法是创建一个具有独立密钥和配置的新 Facebook 应用程序......)

希望这有所帮助。

Answer to 1 is pretty easy: just add a if path into the omniauth_callbacks_controller::process_callback method like this

  # If a user is signed in then he is trying to link a new account
    if user_signed_in?
      if authentication.persisted? # This was a linking operation so send back the user to the account edit page  
        flash[:success] = I18n.t "controllers.omniauth_callbacks.process_callback.success.link_account", 
                                :provider => registration_hash[:provider].capitalize, 
                                :account => registration_hash[:email]
      else
        flash[:error] = I18n.t "controllers.omniauth_callbacks.process_callback.error.link_account", 
                               :provider => registration_hash[:provider].capitalize, 
                               :account => registration_hash[:email],
                               :errors =>authentication.errors
      end  
      redirect_to edit_user_account_path(current_user)

This is what I do in my application and it works fine.

Regarding question 2 I do not know how to support 2 different facebook authentication configurations however I have hard time seeing how that is useful to users since they need a consistent experience across both path: "sign in using facebook" and "link your account to facebook".
(If you still want to pursue this path one idea I would explore is to create a new facebook application with its independent keys and configuration...)

Hope this help.

殤城〤 2024-12-17 21:05:46

实现多层权限的一种简单方法是使用 Facebook Javascript SDK(如果需要,除了omniauth之外)。您可以简单地在每次调用时指定不同的“范围”参数,该参数指定所需的权限。我正在做的是让omniauth提供一组基本的权限,然后,用户通过omniauth连接(从而将他们的数据存储在我们的数据库中),如果需要进一步的权限,我们会显示它们是基于 JS 的按钮,提供扩展的权限集。如果您想检查用户授予您哪些特定权限,您只需使用 me/permissions API 调用即可。

One simple way to implement multi-tier permissions is to use Facebook Javascript SDK(in addition to omniauth, if you want). You can simply specify different "scope" parameter, which specifies permissions required, at each call you want. What I'm doing is making omniauth provide a basic set of permissions, then, after the user has connected through omniauth(and thus stored their data in our DB), if further permissions are needed, we show them JS-based buttons which provide expanded sets of permissions. If you want to check what particular permissions a user has granted to you, you can simply use me/permissions API call.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文