Devise auth/sign_in api not returning access-token

2019-07-20 13:33发布

问题:

i have recently integrated omniauth plugin into my rails app. and now i have stuck with some issue in devise sign_in api.

The api is not returning the access-token and client information.

Request payload

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

Request headers

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

Response header

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

Before integrating omniauth , sign_in api was working properly and returning the access-token and client,

Using devise 3.5.10
Using devise_invitable 1.6.0
Using devise_token_auth 0.1.29

Application controller

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' }

回答1:

Hi adding this code #config/application.rb

worked for me

 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