1
0
mirror of https://github.com/bitwarden/browser.git synced 2024-10-19 07:35:48 +02:00
bitwarden-browser/src/services/appIdService.js

37 lines
1.0 KiB
JavaScript
Raw Normal View History

2017-10-17 15:25:22 +02:00
function AppIdService(utilsService) {
this.utilsService = utilsService;
initAppIdService();
2017-07-14 21:34:05 +02:00
}
function initAppIdService() {
2017-10-17 15:25:22 +02:00
AppIdService.prototype.getAppId = function () {
return makeAndGetAppId('appId', this);
};
2017-10-17 15:25:22 +02:00
AppIdService.prototype.getAnonymousAppId = function () {
return makeAndGetAppId('anonymousAppId', this);
};
2017-10-17 15:25:22 +02:00
function makeAndGetAppId(key, self) {
return self.utilsService.getObjFromStorage(key).then(function (obj) {
if (obj) {
return obj;
}
2017-10-17 15:25:22 +02:00
var guid = newGuid();
return self.utilsService.saveObjToStorage(key, guid).then(function () {
return guid;
});
});
}
// ref: http://stackoverflow.com/a/2117523/1090359
function newGuid() {
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
var r = Math.random() * 16 | 0, v = c === 'x' ? r : (r & 0x3 | 0x8);
return v.toString(16);
});
}
2017-07-14 21:34:05 +02:00
}