-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuserDatabase.js
More file actions
136 lines (112 loc) · 3.75 KB
/
Copy pathuserDatabase.js
File metadata and controls
136 lines (112 loc) · 3.75 KB
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
import AsyncStorage from '@react-native-async-storage/async-storage';
const USERS_STORAGE_KEY = 'safera_users';
const SESSION_STORAGE_KEY = 'safera_active_user';
const normalizeMobile = (mobile) => mobile.replace(/\D/g, '');
const normalizeUsername = (username) => username.trim().toLowerCase();
async function readUsers() {
const raw = await AsyncStorage.getItem(USERS_STORAGE_KEY);
if (!raw) return [];
try {
const parsed = JSON.parse(raw);
return Array.isArray(parsed) ? parsed : [];
} catch {
return [];
}
}
async function writeUsers(users) {
await AsyncStorage.setItem(USERS_STORAGE_KEY, JSON.stringify(users));
}
export async function registerUser({
username,
mobile,
password,
profileName,
email,
emergencyContact1,
emergencyContact2,
}) {
const users = await readUsers();
const normalizedMobile = normalizeMobile(mobile);
const normalizedUsername = normalizeUsername(username);
const existingUser = users.find(
(user) =>
normalizeMobile(user.mobile) === normalizedMobile ||
normalizeUsername(user.username) === normalizedUsername
);
if (existingUser) {
throw new Error('An account with that username or mobile number already exists.');
}
const newUser = {
id: `${Date.now()}`,
username: username.trim(),
mobile: normalizedMobile,
password,
profileName: profileName?.trim() || '',
email: email?.trim() || '',
emergencyContact1: normalizeMobile(emergencyContact1 || ''),
emergencyContact2: normalizeMobile(emergencyContact2 || ''),
createdAt: new Date().toISOString(),
};
const nextUsers = [...users, newUser];
await writeUsers(nextUsers);
await AsyncStorage.setItem(SESSION_STORAGE_KEY, JSON.stringify(newUser));
return newUser;
}
export async function loginUser({ identifier, password }) {
const users = await readUsers();
const normalizedIdentifier = identifier.trim().toLowerCase();
const mobileIdentifier = normalizeMobile(identifier);
const user = users.find(
(entry) =>
normalizeUsername(entry.username) === normalizedIdentifier ||
normalizeMobile(entry.mobile) === mobileIdentifier
);
if (!user || user.password !== password) {
throw new Error('Invalid username/mobile or password.');
}
await AsyncStorage.setItem(SESSION_STORAGE_KEY, JSON.stringify(user));
return user;
}
export async function getActiveUser() {
const raw = await AsyncStorage.getItem(SESSION_STORAGE_KEY);
if (!raw) return null;
try {
return JSON.parse(raw);
} catch {
return null;
}
}
export async function logoutUser() {
await AsyncStorage.removeItem(SESSION_STORAGE_KEY);
}
export async function updateActiveUserProfile(updates) {
const activeUser = await getActiveUser();
if (!activeUser) {
throw new Error('No active user found.');
}
const users = await readUsers();
const nextUser = {
...activeUser,
...updates,
profileName: updates.profileName?.trim() ?? activeUser.profileName ?? '',
email: updates.email?.trim() ?? activeUser.email ?? '',
emergencyContact1:
updates.emergencyContact1 !== undefined
? normalizeMobile(updates.emergencyContact1)
: activeUser.emergencyContact1 ?? '',
emergencyContact2:
updates.emergencyContact2 !== undefined
? normalizeMobile(updates.emergencyContact2)
: activeUser.emergencyContact2 ?? '',
};
const nextUsers = users.map((user) => (user.id === activeUser.id ? nextUser : user));
await writeUsers(nextUsers);
await AsyncStorage.setItem(SESSION_STORAGE_KEY, JSON.stringify(nextUser));
return nextUser;
}
export async function deleteAccount(userId) {
const users = await readUsers();
const filtered = users.filter((u) => u.id !== userId);
await writeUsers(filtered);
await AsyncStorage.removeItem(SESSION_STORAGE_KEY);
}