-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUser.model.ts
62 lines (50 loc) · 1.75 KB
/
User.model.ts
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
import mongoose from 'mongoose';
export interface UserData {
_id: mongoose.Types.ObjectId,
username: string,
email: string,
password: string,
// Administrators can lock users' accounts via admin panel
locked: boolean,
// Someone is online if lastLogin > lastLogout
lastLogin: Date,
lastLogout: Date,
}
export interface ClientUserData {
_id: mongoose.Types.ObjectId,
username: string,
// Administrators can lock users' accounts via admin panel
locked: boolean,
// Someone is online if lastLogin > lastLogout
lastLogin: Date,
lastLogout: Date,
}
export const UNAVAILABLE_USER: ClientUserData = {
_id: new mongoose.Types.ObjectId('000000000000000000000000'),
username: 'user_unavailable',
locked: false,
lastLogin: new Date(),
lastLogout: new Date(),
}
// Create a schema using the interface
const userSchema = new mongoose.Schema<UserData>({
username: { type: String, required: true },
email: { type: String, required: true },
password: { type: String, required: true },
// Auth checks locked status at login; must be false to authenticate
locked: { type: Boolean, required: true, default: false },
lastLogin: { type: Date, required: true, default: Date.now() },
// lastLogout defaults to before the creation of the app; users who just created an account will show as online
lastLogout: { type: Date, required: true, default: new Date('January 1, 2000 00:00:00') },
});
// Create a model using the schema and interface
export const User = mongoose.model<UserData>('User', userSchema);
export const sanitizeUserData = (user: UserData | ClientUserData): ClientUserData => {
return {
_id: user._id,
username: user.username,
locked: user.locked,
lastLogin: user.lastLogin,
lastLogout: user.lastLogout,
};
}