我已经将devise_token_auth gem添加到了我的Rails应用程序中,但是无法通过POST创建会话。我在回购中发布了一个问题,并将其复制到此处:
发布到sign_in时,SessionsController#new最终处理请求并抛出405响应错误。我试图将binding.pry语句放入SessionsController#create中,但似乎永远都无法做到这一点。我已经浏览了控制器代码,但不确定是什么原因导致转发。
Request URL: http://localhost:3001/api/v1/auth/sign_in/
Request Method: OPTIONS
Status Code: 200 OK
Remote Address: [::1]:3001
Referrer Policy: no-referrer-when-downgrade
Access-Control-Allow-Headers: content-type
Access-Control-Allow-Methods: GET, POST, DELETE, PUT, OPTIONS
Access-Control-Allow-Origin: *
Access-Control-Expose-Headers: access-token, expiry, token-type, uid, client
Access-Control-Max-Age: 0
Cache-Control: no-store, must-revalidate, private, max-age=0
Connection: Keep-Alive
Content-Length: 0
Date: Sat, 01 Jun 2019 21:55:36 GMT
Server: WEBrick/1.3.1 (Ruby/2.4.5/2018-10-18)
X-Miniprofiler-Ids: ["68m1ljg85tsd0xrzzmcc","j1r1eprbdi3557u8n9wx","qt8p97wcf4c5h2c843qe","mga8ke9kg7tazpykg189","9ofwsj7y8s2rbencil7w","4qzfcw9ffzrpdt286kln","2opxz8nfmgcy128jw1iw","dzquvys1vz3gwfqvzxrq","qnmqv8bryuszhdcx81ck","ka0z5wp5am4gqmjvg83r","p375w63rkvnjymyjs5y4","9bpw5xhsv5bdnswi6j04","4tv42067lbh9259zfizg","v8rmdrixi14mqv6jqna4","m0rlj6kc47iqk9922kjl","un61k4w2aetsb3lqjelr","7fy92psnxru0jdkrmnuz","9w9yqk31aico8rdrf4dp","9xwjo0s7xes9wv9fhaw0","h6xm3isj5xju2xnnqs9v"]
Provisional headers are shown
Access-Control-Request-Headers: content-type
Access-Control-Request-Method: POST
Origin: http://localhost:3000
Referer: http://localhost:3000/
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36
2。
Request URL: http://localhost:3001/api/v1/auth/sign_in/
Request Method: GET
Status Code: 301 Moved Permanently
Remote Address: [::1]:3001
Referrer Policy: no-referrer-when-downgrade
Access-Control-Allow-Methods: GET, POST, DELETE, PUT, OPTIONS
Access-Control-Allow-Origin: *
Access-Control-Expose-Headers: access-token, expiry, token-type, uid, client
Access-Control-Max-Age: 0
Cache-Control: no-cache
Connection: Keep-Alive
Content-Length: 111
Content-Type: text/html
Date: Sat, 01 Jun 2019 21:55:36 GMT
Location: http://www.localhost:3001/api/v1/auth/sign_in
Server: WEBrick/1.3.1 (Ruby/2.4.5/2018-10-18)
Vary: Origin
X-Request-Id: 900ce57c-6ca9-45b0-b5ba-9d51b552e8e9
X-Runtime: 0.159825
Provisional headers are shown
Referer: http://localhost:3000/
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36
Request URL: http://www.localhost:3001/api/v1/auth/sign_in
Request Method: OPTIONS
Status Code: 200 OK
Remote Address: [::1]:3001
Referrer Policy: no-referrer-when-downgrade
Access-Control-Allow-Headers: content-type
Access-Control-Allow-Methods: GET, POST, DELETE, PUT, OPTIONS
Access-Control-Allow-Origin: *
Access-Control-Expose-Headers: access-token, expiry, token-type, uid, client
Access-Control-Max-Age: 0
Cache-Control: no-store, must-revalidate, private, max-age=0
Connection: Keep-Alive
Content-Length: 0
Date: Sat, 01 Jun 2019 21:55:36 GMT
Server: WEBrick/1.3.1 (Ruby/2.4.5/2018-10-18)
X-Miniprofiler-Ids: ["j1b0awwzflski3efghxv","68m1ljg85tsd0xrzzmcc","j1r1eprbdi3557u8n9wx","qt8p97wcf4c5h2c843qe","mga8ke9kg7tazpykg189","9ofwsj7y8s2rbencil7w","4qzfcw9ffzrpdt286kln","2opxz8nfmgcy128jw1iw","dzquvys1vz3gwfqvzxrq","qnmqv8bryuszhdcx81ck","ka0z5wp5am4gqmjvg83r","p375w63rkvnjymyjs5y4","9bpw5xhsv5bdnswi6j04","4tv42067lbh9259zfizg","v8rmdrixi14mqv6jqna4","m0rlj6kc47iqk9922kjl","un61k4w2aetsb3lqjelr","7fy92psnxru0jdkrmnuz","9w9yqk31aico8rdrf4dp","9xwjo0s7xes9wv9fhaw0"]
Provisional headers are shown
Access-Control-Request-Headers: content-type
Access-Control-Request-Method: GET
Origin: null
Referer: http://localhost:3000/
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36
4。
Request URL: http://www.localhost:3001/api/v1/auth/sign_in
Request Method: GET
Status Code: 405 Method Not Allowed
Remote Address: [::1]:3001
Referrer Policy: no-referrer-when-downgrade
Access-Control-Allow-Methods: GET, POST, DELETE, PUT, OPTIONS
Access-Control-Allow-Origin: *
Access-Control-Expose-Headers: access-token, expiry, token-type, uid, client
Access-Control-Max-Age: 0
Cache-Control: no-cache
Connection: Keep-Alive
Content-Length: 82
Content-Type: application/json; charset=utf-8
Date: Sat, 01 Jun 2019 21:55:49 GMT
Server: WEBrick/1.3.1 (Ruby/2.4.5/2018-10-18)
Vary: Origin
X-Content-Type-Options: nosniff
X-Frame-Options: SAMEORIGIN
X-Request-Id: 7d0df751-c98f-41f2-967f-714078459948
X-Runtime: 12.819341
X-Xss-Protection: 1; mode=block
Provisional headers are shown
Content-Type: application/json
Origin: null
Referer: http://localhost:3000/
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36
Started POST "/api/v1/auth/sign_in/" for ::1 at 2019-06-01 17:55:36 -0400
Started GET "/api/v1/auth/sign_in" for ::1 at 2019-06-01 17:55:36 -0400
Processing by DeviseTokenAuth::SessionsController#new as */*
Parameters: {"session"=>{}}
Completed 405 Method Not Allowed in 12763ms (Views: 1.5ms | ActiveRecord: 0.0ms)
api/v1/auth
fetch(`${process.env.REACT_APP_API}/auth/sign_in/`, {
method: 'POST',
mode: 'cors',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({ username, password }),
})