我正在尝试使用 react 和 django rest 登录。我有登录后端的 rest-auth,用户来自 LDAP。
django 上的登录有效。当从 React 调用时,我的后端的响应也有效。
我尝试使用 react-cookie 将我的令牌保存在 cookie 中。
但是当我这样做时,出现错误:TypeError: Cannot read property 'token' of undefined
我拆分了我的代码。我有一个文件 api_auth_service.js
export class APILogin {
static loginUser(body){
return fetch('http://127.0.0.1:8000/rest-auth/login/', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(body)
}).then( resp => resp.json())
}
}
和我的登录视图反应:
export default function Login() {
const [ username, setUsername] = useState('');
const [ password, setPassword] = useState('');
const [token, setToken] = useCookies(['gra-token']);
useEffect(() => {
console.log(token);
}, [token])
const loginClicked = () => {
APILogin.loginUser({username, password})
.then( resp => console.log(resp))
.then(resp => setToken('gra-token', resp.token))
.catch( error => console.log(error))
}
return ( .............
正如您所看到的,保存到我的 cookie 中的显然不是令牌
侃侃尔雅
相关分类