2 Commits c3999ad80f ... bb50fe576b

Author SHA1 Message Date
  yadid rosell bb50fe576b se añadio nanostores 4 months ago
  yadid rosell 286687db8a se añadieron mas actualizaciones 4 months ago
4 changed files with 49 additions and 0 deletions
  1. 1 0
      package.json
  2. 19 0
      src/models/Sesion.model.ts
  3. 28 0
      src/stores/authStore.ts
  4. 1 0
      src/stores/index.js

+ 1 - 0
package.json

@@ -21,6 +21,7 @@
     "@types/react": "^18.3.12",
     "@types/react": "^18.3.12",
     "@types/react-dom": "^18.3.1",
     "@types/react-dom": "^18.3.1",
     "astro": "^4.16.9",
     "astro": "^4.16.9",
+    "nanostores": "^0.11.3",
     "react": "^18.3.1",
     "react": "^18.3.1",
     "react-dom": "^18.3.1",
     "react-dom": "^18.3.1",
     "react-html-parser": "^2.0.2",
     "react-html-parser": "^2.0.2",

+ 19 - 0
src/models/Sesion.model.ts

@@ -0,0 +1,19 @@
+export class Sesion {
+  id?: string;
+  nombre?: string;
+  correo?: string;
+  primerApellido?: string;
+  segundoApellido?: string;
+  pin?: string;
+  clave?: string;
+  token?: string;
+  aceptoTerminos?: string;
+
+  constructor(json?: Partial<Sesion>) {
+    Object.assign(this, json);
+  }
+
+  static fromJson(json: Partial<Sesion>) {
+    return new Sesion(json);
+  }
+}

+ 28 - 0
src/stores/authStore.ts

@@ -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);
+  }
+});

+ 1 - 0
src/stores/index.js

@@ -0,0 +1 @@
+export * from "./authStore";