Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in
Toggle navigation
P
products_lesson
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Pavel Mishakov
products_lesson
Commits
2e137c79
Commit
2e137c79
authored
Mar 28, 2023
by
Pavel Mishakov
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
lesson 91 done BACK
parent
ed053dd4
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
41 additions
and
5 deletions
+41
-5
user.ts
src/controllers/user.ts
+8
-2
generateJWT.ts
src/helpers/generateJWT.ts
+1
-1
IUser.ts
src/interfaces/IUser.ts
+1
-0
mongo.ts
src/repository/mongo.ts
+27
-2
user.ts
src/services/user.ts
+4
-0
No files found.
src/controllers/user.ts
View file @
2e137c79
...
...
@@ -19,8 +19,7 @@ export class UserController {
this
.
router
.
post
(
'/'
,
this
.
createUser
)
this
.
router
.
post
(
'/login'
,
this
.
login
)
this
.
router
.
get
(
'/token'
,
auth
,
this
.
checkToken
)
this
.
router
.
put
(
'/'
,
auth
,
this
.
editUser
)
}
public
getRouter
=
():
Router
=>
{
return
this
.
router
...
...
@@ -30,6 +29,13 @@ export class UserController {
res
.
status
(
200
).
send
(
response
)
}
private
editUser
=
async
(
expressReq
:
Request
,
res
:
Response
):
Promise
<
void
>
=>
{
const
req
=
expressReq
as
IRequestWithTokenData
const
user
=
req
.
dataFromToken
as
IUserGetDto
const
response
:
IResponse
<
IUserGetDto
|
undefined
>
=
await
this
.
service
.
editUser
(
req
.
body
,
user
.
_id
)
res
.
status
(
200
).
send
(
response
)
}
public
login
=
async
(
req
:
Request
,
res
:
Response
):
Promise
<
void
>
=>
{
const
response
:
IResponse
<
IUserGetDto
|
undefined
>
=
await
this
.
service
.
login
(
req
.
body
)
res
.
status
(
200
).
send
(
response
)
...
...
src/helpers/generateJWT.ts
View file @
2e137c79
import
jwt
from
'jsonwebtoken'
export
const
generateJWT
=
(
payload
:
{[
key
:
string
]:
string
|
number
|
boolean
})
=>
{
return
jwt
.
sign
(
payload
,
process
.
env
.
SECRET_KEY
||
''
,
{
expiresIn
:
'2
m
'
})
return
jwt
.
sign
(
payload
,
process
.
env
.
SECRET_KEY
||
''
,
{
expiresIn
:
'2
h
'
})
}
\ No newline at end of file
src/interfaces/IUser.ts
View file @
2e137c79
...
...
@@ -5,4 +5,5 @@ export default interface IUser extends Document {
username
:
string
password
:
string
active
:
boolean
checkPassword
:
(
pass
:
string
)
=>
boolean
}
\ No newline at end of file
src/repository/mongo.ts
View file @
2e137c79
...
...
@@ -281,8 +281,7 @@ export class Mongo implements IDataBase {
if
(
!
user
)
{
throw
new
Error
(
'Not found'
)
}
//@ts-ignore
const
isMatch
:
boolean
=
await
user
.
checkPassword
(
userDto
.
password
)
const
isMatch
:
boolean
=
user
.
checkPassword
(
userDto
.
password
)
if
(
!
isMatch
)
{
throw
new
Error
(
'Wrong password'
)
...
...
@@ -323,6 +322,32 @@ export class Mongo implements IDataBase {
}
}
}
public
editUser
=
async
(
userDto
:
IUserCreateDto
,
id
:
string
):
Promise
<
IResponse
<
IUserGetDto
|
undefined
>>
=>
{
try
{
const
update
:
{
username
:
string
,
password
?:
string
}
=
{
username
:
userDto
.
username
}
if
(
userDto
.
password
)
{
update
.
password
=
userDto
.
password
}
const
user
:
IUserGetDto
|
null
=
await
User
.
findOneAndUpdate
({
_id
:
id
},
update
,
{
new
:
true
})
return
{
status
:
EStatuses
.
OK
,
result
:
user
||
undefined
,
message
:
'User was updated'
}
}
catch
(
err
:
unknown
)
{
return
{
status
:
EStatuses
.
NOT_OK
,
result
:
undefined
,
message
:
'[ERROR] Cannot update user'
}
}
}
}
export
const
mongo
=
new
Mongo
()
\ No newline at end of file
src/services/user.ts
View file @
2e137c79
...
...
@@ -15,6 +15,10 @@ export class UserService {
return
await
this
.
repository
.
createUser
(
userDto
)
}
public
editUser
=
async
(
userDto
:
IUserCreateDto
,
id
:
string
):
Promise
<
IResponse
<
IUserGetDto
|
undefined
>>
=>
{
return
await
this
.
repository
.
editUser
(
userDto
,
id
)
}
public
login
=
async
(
userDto
:
IUserCreateDto
):
Promise
<
IResponse
<
IUserGetDto
|
undefined
>>
=>
{
return
await
this
.
repository
.
login
(
userDto
)
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment