🦜
Size: a a a
🦜
Б
🦜
Б
JC
e
OJ
JC
OJ
server.post('/login', async (req, res) => {
const { username, password } = req.body;
try {
const resp = await fetch(`${process.env.BACKEND_URL}/login/`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({ username, password }),
});
const { token } = await resp.json();
const options = {
maxAge: 7 * 24 * 3600 * 1000,
httpOnly: true,
signed: false,
};
// Set cookie
ctx.res.cookie(‘token’, token, options);
res.send('success');
} catch (error) {
console.error(error);
}
});
JC
server.post('/login', async (req, res) => {
const { username, password } = req.body;
try {
const resp = await fetch(`${process.env.BACKEND_URL}/login/`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({ username, password }),
});
const { token } = await resp.json();
const options = {
maxAge: 7 * 24 * 3600 * 1000,
httpOnly: true,
signed: false,
};
// Set cookie
ctx.res.cookie(‘token’, token, options);
res.send('success');
} catch (error) {
console.error(error);
}
});
OJ
axios.defaults.withCredentials = true;
JC
axios.defaults.withCredentials = true;
Б
server.post('/login', async (req, res) => {
const { username, password } = req.body;
try {
const resp = await fetch(`${process.env.BACKEND_URL}/login/`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({ username, password }),
});
const { token } = await resp.json();
const options = {
maxAge: 7 * 24 * 3600 * 1000,
httpOnly: true,
signed: false,
};
// Set cookie
ctx.res.cookie(‘token’, token, options);
res.send('success');
} catch (error) {
console.error(error);
}
});
Б
Б
Б
export default async function sendRequest(path, opts = {}) {
const headers = Object.assign({}, opts.headers || {}, {
'Content-type': 'application/json; charset=UTF-8'
});
const response = await fetch(
`${API}${path}`,
Object.assign({ method: 'POST', credentials: 'same-origin' }, opts, {
headers
})
);
const data = await response.json();
if (data.error) {
throw new Error(data.error);
}
return data;
}
чтобы потом разбивать по файликам и делать что-то типа
import sendRequest from './senRequest.js';
const BASE_PATH = '/api/v1';
export const getItems = () =>
sendRequest(`${BASE_PATH}/itemsraw/`, {
method: 'GET'
});
sendRequest(`${BASE_PATH}/login`, {
method: 'GET'
});
О
e
e
О