|
92 | 92 | * [getUui - generate a unique id]
|
93 | 93 | * @return {[string]} [a unique id]
|
94 | 94 | */
|
95 |
| - function getUui() { |
| 95 | + function _getUui() { |
96 | 96 | var navigator_info = window.navigator;
|
97 | 97 | var screen_info = window.screen;
|
98 | 98 | var uid = navigator_info.mimeTypes.length;
|
|
107 | 107 | }
|
108 | 108 |
|
109 | 109 | app.helpers.join = function(data) {
|
| 110 | + var userRequiredParams = { |
| 111 | + 'login': _getUui(), |
| 112 | + 'password': 'webAppPass' |
| 113 | + }; |
| 114 | + |
110 | 115 | return new Promise(function(resolve, reject) {
|
111 |
| - QB.createSession(function(csError, csResult){ |
112 |
| - if(csError) { |
113 |
| - reject(csError); |
| 116 | + QB.createSession(function(csErr, csRes){ |
| 117 | + if(csErr) { |
| 118 | + reject(csErr); |
114 | 119 | } else {
|
115 |
| - /** Login */ |
116 |
| - QB.login({ |
117 |
| - 'login': getUui(), |
118 |
| - 'password': 'webAppPass' |
119 |
| - }, |
120 |
| - function(egError, egUser){ |
121 |
| - /** User not found */ |
122 |
| - if(egError) { |
123 |
| - QB.users.create({ |
124 |
| - 'login': getUui(), |
125 |
| - 'password': 'webAppPass', |
126 |
| - 'full_name': data.username, |
127 |
| - 'tag_list': data.room |
128 |
| - }, |
129 |
| - function(ucError, ucUser){ |
130 |
| - if(ucError) { |
131 |
| - console.log('[create user] Error:', ucError); |
132 |
| - reject(ucError); |
133 |
| - } else { |
134 |
| - console.log('[create user] User:', ucUser); |
135 |
| - resolve(ucUser); |
136 |
| - } |
137 |
| - } |
138 |
| - ); |
139 |
| - } else { |
140 |
| - /** Update info */ |
141 |
| - if(egUser.user_tags !== data.room || egUser.full_name !== data.username ) { |
142 |
| - QB.users.update(egUser.id, { |
143 |
| - 'full_name': data.username, |
144 |
| - 'tag_list': data.room |
145 |
| - }, function(uuError, uuUser) { |
146 |
| - if(uuError) { |
147 |
| - console.log('APP [update user] Error:', uuError); |
148 |
| - reject(uuError); |
| 120 | + /** In first trying to login */ |
| 121 | + QB.login(userRequiredParams, function(loginErr, loginUser){ |
| 122 | + if(loginErr) { |
| 123 | + /** Login failed, trying to create account */ |
| 124 | + QB.users.create({ |
| 125 | + 'login': _getUui(), |
| 126 | + 'password': 'webAppPass', |
| 127 | + 'full_name': data.username, |
| 128 | + 'tag_list': data.room |
| 129 | + }, function(createErr, createUser){ |
| 130 | + if(createErr) { |
| 131 | + console.log('[create user] Error:', createErr); |
| 132 | + reject(createErr); |
| 133 | + } else { |
| 134 | + QB.login(userRequiredParams, function(reloginErr, reloginUser) { |
| 135 | + if(reloginErr) { |
| 136 | + console.log('[relogin user] Error:', reloginErr); |
149 | 137 | } else {
|
150 |
| - console.log('APP [update user] User:', uuUser); |
151 |
| - resolve(uuUser); |
| 138 | + resolve(reloginUser); |
152 | 139 | }
|
153 | 140 | });
|
154 |
| - } else { |
155 |
| - resolve(egUser); |
156 | 141 | }
|
| 142 | + }); |
| 143 | + } else { |
| 144 | + /** Update info */ |
| 145 | + if(loginUser.user_tags !== data.room || loginUser.full_name !== data.username ) { |
| 146 | + QB.users.update(loginUser.id, { |
| 147 | + 'full_name': data.username, |
| 148 | + 'tag_list': data.room |
| 149 | + }, function(updateError, updateUser) { |
| 150 | + if(updateError) { |
| 151 | + console.log('APP [update user] Error:', updateError); |
| 152 | + reject(updateError); |
| 153 | + } else { |
| 154 | + resolve(updateUser); |
| 155 | + } |
| 156 | + }); |
| 157 | + } else { |
| 158 | + resolve(loginUser); |
157 | 159 | }
|
158 | 160 | }
|
159 |
| - ); |
| 161 | + }); |
160 | 162 | }
|
161 | 163 | });
|
162 | 164 | });
|
|
180 | 182 | }
|
181 | 183 | });
|
182 | 184 |
|
183 |
| - if(!result.items.length) { |
| 185 | + if(result.items.length < 2) { |
184 | 186 | reject({
|
185 | 187 | 'title': 'not found',
|
186 | 188 | 'message': 'Not found users by tag'
|
|
0 commit comments