diff --git a/src/controllers/users.ts b/src/controllers/users.ts index aa57735..88ed8de 100644 --- a/src/controllers/users.ts +++ b/src/controllers/users.ts @@ -17,6 +17,7 @@ export const createUser = async (req: Request, res: Response) => { name, company, language, + signInType: "Email", }); if (newUser && newUser.verify) { await sendVerificationEmail(email, newUser.verify.token); @@ -231,6 +232,7 @@ export const loginUserWithGoogle = async (req: Request, res: Response) => { company: "Google", picture, language, + signInType: "Google", }); const user = await UserModel.findOne({ email }); if (user) return res.status(200).send({ jwt: user.getAuthToken(true) }); @@ -263,6 +265,7 @@ export const loginUserWithWallet = async (req: Request, res: Response) => { name: name || `${address.slice(0, 5)}...${address.slice(-5)}`, company: "Web3", language, + signInType: "Web3", }); const user = await UserModel.findOne({ email: addr }); if (user) return res.status(200).send({ jwt: user.getAuthToken(true) }); diff --git a/src/models/User.ts b/src/models/User.ts index 3952011..273ebaf 100644 --- a/src/models/User.ts +++ b/src/models/User.ts @@ -45,6 +45,9 @@ class User extends BaseModel implements UserInfo { @prop({ required: false, type: Date }) public lastActivity: Date; + @prop({ required: true, enum: ["Email", "Google", "Web3"], type: String, default: "Email" }) + public signInType: string; + public static async hashPassword(password: string): Promise { try { return new Promise((resolve, reject) => { @@ -69,6 +72,7 @@ class User extends BaseModel implements UserInfo { company, picture, isAdmin = false, + signInType, }: { email: string; password: string; @@ -77,6 +81,7 @@ class User extends BaseModel implements UserInfo { company: string; picture?: string; isAdmin?: boolean; + signInType: "Email" | "Google" | "Web3"; }, ): Promise { try { @@ -84,6 +89,16 @@ class User extends BaseModel implements UserInfo { if (exists) { throw "User exists"; } + + // We do not need to add verification for users from Google and Web3/Wallet + let verify; + if (signInType === "Email") { + verify = { + token: crypto.randomBytes(16).toString("hex"), + date: new Date(), + }; + } + const user = await UserModel.create({ email: email.toLowerCase(), password: await this.hashPassword(password || crypto.randomBytes(16).toString("hex")), @@ -92,12 +107,11 @@ class User extends BaseModel implements UserInfo { company, picture, isAdmin: isAdmin || false, - verify: { - token: crypto.randomBytes(16).toString("hex"), - date: new Date(), - }, + verify, lastActivity: new Date(), + signInType, }); + return { id: user._id, email: user.email, diff --git a/tests/progress.test.ts b/tests/progress.test.ts index 7a1c891..9935c5f 100644 --- a/tests/progress.test.ts +++ b/tests/progress.test.ts @@ -65,6 +65,7 @@ describe("Setting API Server up...", () => { company: "company", picture: "Base64OrLink", isAdmin: false, + signInType: "Email", }); lesson1 = await LessonModel.create({ diff --git a/tests/users.test.ts b/tests/users.test.ts index 5ec4e91..c428394 100644 --- a/tests/users.test.ts +++ b/tests/users.test.ts @@ -75,6 +75,7 @@ describe("Setting API Server up...", () => { company: "company", picture: "Base64OrLink", isAdmin: false, + signInType: "Email", }); await axios .get(`${API_URL}/users/${user?.id}`) @@ -97,6 +98,7 @@ describe("Setting API Server up...", () => { company: "company", picture: "Base64OrLink", isAdmin: false, + signInType: "Email", }); const newEmail = "New Email"; @@ -140,6 +142,7 @@ describe("Setting API Server up...", () => { company: "company", picture: "Base64OrLink", isAdmin: false, + signInType: "Email", }); await axios .delete(`${API_URL}/users/${user?.id}`)