How to use the routing-controllers.UnauthorizedError function in routing-controllers

To help you get started, we’ve selected a few routing-controllers 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 geli-lms / geli / api / src / security / passportLoginStrategy.ts View on Github external
if (!user) {
        return done(new UnauthorizedError('couldNotBeVerified'), null);
      }

      // dismiss password reset process
      if (!isNullOrUndefined(user.resetPasswordToken)) {
        user.resetPasswordToken = undefined;
        user.resetPasswordExpires = undefined;
        await user.save();
      }

      const isValid = await user.isValidPassword(password);
      if (!isValid) {
        return done(new UnauthorizedError('couldNotBeVerified'), null);
      } else if (!user.isActive) {
        return done(new UnauthorizedError('notActiveYet'), null);
      } else {
        return done(null, user);
      }
    } catch (err) {
      done(new UnauthorizedError('unknown'), null);
    }
  });
github geli-lms / geli / api / src / security / passportLoginStrategy.ts View on Github external
try {
      const user = await User.findOne({email: email});
      if (!user) {
        return done(new UnauthorizedError('couldNotBeVerified'), null);
      }

      // dismiss password reset process
      if (!isNullOrUndefined(user.resetPasswordToken)) {
        user.resetPasswordToken = undefined;
        user.resetPasswordExpires = undefined;
        await user.save();
      }

      const isValid = await user.isValidPassword(password);
      if (!isValid) {
        return done(new UnauthorizedError('couldNotBeVerified'), null);
      } else if (!user.isActive) {
        return done(new UnauthorizedError('notActiveYet'), null);
      } else {
        return done(null, user);
      }
    } catch (err) {
      done(new UnauthorizedError('unknown'), null);
    }
  });
github SammyLiang97 / Mikey / src / controllers / user.controller.ts View on Github external
async wechatLogin(@BodyParam('code', {required: true}) code: string): Promise<{usertype: UserType, jwt: {token: string, expiresOn: number}}> {
        try {
            const res = await Axios.get(`https://api.weixin.qq.com/sns/jscode2session?appid=${wechatConfig.appid}&secret=${wechatConfig.appsecret}&js_code=${code}&grant_type=authorization_code`)
            const openid = res.data.openid
            const oldUser = await this.userService.userModel.findOne({openid: openid})
            if (oldUser) {
                return this.userService.signUser(oldUser)
            } else {
                const newUser = await this.userService.createNewCustomer(openid)
                return this.userService.signUser(newUser)
            }
        } catch (e) {
            console.log(e)
            throw new UnauthorizedError()
        }
    }
github geli-lms / geli / api / src / security / passportJwtStrategyFactory.ts View on Github external
const verify: VerifiedCallback = async (payload, done) => {
    if (forbidMediaTokens && payload.isMediaToken) {
      done(new UnauthorizedError(errorCodes.misc.mediaTokenInsufficient.code), false);
    }
    try {
      if (await User.findById(payload._id)) {
        done(null, {tokenPayload: payload});
      } else {
        done(null, false);
      }
    } catch (error) {
      done(error);
    }
  };
github geli-lms / geli / api / src / security / passportLoginStrategy.ts View on Github external
if (!isNullOrUndefined(user.resetPasswordToken)) {
        user.resetPasswordToken = undefined;
        user.resetPasswordExpires = undefined;
        await user.save();
      }

      const isValid = await user.isValidPassword(password);
      if (!isValid) {
        return done(new UnauthorizedError('couldNotBeVerified'), null);
      } else if (!user.isActive) {
        return done(new UnauthorizedError('notActiveYet'), null);
      } else {
        return done(null, user);
      }
    } catch (err) {
      done(new UnauthorizedError('unknown'), null);
    }
  });
github bitwit / typescript-express-api-template / api / controllers / UserController.ts View on Github external
async put(
        @CurrentUser({ required: true }) currentUser: User,
        @Param("id") id: number,
        @Body({ validate: true }) userData: UpdateUserBody) {

        if (id != currentUser.id) {
            throw new UnauthorizedError("Can not edit other users")
        }

        Object.assign(currentUser, userData)
        return currentUser.save()
    }
github geli-lms / geli / api / src / controllers / ConfigController.ts View on Github external
getPublicConfig(@Param('id') name: string) {
    if (!isPublicConfig(name)) {
      throw new UnauthorizedError();
    }
    return this.findConfig(name);
  }
github geli-lms / geli / api / src / security / RoleAuthorization.ts View on Github external
static checkAuthorization(action: Action, roles: string[]): Promise {
    const jwtData = action.request.jwtData;
    if (!jwtData) {
      throw new UnauthorizedError();
    }
    const userId = jwtData.tokenPayload._id;

    return User.findById(mongoose.Types.ObjectId(userId))
      .then((user) => {
        if (user && !roles.length) {
          return true;
        }

        if (user && (roles.indexOf(user.role) !== -1)) {
          return true;
        }

        return false;
      });
  }
github SammyLiang97 / Mikey / src / controllers / user.controller.ts View on Github external
async doAdminLogin(@BodyParam('username', {required: true}) username: string, @BodyParam('password', {required: true}) password: string): Promise<{usertype: UserType, jwt: {token: string, expiresOn: number}}> {
        const u = await this.userService.userModel.findOne({username: username, password: md5(password), usertype: 1})
        if (u) {
            return this.userService.signUser(u)
        } else {
            throw new UnauthorizedError()
        }
    }