guild_front/src/server/server.js

101 lines
2.6 KiB
JavaScript
Raw Normal View History

2021-07-02 16:02:47 +03:00
export const fetchProfile = async (link, index) => {
2021-07-21 14:40:06 +03:00
try {
2021-08-04 17:27:09 +03:00
const response = await fetch(`${link}${index}`, {
2021-08-05 16:52:18 +03:00
method: 'GET',
2021-08-04 17:27:09 +03:00
headers: {
2021-08-09 11:18:09 +03:00
// 'Access-Control-Request-Headers': 'authorization',
2021-08-04 17:48:20 +03:00
'Authorization': `Bearer ${localStorage.getItem('auth_token')}`,
2021-08-09 11:18:09 +03:00
// 'Origin': `${process.env.REACT_APP_BASE_URL}`,
2021-08-04 17:27:09 +03:00
}
})
2021-08-04 13:04:05 +03:00
let data = await response.json()
2021-06-28 17:57:28 +03:00
2021-08-04 13:04:05 +03:00
return data
2021-07-21 14:40:06 +03:00
} catch (error) {}
2021-08-04 13:04:05 +03:00
}
2021-06-29 17:58:15 +03:00
export const fetchSkills = async (link) => {
2021-07-21 14:40:06 +03:00
try {
2021-08-04 17:27:09 +03:00
const response = await fetch(link, {
2021-08-05 16:52:18 +03:00
method: 'GET',
2021-08-04 17:27:09 +03:00
headers: {
2021-08-09 11:18:09 +03:00
// 'Access-Control-Request-Headers': 'authorization',
2021-08-04 17:48:20 +03:00
'Authorization': `Bearer ${localStorage.getItem('auth_token')}`,
2021-08-09 11:18:09 +03:00
// 'Origin': `${process.env.REACT_APP_BASE_URL}`,
2021-08-04 17:27:09 +03:00
}
})
2021-08-04 13:04:05 +03:00
let data = await response.json()
2021-06-29 17:58:15 +03:00
2021-08-04 13:04:05 +03:00
return data
2021-07-21 14:40:06 +03:00
} catch (error) {}
2021-08-04 13:04:05 +03:00
}
2021-07-03 17:37:30 +03:00
export const fetchItemsForId = async (link, id) => {
2021-08-05 16:44:49 +03:00
console.log(`Bearer ${localStorage.getItem('auth_token')}`);
2021-07-21 14:40:06 +03:00
try {
2021-08-04 17:27:09 +03:00
const response = await fetch(`${link}${id}`, {
2021-08-05 16:52:18 +03:00
method: 'GET',
2021-08-04 17:27:09 +03:00
headers: {
2021-08-09 11:18:09 +03:00
// 'Access-Control-Request-Headers': 'authorization',
2021-08-04 17:48:20 +03:00
'Authorization': `Bearer ${localStorage.getItem('auth_token')}`,
2021-08-09 11:18:09 +03:00
// 'Origin': `${process.env.REACT_APP_BASE_URL}`,
2021-08-04 17:27:09 +03:00
}
})
2021-08-04 13:04:05 +03:00
let data = await response.json()
2021-07-03 17:37:30 +03:00
2021-08-04 13:04:05 +03:00
return data
2021-07-21 14:40:06 +03:00
} catch (error) {}
2021-08-04 13:04:05 +03:00
}
2021-07-03 17:37:30 +03:00
export const fetchForm = async (link, info) => {
2021-07-21 14:40:06 +03:00
try {
const response = await fetch(link, {
method: 'POST',
headers: {
2021-08-09 11:18:09 +03:00
// 'Access-Control-Request-Headers': 'authorization',
2021-08-04 17:27:09 +03:00
'Authorization': `Bearer ${localStorage.getItem('auth_token')}`,
2021-08-09 11:18:09 +03:00
// 'Origin': `${process.env.REACT_APP_BASE_URL}`,
2021-08-04 13:04:05 +03:00
'Content-Type': 'multipart/form-data'
2021-07-21 14:40:06 +03:00
},
2021-08-04 13:04:05 +03:00
body: info
})
2021-07-03 17:37:30 +03:00
2021-08-04 13:04:05 +03:00
return response
2021-07-21 14:40:06 +03:00
} catch (error) {}
2021-08-04 13:04:05 +03:00
}
export const fetchAuth = async ({ username, password, dispatch }) => {
2021-08-04 17:27:09 +03:00
const baseURL = process.env.REACT_APP_BASE_URL;
const apiURL = process.env.REACT_APP_API_URL;
2021-08-04 13:04:05 +03:00
try {
const response = await fetch(
2021-08-04 17:27:09 +03:00
`${apiURL}/api/user/login`,
2021-08-04 13:04:05 +03:00
{
method: 'POST',
headers: {
2021-08-04 17:27:09 +03:00
'Content-Type': 'application/json',
2021-08-09 11:18:09 +03:00
// 'Origin': `${baseURL}`,
2021-08-04 13:04:05 +03:00
},
body: JSON.stringify({
username,
password
})
}
)
if(!response.ok) {
return response.statusText;
}
response
.json()
.then((resJSON) => {
localStorage.setItem('auth_token', resJSON.access_token)
localStorage.setItem('access_token_expired_at', resJSON.access_token_expired_at)
dispatch();
})
} catch (error) {
console.error('Error occured: ', error)
}
}