Skip to content

Commit ade1ea8

Browse files
committed
write test bloominstituteoftechnology#10 and build out login endpoint to satisfy
1 parent 3b77ec5 commit ade1ea8

File tree

2 files changed

+37
-2
lines changed

2 files changed

+37
-2
lines changed

api/auth/auth-router.js

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,23 @@ router.post('/register', checkUserAndPass, usernameNotTaken, (req, res, next) =>
4343
*/
4444
});
4545

46-
router.post('/login', checkUserAndPass, usernameExists, (req, res) => {
47-
res.end('implement login, please!');
46+
router.post('/login', checkUserAndPass, usernameExists, (req, res, next) => {
47+
let { username, password } = req.body;
48+
49+
Users.getBy({username})
50+
.then( user => {
51+
if(bcrypt.compareSync(password, user.password)) return res.json({
52+
message: `welcome, ${username}`,
53+
token: buildToken(user)
54+
})
55+
else next({
56+
status: 401,
57+
message: "invalid credentials"
58+
})
59+
})
60+
.catch(next);
61+
62+
4863
/*
4964
IMPLEMENT
5065
You are welcome to build additional middlewares to help with the endpoint's functionality.

api/server.test.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,4 +200,24 @@ describe("[POST] /api/auth/login", () => {
200200

201201
expect(res.body.message).toBe("invalid credentials");
202202
})
203+
204+
test(`[10] responds with "invalid credentials" if password doesn't match`, async () => {
205+
let res = await request(server)
206+
.post("/api/auth/login")
207+
.send({username: "aaron", password: "1234"})
208+
209+
expect(res.body.message).toBe("invalid credentials");
210+
211+
res = await request(server)
212+
.post("/api/auth/login")
213+
.send({username: "aaron", password: "aaron"})
214+
215+
expect(res.body.message).toBe("invalid credentials");
216+
217+
res = await request(server)
218+
.post("/api/auth/login")
219+
.send({username: "aaron", password: "password"})
220+
221+
expect(res.body.message).toBe("invalid credentials");
222+
})
203223
})

0 commit comments

Comments
 (0)