-
Notifications
You must be signed in to change notification settings - Fork 0
/
identity.js
69 lines (57 loc) · 1.64 KB
/
identity.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import AsyncStorage from '@react-native-async-storage/async-storage';
import uuid from 'react-native-uuid';
const ANONYMOUS_ID = 'anonymousID';
const USER_ID = 'userID';
class IdentityManager {
static instance;
anonymousID = '';
userID = '';
constructor() {
}
async loadAnonymousID(callback) {
const _anonymousID = await AsyncStorage.getItem(ANONYMOUS_ID);
if (_anonymousID === null) {
this.resetIDs();
} else {
this.anonymousID = _anonymousID;
};
callback.bind(this)(this.anonymousID);
}
async loadUserID(callback) {
var _userID = await AsyncStorage.getItem(USER_ID);
if (_userID !== null) {
this.userID = _userID;
}
else{
this.userID = "";
}
callback.bind(this)( this.userID);
}
static getInstance() {
if (!IdentityManager.instance) {
IdentityManager.instance = new IdentityManager();
}
return IdentityManager.instance;
}
resetIDs() {
var anonymousID = uuid.v4();
this.updateAnonymousID(anonymousID);
this.updateUserID("");
return anonymousID;
}
async updateAnonymousID(anonymousID) {
this.anonymousID = anonymousID;
return await AsyncStorage.setItem(ANONYMOUS_ID, anonymousID.toString());
}
async updateUserID(userID) {
this.userID = userID;
return await AsyncStorage.setItem(USER_ID, userID.toString());
}
getAnonymousID() {
return this.anonymousID;
}
getUserID() {
return this.userID;
}
}
module.exports = IdentityManager