简体   繁体   English

设计ajax登录

[英]Devise ajax sign_in

I was trying to make a ajax sign_in via devise , I follow this tutorial . 我试图通过做一个AJAX sign_in devise ,我按照这个教程

I've successfully created a ajax sign_up . 我已经成功创建了一个ajax sign_up Here is my registrations_controller.rb 这是我的registrations_controller.rb

class RegistrationsController < Devise::RegistrationsController
  respond_to :json
  skip_before_filter :verify_authenticity_token, :only => :create
end

It works fine but fail in sign_in . 它工作正常,但sign_in失败。

Here is my session_controller.rb 这是我的session_controller.rb

class SessionsController < Devise::SessionsController
  respond_to :json
end

The parameter and path would be like this 参数和路径是这样的

Path: /users/sign_in Parameter: 路径: /users/sign_in参数:

{
  "user": 
  {
    "email": "123@gmail.com",
    "password":"12345678",
    "remember_me":"1"
  },
  "commit":"Log in"
}

But I got this error. 但是我得到了这个错误。

在此处输入图片说明

Update 更新

I still got 401 Unauthorized . 我仍然有401 Unauthorized I've tried the following way. 我尝试了以下方法。

1.Editing application_controller.rb the protect_from_forgery to protect_from_forgery with: :null_session 1. 使用以下protect_from_forgeryapplication_controller.rbprotect_from_forgeryprotect_from_forgery with: :null_session

2.Add skip_before_filter :verify_authenticity_token, :only => :create in sessions_controller.rb skip_before_filter :verify_authenticity_token, :only => :createsessions_controller.rb中添加 skip_before_filter :verify_authenticity_token, :only => :create

3.Modify my ajax to this 3.修改我的ajax到这个

$.ajax ({
      headers: {
        'X-Transaction': 'POST Example',
        'X-CSRF-Token': $('meta[name="csrf-token"]').attr('content')
      },
      url: "sign_in",
      type: "POST",
      dataType: 'json',
      data: {
        "user": {"email": "example@gmail.com", "password": "password"}
      },
      success: function(msg){
        console.log("Success");
      },
      error: function(msg) {
        console.log("Errors");
      }
});

However, all of them threw the same error. 但是,它们都引发了相同的错误。

Started POST "/users/sign_in" for ::1 at 2016-12-14 01:13:09 +0800
Processing by SessionsController#create as JSON
  Parameters: {"user"=>{"email"=>"example4@gmail.com", "password"=>"[FILTERED]"}, "commit"=>"Log in"}
  User Load (0.3ms)  SELECT  `users`.* FROM `users` WHERE `users`.`email` = 'example4@gmail.com' ORDER BY `users`.`id` ASC LIMIT 1
Completed 401 Unauthorized in 2ms (ActiveRecord: 0.3ms)

I believe the CSRF token is missing... it's also stated in the tutorial you are following: http://blog.andrewray.me/how-to-set-up-devise-ajax-authentication-with-rails-4-0/#maketheajaxrequests 我相信CSRF令牌丢失了……您正在关注的教程中也对此进行了说明: http : //blog.andrewray.me/how-to-set-up-devise-ajax-authentication-with-rails-4- 0 /#maketheajax请求

Try editing in your application_controller.rb the protect_from_forgery to protect_from_forgery with: :null_session 尝试在您的application_controller.rb通过以下protect_from_forgery编辑protect_from_forgeryprotect_from_forgery with: :null_session

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM