Devise and OmniAuth remembering OAuth

前端 未结 4 644
梦谈多话
梦谈多话 2020-12-04 22:18

So, I just got setup using Rails 3, Devise and OmniAuth via https://github.com/plataformatec/devise/wiki/OmniAuth:-Overview.

I\'m successfully authenticating users v

4条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-04 23:18

    I'd like to elaborate on the (correct) answer @jeroen-van-dijk gave above which worked for me.

    In config/routes.rb, add a new route in the devise_for block:

    devise_for :users, :controllers => {
                         :omniauth_callbacks => "user_omniauth_callbacks" } do
      ...
      get '/users/connect/:network', :to => redirect("/users/auth/%{network}"),
                                     :as => 'user_oauth_connect'
    
    end
    

    Then change your "login using facebook" link to use the new route:

    
    <%= link_to "Sign in using Facebook", user_oauth_connect_path(:facebook) %>
    

    In app/controllers/user_omnniauth_callbacks_controller.rb

    class UserOmniauthCallbacksController < Devise::OmniauthCallbacksController
      include Devise::Controllers::Rememberable
    
      def facebook
        @user = User.find(...)
        ...
        remember_me(@user) # set the remember_me cookie
      end
    end
    

    This solution works well for me using Rails 3.1 and Devise 1.4.9.

提交回复
热议问题