Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
P
Progark gruppe 3
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container registry
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Tobias Ingebrigt Ørstad
Progark gruppe 3
Commits
4c79e174
Commit
4c79e174
authored
5 years ago
by
Tobias Ingebrigt Ørstad
Browse files
Options
Downloads
Plain Diff
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
!36
Resolve "Create player and login"
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
backend/api/players.js
+75
-0
75 additions, 0 deletions
backend/api/players.js
with
75 additions
and
0 deletions
backend/api/players.js
+
75
−
0
View file @
4c79e174
...
...
@@ -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
;
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment