1
我正在使用Rails 5和Devise進行Ajax身份驗證。表格用戶列已被阻止,如果用戶被阻止,他將被註銷。我已經設置標題爲阿賈克斯全球:InvalidAuthenticityToken,Ajax,Rails 5 with Device
$(document).ajaxSend(function(e, xhr, options) {
var token = $("meta[name='csrf-token']").attr("content");
xhr.setRequestHeader("X-CSRF-Token", token);
});
這是替換SessionsController創建方法:
def create
if warden.authenticate(:scope => resource_name)
if current_user.blocked
sign_out(@user)
return render json: {blocked: true}
else
return render json:{success: true}
end
else
return render json: {error: true}
end
end
如果用戶被封鎖,他試圖驗證來自服務器的響應是{blocked: true}
。而無需刷新頁面,如果他再次嘗試的響應是一個錯誤:
的ActionController :: InvalidAuthenticityToken在用戶:: SessionsController#創建 的ActionController :: InvalidAuthenticityToken 提取的源(左右線#195):
我在標題中看到每次用戶嘗試登錄時都會發送令牌。 我知道可能是問題是因爲第一次登錄後令牌被更改,但因爲請求是使用Ajax它可以獲得新的令牌,但我不知道如何解決該問題。