그래서 저는 React / Redux SPA 앱을 개발 중이며 인증이 작동하도록하고 싶습니다. devise_token_auth gem이 작동하는 Rails 백엔드가 있고 (React 앱에서) 백엔드가 나에게 응답 한 토큰을 저장해야합니다. response.headers
하지만 사용할 수있는 토큰은 없습니다 . 왜? CORS는 백엔드 측에 적절하게 설정되어 있으므로 문제가되지 않는다고 확신합니다. 코드와 스크린 샷을 살펴보세요.
let config = {
method: 'POST',
headers: {
"Accept": "application/json",
"Content-Type": "application/json"
},
body: JSON.stringify(creds) // { email: '[email protected]', 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 :
브라우저가 헤더를 본다는 증거 :
결정된. 문제는 백엔드 측에있었습니다. 내 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의 소스 코드를 살펴보면 알 수 있습니다.
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다