-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add POST /api/v1/users/sign-in route
- Loading branch information
Showing
9 changed files
with
197 additions
and
65 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import express from "express" | ||
|
||
import usersRouter from "./routes/users"; | ||
|
||
const router = express.Router(); | ||
|
||
router.use("/users", usersRouter); | ||
|
||
export default router; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import express from "express"; | ||
import { createSession } from "../../models/Session.model"; | ||
|
||
import User, { getUserByPassword, updateUser } from "../../models/User.model"; | ||
|
||
|
||
const router = express.Router(); | ||
|
||
// Middleware to check if a password in passed in the body | ||
router.use(async (req, res, next) => { | ||
if (!req.body.password) { | ||
return res.status(401).json({ | ||
description: "Missing Password", | ||
}); | ||
} | ||
|
||
let user; | ||
|
||
try { | ||
user = await getUserByPassword(req.body.password); | ||
} catch (err) { | ||
if (err instanceof RowNotFoundError) { | ||
return res.status(403).json({ | ||
description: err.message, | ||
}); | ||
} | ||
|
||
return res.sendStatus(500); | ||
} | ||
|
||
res.locals.user = user; | ||
return next(); | ||
}); | ||
|
||
router.post("/sign-in", async (_req, res) => { | ||
const user: User = res.locals.user; | ||
|
||
if (user.signed_in) { | ||
return res.status(400).json({ | ||
description: `${user.first_name} ${user.last_name} is already signed in!`, | ||
}); | ||
} | ||
|
||
await updateUser(user.user_id, { signed_in: true, last_signed_in: Date.now() }); | ||
|
||
return res.status(200).json({ | ||
description: `Signed in as ${user.first_name} ${user.last_name}` | ||
}); | ||
}); | ||
|
||
|
||
|
||
export default router; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import { expect } from "chai"; | ||
|
||
import updateBuilder from "../../utils/updateBuilder"; | ||
|
||
|
||
describe("SQL Update Query Builder", () => { | ||
it("returns full prepared statement given table, updates, and conditions", () => { | ||
const table = "users"; | ||
const updates = { | ||
first_name: "fod", | ||
last_name: "bart", | ||
bool_test: true | ||
}; | ||
const conditions = { | ||
user_id: 2, | ||
password: "pwd", | ||
}; | ||
|
||
const update = updateBuilder(table, updates, conditions); | ||
|
||
expect(update).to.be.deep.equal({ | ||
query: "UPDATE ? SET ? WHERE ?", | ||
params: [ | ||
"users", | ||
"first_name = 'fod', last_name = 'bart', bool_test = true", | ||
"user_id = 2 AND password = 'pwd'", | ||
], | ||
}); | ||
}); | ||
|
||
it("returns prepared statement without WHERE given table and updates", () => { | ||
const table = "sessions"; | ||
const updates = { | ||
start_time: 3408539567, | ||
end_time: 948534534, | ||
amended: true, | ||
str_test: "blah blah blah", | ||
}; | ||
|
||
const update = updateBuilder(table, updates); | ||
|
||
expect(update).to.be.deep.equal({ | ||
query: "UPDATE ? SET ?", | ||
params: [ | ||
"sessions", | ||
"start_time = 3408539567, end_time = 948534534, amended = true, str_test = 'blah blah blah'", | ||
], | ||
}); | ||
}); | ||
}); |
Oops, something went wrong.