mirror of
https://github.com/emailerfacu-spec/minix-front.git
synced 2026-04-18 15:57:31 -03:00
añadida logica para refrescar los jwt
This commit is contained in:
@@ -1,20 +1,59 @@
|
|||||||
import { writable } from 'svelte/store';
|
import { writable } from 'svelte/store';
|
||||||
import { browser } from '$app/environment';
|
import { browser } from '$app/environment';
|
||||||
import type { Sesion } from '../../types';
|
import type { Sesion } from '../../types';
|
||||||
|
import { apiBase } from '@/stores/url';
|
||||||
|
|
||||||
|
const { subscribe } = apiBase;
|
||||||
|
let baseUrl: string = '';
|
||||||
|
|
||||||
|
subscribe((value) => {
|
||||||
|
baseUrl = value;
|
||||||
|
})();
|
||||||
|
|
||||||
const initialValue = browser ? JSON.parse(localStorage.getItem('sesion') || 'null') : null;
|
const initialValue = browser ? JSON.parse(localStorage.getItem('sesion') || 'null') : null;
|
||||||
|
|
||||||
export const currentSesion = writable<Sesion | null>(initialValue);
|
export const currentSesion = writable<Sesion | null>(initialValue);
|
||||||
|
|
||||||
if (browser) {
|
|
||||||
currentSesion.subscribe((value) => {
|
|
||||||
localStorage.setItem('sesion', JSON.stringify(value));
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
export const sesionStore = {
|
export const sesionStore = {
|
||||||
subscribe: currentSesion.subscribe,
|
subscribe: currentSesion.subscribe,
|
||||||
set: currentSesion.set,
|
set: currentSesion.set,
|
||||||
update: currentSesion.update,
|
update: currentSesion.update,
|
||||||
reset: () => currentSesion.set(null)
|
reset: () => currentSesion.set(null)
|
||||||
};
|
};
|
||||||
|
|
||||||
|
if (browser) {
|
||||||
|
currentSesion.subscribe((value) => {
|
||||||
|
localStorage.setItem('sesion', JSON.stringify(value));
|
||||||
|
});
|
||||||
|
}
|
||||||
|
if (browser) {
|
||||||
|
const refreshAccessToken = async () => {
|
||||||
|
try {
|
||||||
|
const response = await fetch(baseUrl + '/api/auth/refresh', {
|
||||||
|
method: 'POST',
|
||||||
|
headers: {
|
||||||
|
'Content-Type': 'application/json'
|
||||||
|
},
|
||||||
|
credentials: 'include'
|
||||||
|
});
|
||||||
|
|
||||||
|
if (response.ok) {
|
||||||
|
const data = await response.json();
|
||||||
|
currentSesion.update((sesion) => {
|
||||||
|
if (sesion) {
|
||||||
|
return { ...sesion, accessToken: data.accessToken };
|
||||||
|
}
|
||||||
|
return sesion;
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
console.error('Error refreshing token:', response.statusText);
|
||||||
|
currentSesion.set(null);
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
console.error('Error refreshing token:', error);
|
||||||
|
currentSesion.set(null);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
setInterval(refreshAccessToken, 10 * 60 * 1000);
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user