Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
F
fahrtenbuch
Manage
Activity
Members
Code
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Locked files
Deploy
Releases
Package registry
Model registry
Operate
Terraform modules
Analyze
Contributor 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
SWP WS21_22 - Fahrtenbuch
Team Einhorn
fahrtenbuch
Commits
5a7f9137
Commit
5a7f9137
authored
3 years ago
by
Leander Tolksdorf
Browse files
Options
Downloads
Patches
Plain Diff
add create account route
parent
d41e024b
No related branches found
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
server/src/routes/accounts.controllers.ts
+49
-0
49 additions, 0 deletions
server/src/routes/accounts.controllers.ts
server/src/routes/accounts.routes.ts
+21
-0
21 additions, 0 deletions
server/src/routes/accounts.routes.ts
with
70 additions
and
0 deletions
server/src/routes/accounts.controllers.ts
0 → 100644
+
49
−
0
View file @
5a7f9137
import
bcrypt
from
"
bcrypt
"
;
import
{
Request
,
Response
}
from
"
express
"
;
import
Worker
from
"
../db/models/Worker
"
;
export
const
createAccountController
=
async
(
req
:
Request
,
res
:
Response
)
=>
{
try
{
if
(
!
(
res
.
locals
.
user
.
role
===
"
coordinator
"
))
{
return
res
.
status
(
403
)
.
json
({
success
:
false
,
error
:
"
MustBeCoordinator
"
});
}
const
{
first_name
,
last_name
,
email
,
password
,
role
}
=
req
.
body
;
const
account
=
await
Worker
.
findAll
({
where
:
{
email
:
email
,
},
});
if
(
account
.
length
>
0
)
{
return
res
.
status
(
409
)
.
json
({
success
:
false
,
error
:
"
AccountAlreadyExists
"
});
}
const
hashedPassword
=
await
bcrypt
.
hash
(
password
,
10
);
const
newAccount
=
await
Worker
.
create
({
email
,
firstName
:
first_name
,
lastName
:
last_name
,
password
:
hashedPassword
,
role
,
});
return
res
.
status
(
201
).
send
({
success
:
true
,
account
:
{
first_name
:
newAccount
.
firstName
,
last_name
:
newAccount
.
lastName
,
email
:
newAccount
.
email
,
role
:
newAccount
.
role
,
},
});
}
catch
{
return
res
.
status
(
500
).
json
({
success
:
false
,
error
:
"
serverError
"
});
}
};
This diff is collapsed.
Click to expand it.
server/src/routes/accounts.routes.ts
0 → 100644
+
21
−
0
View file @
5a7f9137
import
{
Router
}
from
"
express
"
;
import
{
body
}
from
"
express-validator
"
;
import
handleValidationResult
from
"
../middleware/handleValidationResult
"
;
import
validateToken
from
"
../middleware/validateToken
"
;
import
{
createAccountController
}
from
"
./accounts.controllers
"
;
const
accountsRouter
=
Router
();
accountsRouter
.
post
(
"
/api/accounts/
"
,
body
(
"
first_name
"
).
not
().
isEmpty
(),
body
(
"
last_name
"
).
not
().
isEmpty
(),
body
(
"
email
"
).
isEmail
().
normalizeEmail
(),
body
(
"
role
"
).
isIn
([
"
coordinator
"
,
"
boatManager
"
]),
body
(
"
password
"
).
isLength
({
min
:
6
}),
handleValidationResult
,
validateToken
,
createAccountController
);
export
default
accountsRouter
;
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