|
@@ -0,0 +1,28 @@
|
|
|
|
+import { atom } from 'nanostores';
|
|
|
|
+import { Sesion } from '../models/Sesion.model';
|
|
|
|
+
|
|
|
|
+const llave = "usuario";
|
|
|
|
+let initialUser: Sesion | undefined;
|
|
|
|
+
|
|
|
|
+if (typeof window !== 'undefined') {
|
|
|
|
+ const storedUser = localStorage.getItem(llave);
|
|
|
|
+ try {
|
|
|
|
+ const parsedUser = storedUser ? JSON.parse(storedUser) : null;
|
|
|
|
+ if(parsedUser !== null) {
|
|
|
|
+ initialUser = Sesion.fromJson(parsedUser);
|
|
|
|
+ }
|
|
|
|
+ } catch(e) { }
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+export const authStore = atom(initialUser);
|
|
|
|
+
|
|
|
|
+authStore.subscribe((nuevoUsuario) => {
|
|
|
|
+ if (typeof window === 'undefined') {
|
|
|
|
+ return;
|
|
|
|
+ }
|
|
|
|
+ if(nuevoUsuario && nuevoUsuario.id) {
|
|
|
|
+ localStorage.setItem(llave, JSON.stringify(nuevoUsuario));
|
|
|
|
+ } else {
|
|
|
|
+ localStorage.removeItem(llave);
|
|
|
|
+ }
|
|
|
|
+});
|