diff --git a/app/services/userService.js b/app/services/userService.js index 8c92dc5..e2c3bf7 100644 --- a/app/services/userService.js +++ b/app/services/userService.js @@ -10,10 +10,24 @@ core.service("UserService", function ($q, StorageService, User, WsApi) { UserService.fetchUser = function () { userEvents.notify('FETCH'); + return WsApi.fetch(currentUser.getMapping().instantiate).then(function (res) { - delete sessionStorage.role; - var credentials = angular.fromJson(res.body).payload.Credentials; - currentUser.anonymous = credentials.role === appConfig.anonymousRole ? true : false; + var body = !!res && !!res.body ? angular.fromJson(res.body) : {}; + var credentials = { role: !!currentUser.role ? currentUser.role : appConfig.anonymousRole }; + + // Only change credentials when packet structure is valid. + if (!!body && !!body.payload && !!body.payload.Credentials) { + delete sessionStorage.role; + credentials = angular.fromJson(res.body).payload.Credentials; + } + + currentUser.anonymous = !credentials.role || credentials.role === appConfig.anonymousRole; + + // Cannot have a token for the anonymous role. + if (currentUser.anonymous) { + StorageService.delete("token"); + } + angular.extend(currentUser, credentials); StorageService.set("role", currentUser.role); userEvents.notify('RECEIVED');