-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathAuthCode.ts
More file actions
61 lines (52 loc) · 1.97 KB
/
Copy pathAuthCode.ts
File metadata and controls
61 lines (52 loc) · 1.97 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
import mongoose, { Document, Schema } from 'mongoose';
import AuthUtils from '../utils/AuthUtils';
import { Model } from '../utils/constants';
import { BaseModel } from '../utils/types';
/**
* (1.01) TODO:
* - Read this interface.
* - Delete this comment once you've done so.
*/
interface IAuthCode extends BaseModel {
/**
* Phone number in which the OTP code is associated with.
*
* There should a MAXIMUM of 1 AuthCode document per phone number. For
* example, if they submit their phone number twice at the login screen,
* and they receive 2 text messages (and different codes), then only the
* latter one will be associated with this phone number.
*/
phoneNumber: string;
/**
* 6-digit OTP code that is generated randomly. This code is
* automatically generated upon creation of an AuthCode document.
*/
value: number;
}
export type AuthCodeDocument = Document<{}, {}, IAuthCode> & IAuthCode;
const authCodeSchema: Schema<AuthCodeDocument> = new Schema<AuthCodeDocument>(
/**
* (1.03) TODO:
* - Create the schema for the AuthCodes that we'll save in the database.
* - Delete this comment and the example field.
* - Add comment(s) to explain your work.
*/
{
// Here's an example of how to add a field to the schema.
phoneNumber: { required: true, type: String, unique: true },
value: { default: AuthUtils.generateOTP, required: true, type: Number }
},
{ timestamps: true }
);
/**
* (1.04) TODO:
* - Add a line of code here that will elete every document in the "AuthCode"
* collection after 5 minutes (60 seconds * 5).
* - To be very clear, the only way you're going to figure this out is by
* Googling around for the answer. The solution is one line.
* - Once you find something, add the code to this document and include a link
* to the code you found in a comment.
* */
const AuthCode: mongoose.Model<AuthCodeDocument> =
mongoose.model<AuthCodeDocument>(Model.AUTH_CODE, authCodeSchema);
export default AuthCode;