Skip to content

Commit

Permalink
feat(cms): add change password and update profile endpoints to cms ba…
Browse files Browse the repository at this point in the history
…ckend api
  • Loading branch information
bahdcoder committed Nov 18, 2021
1 parent 0d3bf74 commit e04f712
Show file tree
Hide file tree
Showing 8 changed files with 385 additions and 8 deletions.
3 changes: 0 additions & 3 deletions examples/typescript/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,6 @@
"main": "src/index.ts",
"license": "MIT",
"dependencies": {
"@tensei/cms": "^0.9.9",
"@tensei/core": "^0.9.9",
"@tensei/graphql": "^0.9.9",
"@mikro-orm/sqlite": "^4.4.0",
"@tensei/cms": "^0.10.0",
"@tensei/core": "^0.10.0",
Expand Down
6 changes: 5 additions & 1 deletion packages/cms/plugin/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ import {
import getRoutes from './routes'
import { setupCms } from './setup'
import { DataPayload } from '@tensei/common/config'
import { changePasswordRoute } from './routes/change-password'
import { updateProfileRoute } from './routes/update-profile'

const indexFileContent = Fs.readFileSync(
Path.resolve(__dirname, 'template', 'index.mustache')
Expand Down Expand Up @@ -120,8 +122,10 @@ class CmsPlugin {

return response.status(204).json()
}),
changePasswordRoute.path(this.getApiPath('auth/change-password')),
updateProfileRoute.path(this.getApiPath('auth/update-profile')),
route('Logout')
.path(this.getApiPath('logout'))
.path(this.getApiPath('auth/logout'))
.id('logout')
.post()
.handle(async (request, response) => {
Expand Down
5 changes: 4 additions & 1 deletion packages/cms/plugin.d.ts → packages/cms/plugin/plugin.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@ import { User } from '@tensei/common'
declare global {
namespace Express {
interface User {
id: number
password: string
firstName: string
lastName: string
email: string
}
}
}
Expand Down
47 changes: 47 additions & 0 deletions packages/cms/plugin/routes/change-password.ts
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()
})
49 changes: 49 additions & 0 deletions packages/cms/plugin/routes/update-profile.ts
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()
})
4 changes: 2 additions & 2 deletions packages/cms/store/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ export const useAuthStore = create<AuthState & AuthMethods>(
return window.Tensei.api.post('/auth/register', credentials)
},
async logout() {
return window.Tensei.api.post('/logout')
},
return window.Tensei.api.post('/auth/logout')
}
}))
)
2 changes: 1 addition & 1 deletion packages/cms/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"compilerOptions": {
"module": "commonjs",
"target": "es5",
"lib": ["es6", "dom"],
"lib": ["es2019", "dom"],
"sourceMap": true,
"allowJs": false,
"jsx": "react",
Expand Down
Loading

0 comments on commit e04f712

Please sign in to comment.