So my problem is that I try to set cookies at server route, but it server route does not set it at all. Here is my code:
export default defineEventHandler(async (e) => {
const access_token : any = getCookie(e, 'access_token');
const refresh_token : any = getCookie(e, 'refresh_token');
if(typeof refresh_token === 'string') {
const authHeader = `Bearer ${access_token}`;
const readCategory = await $fetch.raw('http://localhost/admin/category', {
method: 'GET',
credentials: 'include',
headers: {
Authorization: authHeader,
'X-Refresh-Token': refresh_token
}
});
const token : any = readCategory.headers.get('token');
console.log(token);
setCookie(e, 'testing', 'asdasdasd', {path: '/', httpOnly: false, maxAge: 1800});
setCookie(e, 'access_token', token, {path: '/', httpOnly: true, maxAge: 1800});
return readCategory._data;
}
return {
status: false,
msg: 'sessionexpired'
};
});
I have other server route that uses exactly the same code, but with just that exception that it does POST request. Other route works perfectly and sets “access_token” as cookie and browser receives it. I did check client routes and I did not see any problem in them. Token does have value and it even logs it to console, but for some odd reason setCookie does not work like it should work. Route does not even set this “testing” cookie back to browser for some reason.
EDIT: Oh and I did remove logic when checking if readCategory headers had token it it. I wanted to see if that blocks it from setting it, but it was not about that. Backend returns new access token every time when client sends one that is old or if client does not have one at all.