async signup(dto: AuthDto) {
    try {
      //generate the password
      const hash = await argon.hash(dto.password);

      //generate the admin
      const admin = await this.prisma.admin.create({
        data: {
          adminID: dto.adminID,
          email: dto.email,
          firstName: dto.firstName,
          lastName: dto.lastName,
          hash,
        },
      });

      if (!admin.adminID.startsWith) {
        throw new ForbiddenException('Invalid id');
      }

      return this.adminToken(
        admin.id,
        admin.adminID,
        admin.email,
        admin.firstName,
        admin.lastName,
      );
    } catch (error) {
      if (error instanceof PrismaClientKnownRequestError) {
        if (error.code === 'P2002') {
          throw new ForbiddenException(
            'These credentials already belong to another User!!!',
          );
        }
      }
    }
  }

I want the adminID to start with 22 it is a string. the problem is I am creating a user with another value which starts with any character and then the user is still created which means I am still getting the same results whether I start with 22 or not. I want that whenever I signup with an adminID which doesn't start with 22 I get an error. Your help would really be appreciated!!!

1

There are 1 best solutions below

6
mh377 On

If I have understood this correctly, you want to throw an exception if the adminID does not start with '22'. Then the following code should work:

const adminPrefix = '22';
const adminId = dto.adminID;

if (!adminId.startsWith(adminPrefix) {
   throw new ForbiddenException('Invalid id');
}

See this post for further information