UNPKG

943 BPlain TextView Raw
1import { HookContext } from '@feathersjs/feathers'
2import hashPassword from './hooks/hash-password'
3import protect from './hooks/protect'
4import { LocalStrategy } from './strategy'
5
6export const hooks = { hashPassword, protect }
7export { LocalStrategy }
8
9/**
10 * Returns as property resolver that hashes a given plain text password using a Local
11 * authentication strategy.
12 *
13 * @param options The authentication `service` and `strategy` name
14 * @returns
15 */
16export const passwordHash =
17 (options: { service?: string; strategy: string }) =>
18 async <H extends HookContext<any, any>>(value: string | undefined, _data: any, context: H) => {
19 if (value === undefined) {
20 return value
21 }
22
23 const { app, params } = context
24 const authService = app.defaultAuthentication(options.service)
25 const localStrategy = authService.getStrategy(options.strategy) as LocalStrategy
26
27 return localStrategy.hashPassword(value, params)
28 }