How to use the @accounts/common.AccountsError function in @accounts/common

To help you get started, we’ve selected a few @accounts/common 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 / client / src / AccountsClient.js View on Github external
{
          username: user && user.username,
          email: user && user.email,
        },
        400,
      );
    }

    // In case where password is an object we assume it was prevalidated and hashed
    if (!user.password ||
        (isString(user.password) && !validators.validatePassword(user.password))) {
      throw new AccountsError('Password is required');
    }

    if (!validators.validateUsername(user.username) && !validators.validateEmail(user.email)) {
      throw new AccountsError('Username or Email is required');
    }

    const hashAlgorithm = this.options.passwordHashAlgorithm;
    const password = (user.password && hashAlgorithm) ?
      hashPassword(user.password, hashAlgorithm) : user.password;
    const userToCreate = { ...user, password };
    try {
      const userId = await this.transport.createUser(userToCreate);
      const { onUserCreated } = this.options;
      if (callback && isFunction(callback)) {
        callback();
      }
      if (isFunction(onUserCreated)) {
        try {
          await onUserCreated({ id: userId });
        } catch (err) {
github accounts-js / accounts / packages / client / src / AccountsClient.js View on Github external
async createUser(user: CreateUserType, callback: ?Function): Promise {
    if (!user || user.password === undefined) {
      throw new AccountsError(
        'Unrecognized options for create user request',
        {
          username: user && user.username,
          email: user && user.email,
        },
        400,
      );
    }

    // In case where password is an object we assume it was prevalidated and hashed
    if (!user.password ||
        (isString(user.password) && !validators.validatePassword(user.password))) {
      throw new AccountsError('Password is required');
    }

    if (!validators.validateUsername(user.username) && !validators.validateEmail(user.email)) {
github accounts-js / accounts / packages / client / src / AccountsClient.js View on Github external
async loginWithPassword(user: PasswordLoginUserType,
                          password: ?PasswordType,
                          callback?: Function): Promise {
    if (!password || !user) {
      throw new AccountsError('Unrecognized options for login request', user, 400);
    }
    if ((!isString(user) && !isValidUserObject(user)) || !isString(password)) {
      throw new AccountsError('Match failed', user, 400);
    }

    this.store.dispatch(loggingIn(true));
    try {
      const hashAlgorithm = this.options.passwordHashAlgorithm;
      const pass = hashAlgorithm ? hashPassword(password, hashAlgorithm) : password;
      const res: LoginReturnType = await this.transport.loginWithPassword(user, pass);

      this.store.dispatch(loggingIn(false));
      await this.storeTokens(res.tokens);
      this.store.dispatch(setTokens(res.tokens));
      this.store.dispatch(setUser(res.user));
github accounts-js / accounts / packages / client / src / AccountsClient.js View on Github external
async loginWithPassword(user: PasswordLoginUserType,
                          password: ?PasswordType,
                          callback?: Function): Promise {
    if (!password || !user) {
      throw new AccountsError('Unrecognized options for login request', user, 400);
    }
    if ((!isString(user) && !isValidUserObject(user)) || !isString(password)) {
      throw new AccountsError('Match failed', user, 400);
    }

    this.store.dispatch(loggingIn(true));
    try {
      const hashAlgorithm = this.options.passwordHashAlgorithm;
      const pass = hashAlgorithm ? hashPassword(password, hashAlgorithm) : password;
      const res: LoginReturnType = await this.transport.loginWithPassword(user, pass);

      this.store.dispatch(loggingIn(false));
      await this.storeTokens(res.tokens);
      this.store.dispatch(setTokens(res.tokens));
      this.store.dispatch(setUser(res.user));

      if (this.options.onSignedInHook && isFunction(this.options.onSignedInHook)) {
        this.options.onSignedInHook();
      }
github accounts-js / accounts / packages / client / src / AccountsClient.js View on Github external
this.clearTokens();
      this.store.dispatch(clearUser());
      if (callback && isFunction(callback)) {
        callback();
      }

      if (this.options.onSignedOutHook) {
        this.options.onSignedOutHook();
      }
    } catch (err) {
      this.clearTokens();
      this.store.dispatch(clearUser());
      if (callback && isFunction(callback)) {
        callback(err);
      }
      throw new AccountsError(err.message);
    }
  }
github accounts-js / accounts / packages / client / src / AccountsClient.js View on Github external
async impersonate(username: string): Promise {
    if (!isString(username)) {
      throw new AccountsError('Username is required');
    }
    if (this.isImpersonated()) {
      throw new AccountsError('User already impersonating');
    }
    const { accessToken, refreshToken } = await this.tokens();

    if (!accessToken) {
      throw new AccountsError('There is no access tokens available');
    }

    const res = await this.transport.impersonate(accessToken, username);
    if (!res.authorized) {
      throw new AccountsError(`User unauthorized to impersonate ${username}`);
    } else {
      const { persistImpersonation } = this.options;
      this.store.dispatch(setImpersonated(true));
      this.store.dispatch(setOriginalTokens({ accessToken, refreshToken }));

      if (persistImpersonation) {
github accounts-js / accounts / packages / client / src / AccountsClient.js View on Github external
async impersonate(username: string): Promise {
    if (!isString(username)) {
      throw new AccountsError('Username is required');
    }
    if (this.isImpersonated()) {
      throw new AccountsError('User already impersonating');
    }
    const { accessToken, refreshToken } = await this.tokens();

    if (!accessToken) {
      throw new AccountsError('There is no access tokens available');
    }

    const res = await this.transport.impersonate(accessToken, username);
    if (!res.authorized) {
      throw new AccountsError(`User unauthorized to impersonate ${username}`);
    } else {
      const { persistImpersonation } = this.options;
      this.store.dispatch(setImpersonated(true));
      this.store.dispatch(setOriginalTokens({ accessToken, refreshToken }));

      if (persistImpersonation) {
        await this.storeOriginalTokens({ accessToken, refreshToken });
        await this.storeTokens(res.tokens);
      }

      this.store.dispatch(setTokens(res.tokens));
github accounts-js / accounts / packages / client / src / AccountsClient.js View on Github external
async impersonate(username: string): Promise {
    if (!isString(username)) {
      throw new AccountsError('Username is required');
    }
    if (this.isImpersonated()) {
      throw new AccountsError('User already impersonating');
    }
    const { accessToken, refreshToken } = await this.tokens();

    if (!accessToken) {
      throw new AccountsError('There is no access tokens available');
    }

    const res = await this.transport.impersonate(accessToken, username);
    if (!res.authorized) {
      throw new AccountsError(`User unauthorized to impersonate ${username}`);
    } else {
      const { persistImpersonation } = this.options;
      this.store.dispatch(setImpersonated(true));
github accounts-js / accounts / packages / client / src / AccountsClient.js View on Github external
this.store.dispatch(loggingIn(false));

          await this.storeTokens(refreshedSession.tokens);
          this.store.dispatch(setTokens(refreshedSession.tokens));
          this.store.dispatch(setUser(refreshedSession.user));
        }
      } catch (err) {
        this.store.dispatch(loggingIn(false));
        this.clearTokens();
        this.clearUser();
        throw new AccountsError('falsy token provided');
      }
    } else {
      this.clearTokens();
      this.clearUser();
      throw new AccountsError('no tokens provided');
    }
  }

@accounts/common

Fullstack authentication and accounts-management

MIT
Latest version published 7 years ago

Package Health Score

54 / 100
Full package analysis