如何使用devise_token_auth gem进行社交登录?

时间:2016-07-15 16:07:30

标签: ruby-on-rails oauth devise

如何使用带有devise_token_auth gem的OAUTH实现身份验证系统?

我几乎在互联网上的所有地方都进行了搜索,包括https://github.com/lynndylanhurley/devise_token_auth repo中的所有文档,并搜索了所有问题。

我正在开发一个API来处理用户通过社交网络注册和登录,但我无法找到一种方法来使用这个gem。

问题

  def social_login
    uri = URI('http://localhost:3000/auth/facebook')
    response = Net::HTTP.get_response(uri)
    puts response.body
    redirect_to :back
  end

这个测试API的方法(localhost:3000)给了我这个回复:

<html><body>You are being <a href="http://localhost:3000/omniauth/facebook?resource_class=User">redirected</a>.</body></html>

我的Facebook回调网址:

http://localhost:4000/auth/facebook/callback

客户端测试人员:localhost:4000

我的API路线:

                  Prefix Verb   URI Pattern                                             Controller#Action
        new_user_session GET    /auth/sign_in(.:format)                                 devise_token_auth/sessions#new
            user_session POST   /auth/sign_in(.:format)                                 devise_token_auth/sessions#create
    destroy_user_session DELETE /auth/sign_out(.:format)                                devise_token_auth/sessions#destroy
           user_password POST   /auth/password(.:format)                                devise_token_auth/passwords#create
       new_user_password GET    /auth/password/new(.:format)                            devise_token_auth/passwords#new
      edit_user_password GET    /auth/password/edit(.:format)                            devise_token_auth/passwords#edit
                         PATCH  /auth/password(.:format)                                devise_token_auth/passwords#update
                         PUT    /auth/password(.:format)                                devise_token_auth/passwords#update
cancel_user_registration GET    /auth/cancel(.:format)                                  devise_token_auth/registrations#cancel
       user_registration POST   /auth(.:format)                                         devise_token_auth/registrations#create
   new_user_registration GET    /auth/sign_up(.:format)                                 devise_token_auth/registrations#new
  edit_user_registration GET    /auth/edit(.:format)                                    devise_token_auth/registrations#edit
                         PATCH  /auth(.:format)                                         devise_token_auth/registrations#update
                         PUT    /auth(.:format)                                         devise_token_auth/registrations#update
                         DELETE /auth(.:format)                                         devise_token_auth/registrations#destroy
     auth_validate_token GET    /auth/validate_token(.:format)                          devise_token_auth/token_validations#validate_token
            auth_failure GET    /auth/failure(.:format)                                 devise_token_auth/omniauth_callbacks#omniauth_failure
                         GET    /auth/:provider/callback(.:format)                      devise_token_auth/omniauth_callbacks#omniauth_success
                         GET    /omniauth/:provider/callback(.:format)                  devise_token_auth/omniauth_callbacks#redirect_callbacks
        omniauth_failure GET    /omniauth/failure(.:format)                             devise_token_auth/omniauth_callbacks#omniauth_failure
                         GET    /auth/:provider(.:format)                               redirect(301)

1 个答案:

答案 0 :(得分:1)

您必须使用Omniauth https://github.com/intridea/omniauth并与文档中https://github.com/lynndylanhurley/devise_token_auth#omniauth-authentication

中解释的devise_token_auth集成