0

我想用 facebook 登录构建 RailsAPI,所以我选择了devise_token_auth. 我按照说明运行rails g devise_token_auth:install User auth,我的路线看起来像

路线.rb

Rails.application.routes.draw do
  mount_devise_token_auth_for 'User', at: 'auth'
  # For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
end

所以它是来自生成器的东西。我在 facebook 为开发人员注册了我的应用程序,将 facebook-key 和 facebook-secret 存储在 env 文件中。当我第一次点击localhost:3000/auth/facebook它时,它将我重定向到 facebook auth 弹出窗口,我点击了按钮,然后错误出现了。

在此处输入图像描述

我应该用自己的设计覆盖控制器吗?怎么做?或者还有其他解决方案吗?

我想提一下这是我第一次使用 Rails 构建 API。

4

1 回答 1

0

重写时我已经解决了问题assign_provider_attrs

路由.rb mount_devise_token_auth_for 'User', at: 'auth' , controllers: { omniauth_callbacks: 'authentication_rails/omniauth_callbacks' }

控制器/关注点/authentication_rails/omniauth_callbacks_controller

module AuthenticationRails class OmniauthCallbacksController < DeviseTokenAuth::OmniauthCallbacksController protected # break out provider attribute assignment for easy method extension def assign_provider_attrs(user, auth_hash) if auth_hash['provider'] == 'facebook' user.assign_attributes({ nickname: auth_hash['info']['nickname'], name: auth_hash['info']['name'], image: auth_hash['info']['image'], email: auth_hash['info']['email'] }) else super end end end end

于 2018-03-20T00:54:52.543 回答