0
 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!!!

D12
  • 1
  • 1

1 Answers1

0

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

mh377
  • 1,656
  • 5
  • 22
  • 41
  • Yeah exactly. But this solution doesn't work – D12 Dec 19 '22 at 07:20
  • Have you tried it ?. Your code is missing the startsWith('22') – mh377 Dec 19 '22 at 08:49
  • Yes I tried it to use the startsWith('22') – D12 Dec 19 '22 at 16:49
  • Your code from above has a mistake if (!admin.adminID.startsWith) doesn't check that it starts with 22. I don't see why my suggestion wouldn't work. Have you tried writing a test and debugging the code ? – mh377 Dec 19 '22 at 17:36
  • I tried but me too I'm not getting why it is not working – D12 Dec 20 '22 at 14:50
  • Have you tried my suggestion on the line above the `try {`statement ?. If the id doesn't start with 22 there is no point in going any further. Also log what is in the dto `console.log(${JSON.stringify(dto)});` – mh377 Dec 20 '22 at 14:58