Skip to content

Commit 16d6235

Browse files
committed
Defined function to find user
1 parent 2788db3 commit 16d6235

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed

src/db/models/users.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,25 @@ var userSchema = new mongoose.Schema({
4949
}
5050
});
5151

52+
//
53+
// ─── DEFINE THE FUNCTION TO FIND USER ───────────────────────────────────────────
54+
//
55+
56+
userSchema.statics.findByCredentials = async (email, password) => {
57+
var user = await User.findOne({email});
58+
if (!user) {
59+
throw new Error("Unable to login!");
60+
}
61+
62+
var match = await bcrypt.compare(password,user.password);
63+
64+
if(!match){
65+
throw new Error("Unable to login!");
66+
}
67+
68+
return user;
69+
}
70+
5271
//
5372
// ─── PASSWORD HASHING ───────────────────────────────────────────────────────────
5473
//

src/routers/user.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,20 @@ router.post("/users", async (req, res)=>{
5252

5353
// ────────────────────────────────────────────────────────────────────────────────
5454

55+
//
56+
// ─── LOGIN ──────────────────────────────────────────────────────────────────────
57+
//
58+
59+
router.post('/users/login',async (req, res) => {
60+
try {
61+
var user = await User.findByCredentials(req.body.email, req.body.password);
62+
} catch (e) {
63+
64+
}
65+
})
66+
67+
// ────────────────────────────────────────────────────────────────────────────────
68+
5569
//
5670
// ─── PATCH ROUTE ────────────────────────────────────────────────────────────────
5771
//

0 commit comments

Comments
 (0)