import { AccountsClient } from '@accounts/client' ; import { LoginResult , CreateUserServicePassword , CreateUserResult , LoginUserPasswordService , } from '@accounts/types' ; import { AccountsClientPasswordOptions } from './types' ; export class AccountsClientPassword { private client : AccountsClient ; private options : AccountsClientPasswordOptions ; constructor ( client , options = { } ) { if ( ! client ) { throw new Error ( 'A valid client instance is required' ) ; } this . client = client ; this . options = options ; } /**
     * Create a new user.
     */ public async createUser ( user ) { const hashedPassword = this . hashPassword ( user . password ) ; const createUserResult = await this . client . transport . createUser ( { ... user , password : hashedPassword , } ) ; if ( createUserResult . loginResult ) { await this . client . setTokens ( createUserResult . loginResult . tokens ) ; } return createUserResult ; } /**
     * Log the user in with a password.
     */ public async login ( user ) { const hashedPassword = this . hashPassword ( user . password as string ) ; return this . client . loginWithService ( 'password' , { ... user , password : hashedPassword , } ) ; } param { string } email public requestPasswordReset ( email ) { return this . client . transport . sendResetPasswordEmail ( email ) ; } param { string } token param { string } newPassword public resetPassword ( token , newPassword ) { const hashedPassword = this . hashPassword ( newPassword ) ; return this . client . transport . resetPassword ( token , hashedPassword ) ; } param { string } email public requestVerificationEmail ( email ) { return this . client . transport . sendVerificationEmail ( email ) ; } param { string } token public verifyEmail ( token ) { return this . client . transport . verifyEmail ( token ) ; } param { string } newEmail public addEmail ( newEmail ) { return this . client . transport . addEmail ( newEmail ) ; } param { string } oldPassword param { string } newPassword public changePassword ( oldPassword , newPassword ) { return this . client . transport . changePassword ( this . hashPassword ( oldPassword ) , this . hashPassword ( newPassword ) ) ; } param { string } password public hashPassword ( password ) { if ( this . options . hashPassword ) { return this . options . hashPassword ( password ) ; } return password ; } }