How to use the @accounts/server.ServerHooks.ChangePasswordSuccess function in @accounts/server

To help you get started, we’ve selected a few @accounts/server examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github accounts-js / accounts / packages / password / src / accounts-password.ts View on Github external
public async changePassword(
    userId: string,
    oldPassword: string,
    newPassword: string
  ): Promise {
    if (!this.options.validatePassword(newPassword)) {
      throw new Error(this.options.errors.invalidPassword);
    }

    const user = await this.passwordAuthenticator({ id: userId }, oldPassword);

    const password = await bcryptPassword(newPassword);
    await this.db.setPassword(userId, password);

    this.server.getHooks().emit(ServerHooks.ChangePasswordSuccess, user);

    if (this.options.invalidateAllSessionsAfterPasswordChanged) {
      await this.db.invalidateAllSessions(user.id);
    }

    if (this.options.notifyUserAfterPasswordChanged) {
      const address = user.emails && user.emails[0].address;
      if (!address) {
        throw new Error(this.options.errors.noEmailSet);
      }

      const passwordChangedMail = this.server.prepareMail(
        address,
        '',
        this.server.sanitizeUser(user),
        '',