-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
create class and service, add basic password validation (#475)
* create class and service, add basic password validation * update user row with the new password
- Loading branch information
1 parent
7b4998b
commit ab287b7
Showing
3 changed files
with
105 additions
and
0 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
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', | ||
} | ||
} | ||
} |
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,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 | ||
}, | ||
], | ||
}, | ||
}) | ||
} |
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