mirror of
https://github.com/seanmorley15/AdventureLog.git
synced 2025-07-27 08:49:36 +02:00
migration to new backend
This commit is contained in:
parent
28a5d423c2
commit
9abe9fb315
309 changed files with 21476 additions and 24132 deletions
36
frontend/src/lib/index.server.ts
Normal file
36
frontend/src/lib/index.server.ts
Normal file
|
@ -0,0 +1,36 @@
|
|||
const PUBLIC_SERVER_URL = process.env['PUBLIC_SERVER_URL'];
|
||||
const serverEndpoint = PUBLIC_SERVER_URL || 'http://localhost:8000';
|
||||
|
||||
export const fetchCSRFToken = async () => {
|
||||
const csrfTokenFetch = await fetch(`${serverEndpoint}/csrf/`);
|
||||
if (csrfTokenFetch.ok) {
|
||||
const csrfToken = await csrfTokenFetch.json();
|
||||
return csrfToken.csrfToken;
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
};
|
||||
|
||||
export const tryRefreshToken = async (refreshToken: string) => {
|
||||
const csrfToken = await fetchCSRFToken();
|
||||
const refreshFetch = await fetch(`${serverEndpoint}/auth/token/refresh/`, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'X-CSRFToken': csrfToken,
|
||||
'Content-Type': 'application/json' // Corrected header name
|
||||
},
|
||||
body: JSON.stringify({ refresh: refreshToken })
|
||||
});
|
||||
|
||||
if (refreshFetch.ok) {
|
||||
const refresh = await refreshFetch.json();
|
||||
const token = `auth=${refresh.access}`;
|
||||
return token;
|
||||
// event.cookies.set('auth', `auth=${refresh.access}`, {
|
||||
// httpOnly: true,
|
||||
// sameSite: 'lax',
|
||||
// expires: new Date(Date.now() + 60 * 60 * 1000), // 60 minutes
|
||||
// path: '/'
|
||||
// });
|
||||
}
|
||||
};
|
Loading…
Add table
Add a link
Reference in a new issue