【问题标题】:Devise auth/sign_in api not returning access-token and client设计 auth/sign_in api 不返回访问令牌和客户端
【发布时间】:2016-10-19 15:14:52
【问题描述】:

我最近将omniauth 插件集成到我的rails 应用程序中。现在我在设计 sign_in api 中遇到了一些问题。

api 没有返回访问令牌和客户端信息。

请求负载

{""email":"testuser@gmail.com","password":"test123"}

请求标头

POST /auth/sign_in HTTP/1.1
Host: localhost:3000
Connection: keep-alive
Content-Length: 95
Cache-Control: max-age=0
Accept: application/json, text/plain, */*
Origin: http://localhost:3000
If-Modified-Since: 0
User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Ubuntu Chromium/49.0.2623.108 Chrome/49.0.2623.108 Safari/537.36
Content-Type: application/json;charset=UTF-8
Referer: http://localhost:3000/md
Accept-Encoding: gzip, deflate
Accept-Language: en-US,en;q=0.8
Cookie: _my-app_session=f9cbfc20c86a7c21490b6f947b99dab7; auth_headers=%7B%7D

响应头

HTTP/1.1 200 OK
X-Frame-Options: SAMEORIGIN
X-Xss-Protection: 1; mode=block
X-Content-Type-Options: nosniff
Content-Type: application/json; charset=utf-8
Etag: "d6dcd0e9690ab0f97a38227f8c8d00a2"
Cache-Control: max-age=0, private, must-revalidate
X-Request-Id: c906dc68-5bfb-47db-ad9b-c51ccc9774c5
X-Runtime: 0.329334
Server: WEBrick/1.3.1 (Ruby/2.1.8/2015-12-16)
Date: Fri, 17 Jun 2016 17:20:50 GMT
Content-Length: 835
Connection: Keep-Alive

在集成 omniauth 之前,sign_in api 工作正常并返回访问令牌和客户端,

Using devise 3.5.10
Using devise_invitable 1.6.0
Using devise_token_auth 0.1.29

应用程序控制器

class ApplicationController < ActionController::Base
  include DeviseTokenAuth::Concerns::SetUserByToken
  layout false
  before_filter :configure_permitted_parameters, if: :devise_controller?
  before_filter :load_client
  skip_before_filter :verify_authenticity_token, :if => Proc.new { |c| c.request.format == 'application/json' }

【问题讨论】:

  • 遇到同样的问题,你解决了吗?
  • @rjoe:你的模型中有自定义密码验证吗?
  • @HenryOllarves:你的模型中有自定义密码验证吗?
  • 不!对我来说,这是一个简单的活动记录错误,因为请求缺少参数,因此正在回滚数据库查询。在我的情况下,它是用户的“名称”,我在我的模型中声明了它。所以这就是设计返回错误 200 状态的原因
  • @HenryOllarves 同样的事情发生在我身上。在我的用户模型上根据需要添加了一个参数,并且没有返回任何令牌。删除了标头中包含的验证和令牌。似乎是 devise_auth_token 中的一个错误。

标签: ruby-on-rails devise


【解决方案1】:

您好,添加此代码#config/application.rb

为我工作

 gem 'rack-cors', :require => 'rack/cors'
 module YourApp
 class Application < Rails::Application
   config.middleware.use Rack::Cors do
    allow do
      origins '*'
      resource '*',
        :headers => :any,
        :expose  => ['access-token', 'expiry', 'token-type', 'uid', 'client'],
        :methods => [:get, :post, :options, :delete, :put]
    end
    end
   end
 end

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-05-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多