11const getState = ( { getStore, getActions, setStore } ) => {
22 return {
33 store : {
4+ user : null ,
5+ token : null ,
46 message : null ,
57 demo : [
68 {
@@ -24,6 +26,95 @@ const getState = ({ getStore, getActions, setStore }) => {
2426 getActions ( ) . changeColor ( 0 , "green" ) ;
2527 } ,
2628
29+ login : async ( email , password , navigate ) => {
30+ try {
31+ const resp = await fetch ( `${ process . env . BACKEND_URL } api/login/user` , {
32+ method : "POST" ,
33+ headers : {
34+ "Content-Type" : "application/json"
35+ } ,
36+ body : JSON . stringify ( { email, password } )
37+ } ) ;
38+
39+ if ( ! resp . ok ) {
40+ throw new Error ( "Error al iniciar sesión" ) ;
41+ }
42+
43+ const data = await resp . json ( ) ;
44+ console . log ( "Inicio de sesión exitoso:" , data ) ;
45+
46+ const token = data . token ;
47+ if ( ! token ) {
48+ throw new Error ( "No se recibió el token" ) ;
49+ }
50+
51+ localStorage . setItem ( "token" , token ) ;
52+ setStore ( { token } ) ;
53+
54+ const actions = getActions ( ) ;
55+ actions . getUser ( ) ;
56+ navigate ( "/" ) ;
57+ } catch ( error ) {
58+ console . log ( "Error al iniciar sesión" , error ) ;
59+ alert ( "Error al iniciar sesión" ) ;
60+ }
61+ } ,
62+ signup : async ( dataUser , navigate ) => {
63+ try {
64+ const resp = await fetch ( `${ process . env . BACKEND_URL } api/signup` , {
65+ method : "POST" ,
66+ headers : {
67+ "Content-Type" : "application/json"
68+ } ,
69+ body : JSON . stringify ( dataUser )
70+ } ) ;
71+
72+ if ( ! resp . ok ) {
73+ throw new Error ( "Error en el registro" ) ;
74+ }
75+
76+ const data = await resp . json ( ) ;
77+ console . log ( "Usuario registrado exitosamente" , data ) ;
78+
79+ const token = data . token ;
80+ if ( ! token ) {
81+ throw new Error ( "No se recibió el token" ) ;
82+ }
83+
84+ localStorage . setItem ( "token" , token ) ;
85+ setStore ( { token } ) ;
86+
87+ const actions = getActions ( ) ;
88+ actions . getUser ( ) ;
89+ navigate ( "/" ) ;
90+ } catch ( error ) {
91+ }
92+ } ,
93+ getUser : async ( ) => {
94+ try {
95+ const token = localStorage . getItem ( "token" ) ;
96+ if ( ! token ) throw new Error ( "No token found" ) ;
97+
98+ const resp = await fetch ( `${ process . env . BACKEND_URL } api/user` , {
99+ headers : {
100+ "Authorization" : `Bearer ${ token } `
101+ }
102+ } ) ;
103+
104+ if ( ! resp . ok ) throw new Error ( "Error al obtener el usuario" ) ;
105+
106+ const data = await resp . json ( ) ;
107+ setStore ( { user : data } ) ;
108+ } catch ( error ) {
109+ console . log ( "Error al obtener usuario" , error ) ;
110+ }
111+ } ,
112+ logout : ( ) => {
113+ localStorage . removeItem ( 'token' ) ;
114+ setStore ( { token : null , user : null } )
115+
116+ } ,
117+
27118 getDogFood : async ( ) => {
28119 const myHeaders = new Headers ( ) ;
29120 myHeaders . append ( "Cookie" , ".Tunnels.Relay.WebForwarding.Cookies=CfDJ8Cs4yarcs6pKkdu0hlKHsZs0q_CxPIxRcsYOazLvQz4rP7s5FWFmvGJndFqy0N7fvoY5B6Jou5i4ZPgwsQZsEYGDV4DoaNhJP3xwIvv1aGmoRIVdScF1G2c_hWBWqeCTHFNCvGD1Dy0sm3kBmaNdXiMsSO0myHKUFvlWHCed2AdtyCiC6CHBqk9DHs32cYjJV4GQr4cxW2IXl6QDukWwCPSYuzTnP699Rz_4pCbB8OPOQNBDyDtdks_LUoMZR2Qt6IWKmUnLGt-n3JLFjeQMZiSeKEXKNTcJknrz4p25p9-5rh3BY2FBX_kg6MtH3cLbqOyS6yqrG4cjJPpyZbVfN3iEYSR6lzEGiGZDFPvokcj_PM8fq32HR1_olrhti8nYtDctNR_8YRewW5quhBNW6mtF_-SqGTbCQVH1CiLUF2UKK_H_nGmwvWAce2n4Cdw1BLUCZhlCr3GAKWJWHqLI1K5n9OekmI4zs0TI_60R6urTRxIQx2IgkRPYizg-AUdyr6bORhYr7s3c6oFBrdA4yBShqyJFOo4fuMkQuHflmg717cZeB1MDnWgSm9Xnl4qmOlcta0fCSq15GNUPvXhAwvclIoK9NTrmSSd1wvRhoqz7ypodxTSOafQx0ybhJZwTxDeS_gv-4KjbyFngwj7Bj1TluB2qE5Hijbi4uhZb2KILE9AGYHKWSW-IWoSmEXW71c7HhH8mBEbBxidpsSi_Rip3CdXL2oUO-8TGtLx2HgJtExj_7AyTD1trSanlKgDurPSEfDuXuwtqxawfWf0a1sbp9BGk0pRLOA-tVKrxmMiFAPiNCVC1W2EXb95TKydzKIwtbcC70YyDJ4dFwnHrQmgPYxzIz7PYzICzKqZ-VDpO4lf7C3jf4OBJ9ZMV4JRvPiR2kUwMNX_c5CuMLaKrZNzqCFwZBUa4N4AUyTT83mtzFQGAZjMXDeZNok7mjTYBp121qquiSKX8ft05b1MTsVtRfg3ETiFdOHmYQ2-1EcSwBY-VhjWIlkQiFr1yBWXk-g" ) ;
@@ -35,7 +126,7 @@ const getState = ({ getStore, getActions, setStore }) => {
35126 } ;
36127
37128 try {
38- const response = await fetch ( "https://silver-meme-jj4x947jg76ghqjg9-3001.app.github.dev /api/foods/dog", requestOptions ) ;
129+ const response = await fetch ( process . env . BACKEND_URL + " /api/foods/dog", requestOptions ) ;
39130
40131 if ( ! response . ok ) {
41132 throw new Error ( 'Network response was not ok' ) ;
@@ -65,7 +156,7 @@ const getState = ({ getStore, getActions, setStore }) => {
65156 } ;
66157
67158 try {
68- const response = await fetch ( "https://silver-meme-jj4x947jg76ghqjg9-3001.app.github.dev /api/foods/cat", requestOptions ) ;
159+ const response = await fetch ( process . env . BACKEND_URL + " /api/foods/cat", requestOptions ) ;
69160 if ( ! response . ok ) {
70161 throw new Error ( 'Network response was not ok' ) ;
71162 }
@@ -90,7 +181,7 @@ const getState = ({ getStore, getActions, setStore }) => {
90181 } ;
91182
92183 try {
93- const response = await fetch ( "https://silver-meme-jj4x947jg76ghqjg9-3001.app.github.dev /api/foods/exotic", requestOptions ) ;
184+ const response = await fetch ( process . env . BACKEND_URL + " /api/foods/exotic", requestOptions ) ;
94185 if ( ! response . ok ) {
95186 throw new Error ( 'Network response was not ok' ) ;
96187 }
@@ -139,4 +230,4 @@ const getState = ({ getStore, getActions, setStore }) => {
139230 } ;
140231} ;
141232
142- export default getState ;
233+ export default getState ;
0 commit comments