Переписываю спорные решения
This commit is contained in:
@ -1,22 +1,22 @@
|
||||
export const withAuthRedirect =
|
||||
(actionCall) =>
|
||||
({ link, params, history, role, logout, body }) => {
|
||||
const linkWithParams = params
|
||||
? `${link}?${new URLSearchParams(params)}`
|
||||
: link
|
||||
return actionCall(linkWithParams, body)
|
||||
.then((res) => {
|
||||
if (res.status && res.status === 401) {
|
||||
localStorage.clear()
|
||||
logout && logout()
|
||||
history.push(role === 'ROLE_DEV' ? '/authdev' : '/auth')
|
||||
}
|
||||
|
||||
return res
|
||||
})
|
||||
.catch((err) => {
|
||||
localStorage.clear()
|
||||
logout && logout()
|
||||
history.push(role === 'ROLE_DEV' ? '/authdev' : '/auth')
|
||||
})
|
||||
}
|
||||
// const linkWithParams = params
|
||||
// ? `${link}?${new URLSearchParams(params)}`
|
||||
// : link;
|
||||
// return actionCall(linkWithParams, body)
|
||||
// .then((res) => {
|
||||
// if (res.status && res.status === 401) {
|
||||
// localStorage.clear();
|
||||
// logout && logout();
|
||||
// history.push(role === 'ROLE_DEV' ? '/authdev' : '/auth')
|
||||
// }
|
||||
//
|
||||
// return res
|
||||
// })
|
||||
// .catch((err) => {
|
||||
// localStorage.clear();
|
||||
// logout && logout();
|
||||
// history.push(role === 'ROLE_DEV' ? '/authdev' : '/auth')
|
||||
// })
|
||||
};
|
||||
|
@ -19,67 +19,67 @@ import { withAuthRedirect } from './authRedirect'
|
||||
// }
|
||||
// });
|
||||
|
||||
export const fetchAuth = async ({
|
||||
username,
|
||||
password,
|
||||
dispatch,
|
||||
catchError
|
||||
}) => {
|
||||
const apiURL = process.env.REACT_APP_API_URL;
|
||||
try {
|
||||
const response = await fetch(`${apiURL}/api/user/login`, {
|
||||
method: 'POST',
|
||||
mode: 'cors',
|
||||
headers: {
|
||||
'Access-Control-Request-Headers': 'authorization',
|
||||
'Content-Type': 'application/json'
|
||||
// Origin: `http://localhost`
|
||||
},
|
||||
body: JSON.stringify({
|
||||
username,
|
||||
password
|
||||
})
|
||||
});
|
||||
// export const fetchAuth = async ({
|
||||
// username,
|
||||
// password,
|
||||
// dispatch,
|
||||
// catchError
|
||||
// }) => {
|
||||
// const apiURL = process.env.REACT_APP_API_URL;
|
||||
// try {
|
||||
// const response = await fetch(`${apiURL}/api/user/login`, {
|
||||
// method: 'POST',
|
||||
// mode: 'cors',
|
||||
// headers: {
|
||||
// 'Access-Control-Request-Headers': 'authorization',
|
||||
// 'Content-Type': 'application/json'
|
||||
// // Origin: `http://localhost`
|
||||
// },
|
||||
// body: JSON.stringify({
|
||||
// username,
|
||||
// password
|
||||
// })
|
||||
// });
|
||||
//
|
||||
// if (!response.ok) {
|
||||
// catchError();
|
||||
// return response.statusText
|
||||
// }
|
||||
//
|
||||
// response.json().then((resJSON) => {
|
||||
// localStorage.setItem('auth_token', resJSON.access_token);
|
||||
// localStorage.setItem('id', resJSON.id);
|
||||
// localStorage.setItem('cardId', resJSON.card_id);
|
||||
// localStorage.setItem(
|
||||
// 'access_token_expired_at',
|
||||
// resJSON.access_token_expired_at
|
||||
// );
|
||||
// dispatch(resJSON)
|
||||
// })
|
||||
// } catch (error) {
|
||||
// console.error('Error occured: ', error)
|
||||
// }
|
||||
// };
|
||||
|
||||
if (!response.ok) {
|
||||
catchError();
|
||||
return response.statusText
|
||||
}
|
||||
|
||||
response.json().then((resJSON) => {
|
||||
localStorage.setItem('auth_token', resJSON.access_token);
|
||||
localStorage.setItem('id', resJSON.id);
|
||||
localStorage.setItem('cardId', resJSON.card_id);
|
||||
localStorage.setItem(
|
||||
'access_token_expired_at',
|
||||
resJSON.access_token_expired_at
|
||||
);
|
||||
dispatch(resJSON)
|
||||
})
|
||||
} catch (error) {
|
||||
console.error('Error occured: ', error)
|
||||
}
|
||||
};
|
||||
|
||||
export const fetchReportList = withAuthRedirect(async (link) => {
|
||||
try {
|
||||
const response = await fetch(
|
||||
`https://guild.loc/api/reports/index?user_id=26&fromDate=2021-10-18`,
|
||||
// link,
|
||||
{
|
||||
method: 'GET',
|
||||
headers: {
|
||||
Authorization: `Bearer ${localStorage.getItem('auth_token')}`
|
||||
}
|
||||
}
|
||||
);
|
||||
let data = await response.json();
|
||||
|
||||
return data
|
||||
} catch (error) {
|
||||
console.log('Query error', error)
|
||||
}
|
||||
});
|
||||
// export const fetchReportList = withAuthRedirect(async (link) => {
|
||||
// try {
|
||||
// const response = await fetch(
|
||||
// `https://guild.loc/api/reports/index?user_id=26&fromDate=2021-10-18`,
|
||||
// // link,
|
||||
// {
|
||||
// method: 'GET',
|
||||
// headers: {
|
||||
// Authorization: `Bearer ${localStorage.getItem('auth_token')}`
|
||||
// }
|
||||
// }
|
||||
// );
|
||||
// let data = await response.json();
|
||||
//
|
||||
// return data
|
||||
// } catch (error) {
|
||||
// console.log('Query error', error)
|
||||
// }
|
||||
// });
|
||||
|
||||
export const fetchGet = withAuthRedirect(async (link) => {
|
||||
try {
|
||||
|
Reference in New Issue
Block a user