models/storage.ts   A
last analyzed

Complexity

Total Complexity 6
Complexity/F 0

Size

Lines of Code 58
Function Count 0

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
wmc 6
eloc 41
mnd 6
bc 6
fnc 0
dl 0
loc 58
rs 10
bpm 0
cpm 0
noi 0
c 0
b 0
f 0
1
import AsyncStorage from '@react-native-async-storage/async-storage';
2
3
const storage = {
4
    storeToken: async function storeToken(token: string): Promise<void> {        
5
        try {
6
            const tokenAndDate = {
7
                token: token,
8
                date: new Date().getTime(),
9
            };
10
            const jsonValue = JSON.stringify(tokenAndDate);
11
12
            await AsyncStorage.setItem('@token', jsonValue);
13
        } catch (e) {
14
            // save error
15
        }
16
    },
17
18
    readToken: async function readToken(): Promise<any> {
19
        try {
20
            const jsonValue = await AsyncStorage.getItem('@token');
21
            return jsonValue != null ? JSON.parse(jsonValue) : null;
22
        } catch (e) {
23
            //reading error value
24
        }
25
    },
26
27
    deleteToken: async function deleteToken(): Promise<void> {
28
        await AsyncStorage.removeItem('@token');
29
    },
30
31
    storeUser: async function storeUser(userData: object): Promise<void> {        
32
        try {
33
            const user = {
34
                userData: userData
35
            };
36
            const jsonValue = JSON.stringify(user);
37
38
            await AsyncStorage.setItem('@user', jsonValue);
39
        } catch (e) {
40
            // save error
41
        }
42
    },
43
44
    readUser: async function readUser(): Promise<any> {
45
        try {
46
            const jsonValue = await AsyncStorage.getItem('@user');
47
            return jsonValue != null ? JSON.parse(jsonValue) : null;
48
        } catch (e) {
49
            //reading error value
50
        }
51
    },
52
53
    deleteUser: async function deleteUser(): Promise<void> {
54
        await AsyncStorage.removeItem('@user');
55
    },
56
}
57
58
export default storage;