-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(cms): add change password and update profile endpoints to cms ba…
…ckend api
- Loading branch information
Showing
8 changed files
with
385 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import Bcrypt from 'bcryptjs' | ||
import { route } from '@tensei/common' | ||
|
||
export const changePasswordRoute = route('Change password') | ||
.post() | ||
.authorize(({ user }) => !!user) | ||
.handle(async (request, response) => { | ||
try { | ||
await request.config.indicative.validator.validateAll( | ||
request.body, | ||
{ | ||
password: 'required', | ||
newPassword: 'required|min:12' | ||
}, | ||
{ | ||
'password.required': 'The password is required.', | ||
'newPassword.required': 'The new password is required.' | ||
} | ||
) | ||
} catch (error) { | ||
return response.status(422).json({ | ||
errors: error | ||
}) | ||
} | ||
|
||
const payload = { | ||
password: request.body.password, | ||
newPassword: request.body.newPassword | ||
} | ||
|
||
if (!Bcrypt.compareSync(payload.password, request.user?.password)) { | ||
return response.status(422).json({ | ||
errors: [ | ||
{ | ||
message: 'Your current password is incorrect', | ||
field: 'password' | ||
} | ||
] | ||
}) | ||
} | ||
|
||
request.user.password = payload.newPassword | ||
|
||
await request.repositories.adminUsers().persistAndFlush(request.user) | ||
|
||
return response.status(204).json() | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import { route } from '@tensei/common' | ||
|
||
export const updateProfileRoute = route('Update Profile') | ||
.patch() | ||
.authorize(({ user }) => !!user) | ||
.handle(async (request, response) => { | ||
try { | ||
await request.config.indicative.validator.validateAll( | ||
request.body, | ||
{ | ||
firstName: 'required', | ||
lastName: 'required', | ||
email: 'required|email' | ||
}, | ||
{ | ||
'firstName.required': 'The first name is required.', | ||
'lastName.required': 'The last name is required.', | ||
'email.required': 'The email is required.', | ||
'email.email': 'Please provide a valid email.' | ||
} | ||
) | ||
} catch (error) { | ||
return response.status(422).json({ | ||
errors: error | ||
}) | ||
} | ||
|
||
const payload = { | ||
firstName: request.body.firstName, | ||
lastName: request.body.lastName, | ||
email: request.body.email | ||
} | ||
|
||
if (payload.firstName) { | ||
request.user.firstName = payload.firstName | ||
} | ||
|
||
if (payload.lastName) { | ||
request.user.lastName = payload.lastName | ||
} | ||
|
||
if (payload.email) { | ||
request.user.email = payload.email | ||
} | ||
|
||
await request.repositories.adminUsers().persistAndFlush(request.user) | ||
|
||
return response.status(204).json() | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.