I am writing a basic signup api and using find() function to check if there are any duplicate api and in an if() condition im throwing a BadRequestException but it is giving me an error if the input email is already in use. A very similar code in another project is not giving any error but this is.
This is the code snippet. when given an email id already registered in the database it is supposed to throw an exception.
import { Injectable, BadRequestException } from '@nestjs/common';
import { UsersService } from './users.service';
import { randomBytes, scrypt as _scrypt } from 'crypto';
import { promisify } from 'util';
const scrypt = promisify(_scrypt);
@Injectable()
export class AuthService {
constructor(private usersService: UsersService) {}
async signup(ogname: string,
email: string,
password: string,
phone: string,
date_of_birth: Date,
type: string,) {
// See if email is in use
const name = null;
const users = await this.usersService.find(name, email);
console.log(users.length);
if (users.length) {
throw new BadRequestException('email in use');
}
// Hash the users password
// Generate a salt
const salt = randomBytes(8).toString('hex');
// Hash the salt and the password together
const hash = (await scrypt(password, salt, 32)) as Buffer;
// Join the hashed result and the salt together
const result = salt + '.' + hash.toString('hex');
//Create a new user and save it
const user = await this.usersService.create(ogname, email, result, phone, date_of_birth, type)
// return the user
return user;
}
signin() {}
}
expected result:
{
"statusCode": 400,
"message": "email in use",
"error": "Bad Request"
}
here is the github link for the entire code: https://github.com/chaitanya2108/appaya_basketball_club
I've just read the code you posted on GitHub, it's quite different than the code you posted here, then I cannot reproduce the issue.
However, I can give you some suggestion for better implementations:
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.