Add user email for signing / registration flows

main
MiguelMLorente 2025-12-07 20:16:37 +01:00
parent 674ad8e9a4
commit f4270b7686
5 changed files with 16 additions and 12 deletions

View File

@ -21,21 +21,22 @@ export class AccessController {
@Post('/login')
@Public()
public async login(
@Body('name') name: string,
@Body('email') email: string,
@Body('password') password: string,
) {
this.logger.debug('Received login request');
return await this.authService.signIn(name, password);
return await this.authService.signIn(email, password);
}
@Post('/register')
@Public()
public async new(
@Body('email') email: string,
@Body('name') name: string,
@Body('password') password: string,
) {
this.logger.debug('Received register request');
await this.userService.createUser(name, password);
await this.userService.createUser(email, name, password);
return await this.authService.signIn(name, password);
}
}

View File

@ -17,6 +17,9 @@ export class User {
@PrimaryGeneratedColumn('uuid')
id: string;
@Column()
email: string;
@Column()
name: string;

View File

@ -10,8 +10,8 @@ export class AuthService {
private jwtService: JwtService,
) {}
public async signIn(name: string, password: string) {
const user = await this.usersService.getUserByName(name);
public async signIn(email: string, password: string) {
const user = await this.usersService.getUserByEmail(email);
if (!(await bcrypt.compare(password, user?.password))) {
throw new UnauthorizedException();
}

View File

@ -39,6 +39,7 @@ export class SessionService {
id: sessionId,
status: In([SessionStatus.SCHEDULED, SessionStatus.OPEN]),
},
relations: { users: true },
});
Promise.all(
session.users.map(
@ -56,6 +57,7 @@ export class SessionService {
id: sessionId,
status: In([SessionStatus.SCHEDULED, SessionStatus.OPEN]),
},
relations: { users: true },
});
Promise.all(
session.users.map(

View File

@ -11,9 +11,9 @@ export class UserService {
constructor(@InjectRepository(User) private userRepo: Repository<User>) {}
public async getUserByName(name: string) {
this.logger.debug(`Get user by name: ${name}`);
const user = await this.userRepo.findOne({ where: { name: name } });
public async getUserByEmail(email: string) {
this.logger.debug(`Get user by email: ${email}`);
const user = await this.userRepo.findOne({ where: { email: email } });
if (!user) {
throw new NotFoundException();
}
@ -29,17 +29,15 @@ export class UserService {
return user;
}
public async createUser(name: string, password: string) {
public async createUser(email: string, name: string, password: string) {
try {
await this.getUserByName(name);
await this.getUserByEmail(name);
throw new NotFoundException();
} catch (e) {
if (!(e instanceof NotFoundException)) throw e;
}
this.logger.debug(`Creating user with name: ${name}`);
this.logger.debug(`BCrypt password: ${await bcrypt.hash(password, 10)}`);
this.logger.debug(`Password: ${password}`);
const user: User = this.userRepo.create({
name,
password: await bcrypt.hash(password, 10),