rails5 api + devise_token_auth:通过omniauth2成功登录后,没有访问令牌和uid返回 [英] rails5 api + devise_token_auth: no access-token and uid returns after login successfully via omniauth2

查看:114
本文介绍了rails5 api + devise_token_auth:通过omniauth2成功登录后,没有访问令牌和uid返回的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 devise_token_auth 来通过微博和qq进行omniauth2登录.是的,我正在尝试使用rails5 api功能!

I am using devise_token_auth for omniauth2 login with weibo and qq. Yes, I am trying to use rails5 api feature!

设置并使用omniauth test_mode后,我似乎登录成功,但没有在响应的标题中返回登录信息( access_token,到期,客户端和uid ).

After setup and using omniauth test_mode, I seems like login successfully but not login info(access_token, expiry, client and uid) return in the header of response.

这里是模型,我应用候选而不是 user 作为身份验证资源.

Here is model, I apply candidate rather than user as authentication resource.

#app/models/candidate.rb

class Candidate < ApplicationRecord
  devise :database_authenticatable, :trackable, :omniauthable
  include DeviseTokenAuth::Concerns::User

  has_many :recruitments
  attr_writer :password, :password_confirmation
end

使用虚拟omniauth成功信息.

Using dummy omniauth successful info.

#config/environment/development.rb
#OmniAuth Fake
OmniAuth.config.test_mode = true
OmniAuth.config.mock_auth[:qq_connect] = OmniAuth::AuthHash.new(
  {
      provider: 'qq_connect',
      extra: { raw_info: { nickname: :dev } },
      info: { nickname: :dev },
      uid: '123456'
  })
OmniAuth.config.mock_auth[:weibo] = OmniAuth::AuthHash.new(
  {
      provider: 'weibo',
      extra: { raw_info: { nickname: :dev } },
      info: { nickname: :dev },
      uid: '1234567'
  })

因此,当我向 http://localhost:3000/auth/weibo ,应用程序将我重定向到 http://localhost:3000//auth/weibo/callback .返回200 OK.

So, when I send a get request to http://localhost:3000/auth/weibo, application redirect me to http://localhost:3000//auth/weibo/callback. It returns 200 Ok.

到目前为止,一切似乎都很好.而且我还要检查数据库,该数据库表明已创建候选人(身份验证资源),并且其令牌随每次请求而更改.

As far, everything seems fine. And also I check DB, which shows that candidate(authentication resource) was created, and its token changed with every request to it.

但是看看响应的标题,我发现里面没有什么有用的东西(访问令牌,有效期等).

But look at the header of response, I find nothing useful(access-token, expiry etc) inside.

#Header of response

Cache-Control →max-age=0, private, must-revalidate
Connection →close
Content-Type →text/plain; charset=utf-8
ETag →W/"7215ee9c7d9dc229d2921a40e899ec5f"
Server →thin 1.6.2 codename Doc Brown
X-Content-Type-Options →nosniff
X-Frame-Options →SAMEORIGIN
X-Request-Id →d91648bb-3e69-4d23-8dfa-9eedfafeac26
X-Runtime →0.085038
X-XSS-Protection →1; mode=block

所以我的问题是在使用omniauth2登录后如何获取身份验证信息(如访问令牌,客户端等)?

So My question is How to get auth info(likes access-token, client etc) after login with omniauth2?

致谢!

推荐答案

我遇到了同样的问题.也许这个决定对某人会有帮助.

I had the same problem. Maybe this decision will helpful for someone.

# app/controllers/overrides/omniauth_callbacks_controller.rb

module Overrides
  class OmniauthCallbacksController < DeviseTokenAuth::OmniauthCallbacksController
    after_action :update_auth_header, only: :omniauth_success
    end
  end
end

路线:

# config/routes.rb

Rails.application.routes.draw do
  scope '/api/v1' do
    mount_devise_token_auth_for 'User', at: 'auth', controllers: {
      omniauth_callbacks:  'overrides/omniauth_callbacks'
    }
    end
  end
end

这篇关于rails5 api + devise_token_auth:通过omniauth2成功登录后,没有访问令牌和uid返回的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆