Omniauth Facebook手动路径路线 [英] Omniauth Facebook Manual Rails Routes

查看:87
本文介绍了Omniauth Facebook手动路径路线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正试图在Rails 3应用程序中使用Omniauth Facebook与两个设计模型。目前,这不是可以使用全方位和设计助手的东西。



类似的问题回答了如何做到这一点:



Omniauthwith STI和设计



将您的omniauth配置从devise.rb移动到omniauth.rb并创建自己的全方位路线。



但是我在定义这些路线和帮手时遇到麻烦,即对于我使用了这样的设计助手:

  user_omniauth_authorize_path(:facebook)

..重定向到Facebook然后放置我设置在我的devise_for范围内的回调网址。



如果手动设置,我的路线/帮助者将如何看待omniauth-facebook策略? strong>



我已经有omniauth-facebook,并且很好地为用户模式工作,所以我有各种各样的

  @user = User.find_for_facebook_oauth(request.env [omniauth.auth],current_user,params [:state])

..零件准备好了,但现在有两个模型,我只是在更换设计助手时遇到麻烦

解决方案

您的链接应该是这样的:

 <%= link_to用Facebook登录,/ auth / facebook%> 

您还必须创建一些回调路由,有两种方法可以执行此操作。您可以:


  1. 定义一个捕获所有回调并重定向到单个控制器操作的路由:

      get/ auth /:provider / callback=> 验证#创建

    然后可以使用 params [:provider] / code>获取提供者的名称。使用如果,那么你可以根据提供者的不同做不同的事情。


  2. 或定义一个路由每个单一提供商,您可以指向控制器中的不同操作:

      get/ auth / twitter / callback=> authentications#twitter
    get/ auth / facebook / callback=> 认证#facebook

    这些路线也可以在一条路线上缩小:

      get/ auth /:action / callback,
    :to => authentications,
    :constraints => {:action => / twitter | facebook /}

    然后,您需要定义这些操作。例如,在Twitter的动作中,您可以获取用户的推文,并在Facebook操作中获取用户的帖子。


另外,不要忘记创建失败操作来处理用户不授权使用某个提供商登录的情况。



现在,你会遇到一个问题。你如何知道链接应该指向哪里?对于Twitter和Facebook,分别似乎很明显( / auth / twitter / auth / facebook )。 / p>

但是,如果您使用 omniauth-google-oauth2 gem登录使用Google+如何?您唯一的希望是,每个omniauth宝石(在此列出)有一些好处文件。



无论如何,如果你不知道你应该使用的url,或者你根本不喜欢某个gem使用的url你可以随时更改它!



例如url / auth / google_oauth2 肯定不漂亮,至少与Facebook和Twitter的网址相比。要更改网址,请使用名称选项。

 #omniauth。 rb,当使用纯omniauth 
提供者:google_oauth2,ENV ['GOOGLE_KEY'],ENV ['GOOGLE_SECRET'],
{
名称:'google'
}

#devise.rb,当使用omniauth + devise
config.omniauth:google_oauth2,ENV ['GOOGLE_KEY'],ENV ['GOOGLE_SECRET'],
{
name: 'google',
}

现在你可以使用url / auth / google ,好多了。



注意:我多次引用了一个 AuthenticateController ,但也许你有一个 CallbacksController 或一个 OmniauthCallbacksController ,它不'真的很重要你所说的。


I am trying to use Omniauth facebook with two devise models in a Rails 3 app. Currently that's not something that's possible using omniauthable and the devise helpers.

A similar question answered how to do this:

Omniauth "with" STI and devise

"..move your omniauth configuration from devise.rb to omniauth.rb and create your own omniauth routes.'

But I am having trouble in defining these routes and helpers, i.e. for facebook I used a devise helper like this:

user_omniauth_authorize_path(:facebook) 

..which redirected to facebook and then placed a callback URL I set up in my devise_for scope.

What would my routes/helpers look like for the omniauth-facebook strategy if setting up manually?

I already had omniauth-facebook and devise nicely working for the 'User' model before, so I have the various

@user = User.find_for_facebook_oauth(request.env["omniauth.auth"], current_user, params[:state])

..parts ready to go, but now with two models. I'm just having trouble with replacing the devise helpers I think.

解决方案

Your link should look this this:

<%= link_to "Sign in with Facebook", "/auth/facebook" %>

You also have to create some callback routes, and there are 2 ways you could do this. You can either:

  1. Define a route that catches all callbacks and redirects to a single controller's action:

    get "/auth/:provider/callback" => "authentications#create"
    

    You can then use params[:provider] to get the name of the provider. Using an if you could then do things differently depending on the provider.

  2. Or define a route for every single provider where you can point to different actions in your controller:

    get "/auth/twitter/callback" => "authentications#twitter"
    get "/auth/facebook/callback" => "authentications#facebook"
    

    These routes can also be condensed in a single route:

    get "/auth/:action/callback",
      :to => "authentications",
      :constraints => { :action => /twitter|facebook/ }
    

    You would then need to define these actions. In the twitter action you could fetch the user's tweets and in the facebook action you could fetch the user's posts, for example.

Also, don't forget to create the failure action to deal with the situation where a user does not authorize logging in with a certain provider.

Now, you will come across a problem. How do you find out where the link should point to? For Twitter and Facebook, it seems pretty obvious (/auth/twitter and /auth/facebook, respectively).

But what if you are using the omniauth-google-oauth2 gem to sign in using Google+? Your only hope is that each omniauth gem (listed here) has some good documentation.

In any case, if you don't know the url you're supposed to use or if you simply don't like the url used by a certain gem, you can always change it!

For example the url /auth/google_oauth2 is certainly not pretty, at least compared to facebook's and twitter's urls. To change the url, use the name option.

# omniauth.rb, when using pure omniauth
provider :google_oauth2, ENV['GOOGLE_KEY'], ENV['GOOGLE_SECRET'],
  {
    name: 'google'
  }

# devise.rb, when using omniauth+devise
config.omniauth :google_oauth2, ENV['GOOGLE_KEY'], ENV['GOOGLE_SECRET'],
  {
    name: 'google',
  }

Now you can use the url /auth/google, much better.

Note: I made several references to an AuthenticationsController but maybe what you have is a CallbacksController or an OmniauthCallbacksController, it doesn't really matter what you call it.

这篇关于Omniauth Facebook手动路径路线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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