问题描述
我在让 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?
这是设计令牌认证,销毁方法。它确实到达此方法并在断点处停止。
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
方法:
这是获取标头、检查它们并设置实例变量的地方。
通过发现这个,我意识到我没有发送我认为我正在发送的标题。我更改了我的 http 请求,它工作正常。
axios.get('api/v1/auth/sign_out',headers)
旁注:
设计中默认的注销操作是 delete
但这可以修改:
config.sign_out_via = :get