DeviseToken Auth 未收到要注销的令牌/标头数据

问题描述

我在让 devise_token_auth 注销工作时遇到问题。

我的工作基于这些 SO:

How to set header and options in axios?

Why am I unable to sign out using devise_token_auth and curl?

这是设计令牌认证,销毁方法。它确实到达此方法并在断点处停止。

https://github.com/lynndylanhurley/devise_token_auth/blob/c92258038c05fcc8f6a0374ccce2e63b9f8d5312/app/controllers/devise_token_auth/sessions_controller.rb#L48

    def destroy
      # remove auth instance variables so that after_action does not run
      user = remove_instance_variable(:@resource) if @resource
      client = @token.client
      @token.clear!

      if user && client && user.tokens[client]
        user.tokens.delete(client)
        user.save!

        yield user if block_given?

        render_destroy_success
      else
        render_destroy_error
      end
    end

@token 是在另一个方法中设置的,它似乎没有被调用。我不太明白这个方法应该如何清除令牌。

在我的断点/方法顶部,我的 @token#<struct DevisetokenAuth::TokenFactory::Token client=nil,token=nil,token_hash=nil,expiry=nil> 并且 @resource 为零。

客户端请求(Vue):

 methods: {
      headers() {
        const config = {
          headers: {
            "uid": localStorage.getItem("uid"),"client": localStorage.getItem("client"),"access-token": localStorage.getItem("access-token")
          }
        }
        return config
      },async handlelogout() {
        // e.preventDefault();
        const headers = this.headers()

        localStorage.removeItem('access-token')
        localStorage.removeItem('uid')
        localStorage.removeItem('client')

        this.logout();

        let response = await axios.get('api/v1/auth/sign_out',null,headers)
      }
    }

路线:

 destroy_api_user_session GET    /api/v1/auth/sign_out(.:format)                                                          api/v1/sessions#destroy

我做错了什么? destroy 方法是如何工作的?

解决方法

好的,我错过了这个 before_action 方法:

https://github.com/lynndylanhurley/devise_token_auth/blob/c92258038c05fcc8f6a0374ccce2e63b9f8d5312/app/controllers/devise_token_auth/concerns/set_user_by_token.rb#L26

这是获取标头、检查它们并设置实例变量的地方。

通过发现这个,我意识到我没有发送我认为我正在发送的标题。我更改了我的 http 请求,它工作正常。

axios.get('api/v1/auth/sign_out',headers)

旁注: 设计中默认的注销操作是 delete 但这可以修改:

  config.sign_out_via = :get

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...