Skip to content

Commit

Permalink
Update user model and routes, and fix login functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
etienne committed Feb 16, 2024
1 parent dd94dc6 commit bf8c471
Show file tree
Hide file tree
Showing 7 changed files with 257 additions and 8 deletions.
2 changes: 2 additions & 0 deletions backend/src/models/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ interface IUser extends Document {
email: string;
password: string;
createdAt: Date;
userId: string;
}

interface IUserModel extends Model<IUser> {
Expand All @@ -16,6 +17,7 @@ const userSchema = new Schema<IUser>({
email: { type: String, required: true, unique: true },
password: { type: String, required: true },
createdAt: { type: Date, default: Date.now },
userId: { type: String, required: true },
});

userSchema.statics.findAll = function (): Promise<IUser[]> {
Expand Down
7 changes: 5 additions & 2 deletions backend/src/routes/userRoutes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ router.post("/register", async (req: Request, res: Response) => {
email,
password: hashedPassword,
createdAt: new Date(),
userId: Math.random().toString(36).substring(2) + Date.now().toString(36),
});

res.status(201).json(newUser);
Expand Down Expand Up @@ -80,11 +81,13 @@ router.post("/login", async (req: Request, res: Response) => {
return res.status(401).json({ message: "Invalid password" });
}
const token = jwt.sign(
{ id: user?.id, email: user?.username },
{ id: user?.userId, email: user?.username },
process.env.JWT_SECRET || "",
{ expiresIn: "7d" }
);
res.status(200).json({ message: "Login successful", token });
res
.status(200)
.json({ message: "Login successful", token, userId: user.userId });
} catch (error: any) {
res.status(500).json({ message: error.message });
}
Expand Down
240 changes: 240 additions & 0 deletions frontend/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit bf8c471

Please sign in to comment.