Skip to content

Commit

Permalink
Merge pull request #58 from PolkadotEducation/develop
Browse files Browse the repository at this point in the history
Remove verify from Google/Web3 users. (#57)
  • Loading branch information
arturgontijo authored Oct 29, 2024
2 parents 91ac34c + cd7ac40 commit a13f75c
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 4 deletions.
3 changes: 3 additions & 0 deletions src/controllers/users.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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) });
Expand Down Expand Up @@ -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) });
Expand Down
22 changes: 18 additions & 4 deletions src/models/User.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string> {
try {
return new Promise((resolve, reject) => {
Expand All @@ -69,6 +72,7 @@ class User extends BaseModel implements UserInfo {
company,
picture,
isAdmin = false,
signInType,
}: {
email: string;
password: string;
Expand All @@ -77,13 +81,24 @@ class User extends BaseModel implements UserInfo {
company: string;
picture?: string;
isAdmin?: boolean;
signInType: "Email" | "Google" | "Web3";
},
): Promise<UserInfo | undefined> {
try {
const exists = await this.findOne({ email: email.toLowerCase() });
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")),
Expand All @@ -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,
Expand Down
1 change: 1 addition & 0 deletions tests/progress.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ describe("Setting API Server up...", () => {
company: "company",
picture: "Base64OrLink",
isAdmin: false,
signInType: "Email",
});

lesson1 = await LessonModel.create({
Expand Down
3 changes: 3 additions & 0 deletions tests/users.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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}`)
Expand All @@ -97,6 +98,7 @@ describe("Setting API Server up...", () => {
company: "company",
picture: "Base64OrLink",
isAdmin: false,
signInType: "Email",
});

const newEmail = "New Email";
Expand Down Expand Up @@ -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}`)
Expand Down

0 comments on commit a13f75c

Please sign in to comment.