Skip to content
Snippets Groups Projects
Commit 4c79e174 authored by Tobias Ingebrigt Ørstad's avatar Tobias Ingebrigt Ørstad
Browse files

Merge branch '44-create-player-and-login' into 'dev'

Resolve "Create player and login"

Closes #44

See merge request !36
parents bfe149cf 5291320e
No related branches found
No related tags found
1 merge request!36Resolve "Create player and login"
......@@ -43,5 +43,80 @@ router.get("/username/:userid", (req, res) => {
);
});
router.get("/login/:username/:password", (req, res) => {
// Connect to database
MongoClient.connect(
connectionUrl,
{ useNewUrlParser: true, useUnifiedTopology: true },
(err, client) => {
// Unable to connect to database
if (err) {
res.sendStatus(500); // Internal server error
return;
}
// Using the database gameWare and collection players
const db = client.db("gameWare");
const collection = "players";
// Simply checks if there is a user that matches that exact username and password
db.collection(collection)
.find({
name: req.params.username,
password: req.params.password
})
.toArray((err, result) => {
if (err) {
res.sendStatus(500);
return;
}
res.json(result);
});
}
);
});
router.put("/", (req, res) => {
// Connect to database
MongoClient.connect(
connectionUrl,
{ useNewUrlParser: true, useUnifiedTopology: true },
(err, client) => {
// Unable to connect to database
if (err) {
res.sendStatus(500); // Internal server error
return;
}
// Using the database gameWare and collection players
const db = client.db("gameWare");
const collection = "players";
date = new Date();
//Checks the parameters
if (req.body.username == null || req.body.password == null) {
res.status(400).send("Invalid parameters");
return;
}
// Inserts the user. Note that the name index is unique, inserting a user with an
// already existing username will give an error.
db.collection(collection).insertOne(
{
name: req.body.username,
password: req.body.password,
dateJoined: date
},
(err, result) => {
if (err) {
res.status(400).send("Already existing username"); // Internal server error
return;
}
res.json(result.ops[0]);
}
);
}
);
});
// Export API routes
module.exports = router;
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment