onLogin = (credentials) => async (dispatch) => {
const rawResponse = await fetch(
"https://stranger-go.com/api/v1/token/login/",
{
method: "POST",
headers: {
Accept: "application/json",
"Content-Type": "application/json",
},
body: JSON.stringify(
credentials
),
}
);
if (rawResponse.ok) {
const content = await rawResponse.json();
const response = await fetch("https://stranger-go.com/api/v1/users/me/", {
method: "GET",
headers: {
Accept: "application/json",
"Content-Type": "application/json",
Authorization: `Token ${content.auth_token}`,
},
});
if (response.ok) {
const userData = await response.json();
dispatch({
type: SET_TOKEN,
payload: content.auth_token,
});
dispatch({
type: SET_AUTH,
payload: true,
});
dispatch({
type: SET_ADMIN,
payload: userData.is_staff,
});
dispatch({
type: SET_BLOCK,
payload: userData.is_block,
});
} else {
const err = await response.json();
alert("Ошибка HTTP: " + response.status + " " + JSON.stringify(err));
}
} else {
const err = await rawResponse.json();
alert("Ошибка HTTP: " + rawResponse.status + " " + JSON.stringify(err));
}
};
ПАМАГИТИ)
1. Выложите это в песочницу
Обсуждают сегодня