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
d4346d12
Commit
d4346d12
authored
5 years ago
by
Tobias Ørstad
Browse files
Options
Downloads
Patches
Plain Diff
Added hashing of passwords
parent
b76fd9a7
No related branches found
No related tags found
2 merge requests
!106
Dev
,
!105
Added hashing of passwords
Changes
3
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
backend/api/players.js
+43
-19
43 additions, 19 deletions
backend/api/players.js
backend/package-lock.json
+374
-25
374 additions, 25 deletions
backend/package-lock.json
backend/package.json
+1
-0
1 addition, 0 deletions
backend/package.json
with
418 additions
and
44 deletions
backend/api/players.js
+
43
−
19
View file @
d4346d12
const
express
=
require
(
"
express
"
);
const
router
=
express
.
Router
();
const
mongo
=
require
(
"
mongodb
"
);
const
bcrypt
=
require
(
"
bcrypt
"
);
const
MongoClient
=
mongo
.
MongoClient
;
const
connectionUrl
=
process
.
env
.
MONGO_CONNECTION_STRING
;
const
saltRounds
=
10
;
router
.
get
(
"
/username/:playerId
"
,
(
req
,
res
)
=>
{
// Connect to database
...
...
@@ -67,14 +69,30 @@ router.get("/login/:username/:password", (req, res) => {
db
.
collection
(
collection
)
.
find
({
name
:
req
.
params
.
username
,
password
:
req
.
params
.
password
,
})
.
toArray
((
err
,
result
)
=>
{
if
(
err
)
{
res
.
sendStatus
(
500
);
return
;
}
res
.
json
(
result
);
// Compares the given password with the encrypted password stored in the database,
// response is true on match, false else
bcrypt
.
compare
(
req
.
params
.
password
,
result
[
0
].
password
,
(
err
,
response
)
=>
{
if
(
err
)
{
res
.
sendStatus
(
500
);
client
.
close
();
return
;
}
if
(
response
)
{
res
.
json
(
result
);
}
else
{
res
.
json
([]);
}
}
);
client
.
close
();
});
}
...
...
@@ -103,24 +121,30 @@ router.put("/", (req, res) => {
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
]);
client
.
close
();
//Hashes the password
bcrypt
.
hash
(
req
.
body
.
password
,
saltRounds
,
(
err
,
hash
)
=>
{
if
(
err
)
{
res
.
sendStatus
(
500
);
// Internal server error
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
:
hash
,
dateJoined
:
date
,
},
(
err
,
result
)
=>
{
if
(
err
)
{
res
.
status
(
400
).
send
(
"
Already existing username
"
);
return
;
}
res
.
json
(
result
.
ops
[
0
]);
client
.
close
();
}
);
});
}
);
});
...
...
This diff is collapsed.
Click to expand it.
backend/package-lock.json
+
374
−
25
View file @
d4346d12
This diff is collapsed.
Click to expand it.
backend/package.json
+
1
−
0
View file @
d4346d12
...
...
@@ -10,6 +10,7 @@
"author"
:
""
,
"license"
:
"ISC"
,
"dependencies"
:
{
"bcrypt"
:
"^4.0.1"
,
"body-parser"
:
"^1.19.0"
,
"cors"
:
"^2.8.5"
,
"express"
:
"^4.17.1"
,
...
...
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