@@ -28,25 +28,10 @@ const CoCreateUser = {
2828 signUp : async function ( action ) {
2929 if ( ! action . form ) return ;
3030
31- let data = await Elements . save ( action . form ) ;
31+ let data = await Elements . getData ( action . form ) ;
3232 let user = data [ 0 ] ;
33- // data.method = 'object.create'
34- // data['array'] = array
3533
36- // data.organization_id = organization_id;
37-
38- // if (!data.object[0]._id)
39- // data.object[0]._id = Crud.ObjectId().toString();
40-
41- // let user = await Crud.send(data)
42- // Elements.setTypeValue(formEl, user)
43-
44- // const socket = Crud.socket.getSockets()
45- // if (!socket[0] || !socket[0].connected || window && !window.navigator.onLine) {
46-
47- // ToDo: remove roles handling from client and implement a serverside solution similar to lazyloader webooks processOperators
48- let key = {
49- status : "await" ,
34+ let userKey = {
5035 method : "object.create" ,
5136 array : "keys" ,
5237 object : {
@@ -55,34 +40,57 @@ const CoCreateUser = {
5540 key : user . object [ 0 ] . _id ,
5641 roles : user . object [ 0 ] . roles || [ user . object [ 0 ] [ "roles[]" ] ] ,
5742 email : user . object [ 0 ] . email ,
58- password : user . object [ 0 ] . password || btoa ( "0000" ) ,
43+ password : user . object [ 0 ] . password || window . btoa ( "0000" ) ,
5944 array : user . array
6045 }
6146 } ;
6247
63- let response = await Crud . send ( key ) ;
64- if ( response && response . object && response . object [ 0 ] ) {
65- // Crud.socket.send({ method: 'signUp', user, userKey })
66-
67- render ( {
68- selector : "[template*='signUp']" ,
69- data : [
70- {
71- type : "signUp" ,
72- message : "Succesfully Signed Up" ,
73- success : true
74- }
75- ]
48+ const socket = await Crud . socket . getSockets ( ) ;
49+ if (
50+ ! socket [ 0 ] ||
51+ ! socket [ 0 ] . connected ||
52+ ( window && ! window . navigator . onLine ) ||
53+ Crud . socket . serverOrganization == false
54+ ) {
55+ user . method = "object.create" ;
56+ await Crud . send ( user ) ;
57+ let response = await Crud . send ( userKey ) ;
58+ if ( response && response . object && response . object [ 0 ] ) {
59+ this . signUpResponse ( response , action ) ;
60+ }
61+ } else {
62+ let request = {
63+ method : "signUp" ,
64+ user,
65+ userKey,
66+ broadcastBrowser : false
67+ } ;
68+
69+ Crud . socket . send ( request ) . then ( ( response ) => {
70+ this . signUpResponse ( response , action ) ;
7671 } ) ;
77-
78- action . element . dispatchEvent (
79- new CustomEvent ( "signUp" , {
80- detail : response
81- } )
82- ) ;
8372 }
8473 } ,
8574
75+ signUpResponse : function ( response , action ) {
76+ render ( {
77+ selector : "[template*='signUp']" ,
78+ data : [
79+ {
80+ type : "signUp" ,
81+ message : "Succesfully Signed Up" ,
82+ success : true
83+ }
84+ ]
85+ } ) ;
86+
87+ action . element . dispatchEvent (
88+ new CustomEvent ( "signUp" , {
89+ detail : response
90+ } )
91+ ) ;
92+ } ,
93+
8694 signIn : async function ( action ) {
8795 if ( ! action . form ) return ;
8896 let query = { } ;
@@ -117,6 +125,7 @@ const CoCreateUser = {
117125 Crud . send ( request ) . then ( ( response ) => {
118126 response [ "success" ] = false ;
119127 response [ "status" ] = "signIn failed" ;
128+ response [ "message" ] = "offline" ;
120129 if ( response . object && response . object [ 0 ] ) {
121130 response [ "success" ] = true ;
122131 response [ "status" ] = "success" ;
@@ -145,13 +154,9 @@ const CoCreateUser = {
145154 localStorage . setItem ( "token" , token ) ;
146155 message = "Successful signIn" ;
147156 Crud . socket . user_id = user_id ;
148-
149- action . element . dispatchEvent (
150- new CustomEvent ( "signIn" , {
151- detail : { }
152- } )
153- ) ;
154- } else message = "The email or password you entered is incorrect" ;
157+ } else if ( ! message ) {
158+ message = "The email or password you entered is incorrect" ;
159+ }
155160
156161 render ( {
157162 selector : "[template*='signIn']" ,
@@ -164,6 +169,13 @@ const CoCreateUser = {
164169 }
165170 ]
166171 } ) ;
172+
173+ action . element . dispatchEvent (
174+ new CustomEvent ( "signIn" , {
175+ detail : { }
176+ } )
177+ ) ;
178+
167179 } ,
168180
169181 signOut : ( action ) => {
@@ -206,23 +218,25 @@ const CoCreateUser = {
206218 } ,
207219
208220 redirect : ( data ) => {
221+ const userStatus = data . userStatus ;
222+
209223 if (
210224 ( data . user_id && data . user_id !== Crud . socket . user_id ) ||
211225 ( data . clientId && data . clientId !== Crud . socket . clientId )
212226 )
213227 return ;
214228
215229 let redirectTag ;
216- if ( data . userStatus == "on" || data . userStatus == "idle" ) {
230+ if ( userStatus == "on" || userStatus == "idle" ) {
217231 redirectTag = document . querySelector ( '[session="true"]' ) ;
218- } else if ( data . userStatus == "off" ) {
232+ } else if ( userStatus == "off" ) {
219233 redirectTag = document . querySelector ( '[session="false"]' ) ;
220234 }
221235
222236 if ( redirectTag ) {
223237 let redirectLink = redirectTag . getAttribute ( "href" ) ;
224238 if ( redirectLink ) {
225- if ( data . userStatus == "off" ) {
239+ if ( userStatus == "off" ) {
226240 localStorage . removeItem ( "user_id" ) ;
227241 localStorage . removeItem ( "token" ) ;
228242 }
@@ -243,12 +257,12 @@ const CoCreateUser = {
243257 }
244258 }
245259
246- if ( data . userStatus ) {
260+ if ( userStatus ) {
247261 let sessionElements = document . querySelectorAll (
248262 '[session]:not([session="true"], [session="false"])'
249263 ) ;
250264 for ( let i = 0 ; i < sessionElements . length ; i ++ )
251- sessionElements [ i ] . setAttribute ( "session" , data . userStatus ) ;
265+ sessionElements [ i ] . setAttribute ( "session" , userStatus ) ;
252266 }
253267 } ,
254268
0 commit comments