|
96 | 96 | <script> |
97 | 97 | Polymer({ |
98 | 98 | is: 'x-main', |
99 | | - |
100 | 99 | hasPermission: false, |
101 | 100 | connectedStatus: false, |
102 | 101 | cssVals: {}, |
|
214 | 213 | * page. |
215 | 214 | */ |
216 | 215 | initUserSession: function () { |
217 | | - this.userId = window.localStorage.getItem("userId"); |
218 | 216 | this.uuid = window.localStorage.getItem("uuid"); |
219 | | - console.log(this.userId); |
220 | 217 | console.log(this.uuid); |
221 | 218 |
|
222 | | - var handleUserReady = function () { |
223 | | - this.updateUserHeaderPermissions(function () { |
224 | | - this.updatePermission(); |
225 | | - }.bind(this)); |
226 | | - }.bind(this); |
227 | | - |
228 | | - // Create user |
229 | | - if ( ! this.userId || ! this.uuid) { |
| 219 | + // Get or create user |
| 220 | + if ( ! this.uuid) { |
230 | 221 | this.uuid = Meteor.uuid(); |
231 | 222 | Meteor.call("createUser", { |
232 | | - username: this.uuid, |
233 | | - password: this.uuid, |
234 | | - login: true |
235 | | - }, function (error, result) { |
236 | | - console.log(result); |
237 | | - if (error) { |
238 | | - console.log(error); |
239 | | - } |
240 | | - this.userId = result.id; |
241 | | - window.localStorage.setItem("userId", this.userId); |
242 | | - window.localStorage.setItem("uuid", this.uuid); |
243 | | - handleUserReady() |
244 | | - }.bind(this) |
245 | | - ); |
246 | | - } else { |
247 | | - Meteor.loginWithPassword(this.uuid, this.uuid, function () { |
248 | | - handleUserReady() |
| 223 | + username: this.uuid, |
| 224 | + password: this.uuid, |
| 225 | + login: true |
| 226 | + }, function (error, result) { |
| 227 | + if (error) { |
| 228 | + console.log(error); |
| 229 | + } |
| 230 | + window.localStorage.setItem("uuid", this.uuid); |
| 231 | + // There's an issue where `login: true` isn't doing the trick |
| 232 | + // When running in Sandstorm iframe, so we force a login. |
| 233 | + if ( ! Meteor.userId()) { |
| 234 | + Meteor.loginWithPassword(this.uuid, this.uuid) |
| 235 | + } |
249 | 236 | }.bind(this)); |
| 237 | + } else { |
| 238 | + Meteor.loginWithPassword(this.uuid, this.uuid) |
250 | 239 | } |
251 | 240 |
|
| 241 | + var self = this; |
| 242 | + Tracker.autorun(function () { |
| 243 | + var userId = Meteor.userId(); |
| 244 | + self.handleUserReady(); |
| 245 | + }); |
| 246 | + |
252 | 247 | Tracker.autorun(function () { |
253 | 248 | if (Meteor.status().connected) { |
254 | 249 | console.log("connected"); |
|
257 | 252 | console.log("disconnected"); |
258 | 253 | console.log(Meteor.userId()); |
259 | 254 | } |
260 | | - }.bind(this)); |
| 255 | + }); |
| 256 | + }, |
261 | 257 |
|
| 258 | + handleUserReady: function () { |
| 259 | + console.log('handleUserReady'); |
| 260 | + this.updateUserHeaderPermissions(function () { |
| 261 | + this.updatePermission(); |
| 262 | + }.bind(this)); |
262 | 263 | }, |
263 | 264 |
|
264 | 265 | /** |
|
274 | 275 | * Makes a call to server to update this.hasPermission |
275 | 276 | */ |
276 | 277 | updatePermission: function () { |
277 | | - Meteor.call("checkUserPermissions", this.userId, ["owner", "modify"], function (error, result) { |
| 278 | + Meteor.call("checkUserPermissions", Meteor.userId(), ["owner", "modify"], function (error, result) { |
278 | 279 | this.hasPermission = result; |
279 | 280 | }.bind(this)); |
280 | 281 | }, |
281 | 282 |
|
282 | 283 | /*********************************************************************/ |
283 | | - |
284 | | - |
285 | 284 | }); |
286 | 285 |
|
287 | 286 | </script> |
0 commit comments