Javascript/Rails 授权获取 headers
Javascript/Rails authorization fetch headers
所以我正在开发 React/Redux SPA 应用程序,我希望获得授权。我有 Rails 后端和 devise_token_auth gem 工作并且(在 React 应用程序中)我需要保存后端响应我的令牌。但是 response.headers
没有可用的代币。为什么? CORS 在后端正确设置,所以我确定这不是问题所在。看看代码和截图:
let config = {
method: 'POST',
headers: {
"Accept": "application/json",
"Content-Type": "application/json"
},
body: JSON.stringify(creds) // { email: 'asd@example.org', password: 'asdasdasd' }
}
return dispatch => {
dispatch(requestLogin(creds))
return fetch('http://localhost:3000/auth/sign_in', config)
.then((response => {
response.headers.forEach((el) => console.log(el))
}))
}
console.log:
证明浏览器看到 headers:
已修复。问题出在后端。我在我的 rack-cors 初始值设定项中有这个:
headers: :any,
methods: [:get, :post, :put, :patch, :delete, :options, :head]
改为:
headers: :any,
expose: ['access-token', 'expiry', 'token-type', 'uid', 'client'],
methods: [:get, :post, :put, :patch, :delete, :options, :head]
现在一切正常:)
在查看 devise_token_auth 的源代码时得到它。
所以我正在开发 React/Redux SPA 应用程序,我希望获得授权。我有 Rails 后端和 devise_token_auth gem 工作并且(在 React 应用程序中)我需要保存后端响应我的令牌。但是 response.headers
没有可用的代币。为什么? CORS 在后端正确设置,所以我确定这不是问题所在。看看代码和截图:
let config = {
method: 'POST',
headers: {
"Accept": "application/json",
"Content-Type": "application/json"
},
body: JSON.stringify(creds) // { email: 'asd@example.org', password: 'asdasdasd' }
}
return dispatch => {
dispatch(requestLogin(creds))
return fetch('http://localhost:3000/auth/sign_in', config)
.then((response => {
response.headers.forEach((el) => console.log(el))
}))
}
console.log:
证明浏览器看到 headers:
已修复。问题出在后端。我在我的 rack-cors 初始值设定项中有这个:
headers: :any,
methods: [:get, :post, :put, :patch, :delete, :options, :head]
改为:
headers: :any,
expose: ['access-token', 'expiry', 'token-type', 'uid', 'client'],
methods: [:get, :post, :put, :patch, :delete, :options, :head]
现在一切正常:) 在查看 devise_token_auth 的源代码时得到它。