Skip to content

Commit

Permalink
create class and service, add basic password validation (#475)
Browse files Browse the repository at this point in the history
* create class and service, add basic password validation

* update user row with the new password
  • Loading branch information
danieleguido authored Dec 16, 2024
1 parent 7b4998b commit ab287b7
Show file tree
Hide file tree
Showing 3 changed files with 105 additions and 0 deletions.
54 changes: 54 additions & 0 deletions src/services/change-password/change-password.class.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import type { Sequelize } from 'sequelize'
import initDebug from 'debug'
import type { ImpressoApplication } from '../../types'
import User from '../../models/users.model'
import { NotAuthenticated } from '@feathersjs/errors'

const debug = initDebug('impresso:services/change-password')

export interface ServiceOptions {
app: ImpressoApplication
name: string
}

export class Service {
app: ImpressoApplication
name: string
sequelizeClient?: Sequelize
constructor({ app, name }: ServiceOptions) {
this.app = app
this.name = name

this.sequelizeClient = app.get('sequelizeClient')
debug('change-password initialized.')
}

async create(data: any, params: { user: { id: number } }) {
if (!this.sequelizeClient) {
throw new Error('Sequelize client not available')
}
debug('change-password.create', params.user.id)
const userSequelize = User.sequelize(this.sequelizeClient)
const user = await userSequelize.findOne({
where: {
id: params.user.id,
},
})
if (!user || user.get('id') !== params.user.id) {
throw new NotAuthenticated('User not found')
}
const updated = await userSequelize.update(
{
password: User.buildPassword({ password: data.sanitized.password }),
},
{
// criteria
where: { id: params.user.id },
}
)
debug('change-password.create', updated)
return {
response: 'ok',
}
}
}
50 changes: 50 additions & 0 deletions src/services/change-password/change-password.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import { Service } from './change-password.class'
import { ImpressoApplication } from '../../types'
import { HookContext, ServiceOptions } from '@feathersjs/feathers'
import { authenticateAround } from '../../hooks/authenticate'
import { REGEX_PASSWORD, validate } from '../../hooks/params'
import { BadRequest } from '@feathersjs/errors'

export default (app: ImpressoApplication) => {
app.use(
'/change-password',
new Service({
app,
name: 'change-password',
}),
{
events: [],
} as ServiceOptions
)
const service = app.service('change-password')
service.hooks({
around: {
all: [authenticateAround()],
},
before: {
create: [
// validate a JWT token using a regular expression
validate(
{
password: {
required: true,
regex: REGEX_PASSWORD,
},
verifyPassword: {
required: true,
regex: REGEX_PASSWORD,
},
},
'POST'
),
(context: HookContext) => {
const { password, verifyPassword } = context.data
if (password !== verifyPassword) {
throw new BadRequest('Passwords do not match')
}
return context
},
],
},
})
}
1 change: 1 addition & 0 deletions src/services/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ const internalApiServices = [
'subscriptions',
'text-reuse-connected-clusters',
'password-reset',
'change-password',
'terms-of-use',
'user-requests',
'user-requests-reviews',
Expand Down

0 comments on commit ab287b7

Please sign in to comment.