Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
Server
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
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
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
web-et-numerique
Factory
Resin
Server
Commits
cead66ea
Commit
cead66ea
authored
4 years ago
by
Hugo SUBTIL
Browse files
Options
Downloads
Patches
Plain Diff
feat: add password cahnge endpoint
parent
01878083
No related branches found
Branches containing commit
No related tags found
Tags containing commit
3 merge requests
!27
Recette
,
!26
Dev
,
!6
Feat/change password
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
src/users/change-password.dto.ts
+6
-0
6 additions, 0 deletions
src/users/change-password.dto.ts
src/users/users.controller.ts
+14
-0
14 additions, 0 deletions
src/users/users.controller.ts
src/users/users.service.ts
+20
-1
20 additions, 1 deletion
src/users/users.service.ts
with
40 additions
and
1 deletion
src/users/change-password.dto.ts
0 → 100644
+
6
−
0
View file @
cead66ea
import
{
IsNotEmpty
}
from
'
class-validator
'
;
export
class
PasswordChangeDto
{
@
IsNotEmpty
()
readonly
newPassword
:
string
;
@
IsNotEmpty
()
readonly
oldPassword
:
string
;
}
This diff is collapsed.
Click to expand it.
src/users/users.controller.ts
+
14
−
0
View file @
cead66ea
import
{
Body
,
Controller
,
Get
,
Param
,
Post
,
Query
,
Req
,
Request
,
UseGuards
}
from
'
@nestjs/common
'
;
import
{
ApiOperation
,
ApiParam
,
ApiResponse
}
from
'
@nestjs/swagger
'
;
import
{
JwtAuthGuard
}
from
'
../auth/guards/jwt-auth.guard
'
;
import
{
PasswordChangeDto
}
from
'
./change-password.dto
'
;
import
{
CreateUserDto
}
from
'
./create-user.dto
'
;
import
{
UsersService
}
from
'
./users.service
'
;
...
...
@@ -30,4 +31,17 @@ export class UsersController {
public
async
validateUser
(@
Param
()
params
,
@
Query
(
'
token
'
)
token
:
string
)
{
return
this
.
usersService
.
validateUser
(
params
.
id
,
token
);
}
@
UseGuards
(
JwtAuthGuard
)
@
Post
(
'
change-password
'
)
@
ApiResponse
({
status
:
201
,
description
:
'
Password changed
'
})
@
ApiResponse
({
status
:
401
,
description
:
'
Invalid password
'
})
@
ApiResponse
({
status
:
422
,
description
:
'
Weak password
'
})
public
async
changePassword
(@
Request
()
req
,
@
Body
()
passwordChangeDto
:
PasswordChangeDto
)
{
return
this
.
usersService
.
changeUserPassword
(
req
.
user
.
_id
,
passwordChangeDto
.
oldPassword
,
passwordChangeDto
.
newPassword
);
}
}
This diff is collapsed.
Click to expand it.
src/users/users.service.ts
+
20
−
1
View file @
cead66ea
...
...
@@ -66,7 +66,10 @@ export class UsersService {
return
this
.
userModel
.
findOne
({
email
:
mail
}).
select
(
'
-password
'
).
exec
();
}
public
async
findById
(
id
:
string
):
Promise
<
IUser
|
undefined
>
{
public
async
findById
(
id
:
string
,
passwordQuery
?:
boolean
):
Promise
<
IUser
|
undefined
>
{
if
(
passwordQuery
)
{
return
this
.
userModel
.
findById
(
id
).
exec
();
}
return
this
.
userModel
.
findById
(
id
).
select
(
'
-password
'
).
exec
();
}
...
...
@@ -130,4 +133,20 @@ export class UsersService {
throw
new
HttpException
(
'
Invalid token
'
,
HttpStatus
.
UNAUTHORIZED
);
}
}
public
async
changeUserPassword
(
userId
:
string
,
oldPassword
:
string
,
newPassword
:
string
)
{
const
user
=
await
this
.
findById
(
userId
,
true
);
const
arePasswordEqual
=
await
this
.
comparePassword
(
oldPassword
,
user
.
password
);
if
(
!
arePasswordEqual
)
{
throw
new
HttpException
(
'
Invalid credentials
'
,
HttpStatus
.
UNAUTHORIZED
);
}
if
(
!
this
.
isStrongPassword
(
newPassword
))
{
throw
new
HttpException
(
'
Weak password, it must contain ne lowercase alphabetical character, one uppercase alphabetical character, one numeric character, one special character and be eight characters or longer
'
,
HttpStatus
.
UNPROCESSABLE_ENTITY
);
}
user
.
password
=
await
this
.
hashPassword
(
newPassword
);
user
.
save
();
}
}
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