Add user email for signing / registration flows
parent
674ad8e9a4
commit
f4270b7686
|
|
@ -21,21 +21,22 @@ export class AccessController {
|
||||||
@Post('/login')
|
@Post('/login')
|
||||||
@Public()
|
@Public()
|
||||||
public async login(
|
public async login(
|
||||||
@Body('name') name: string,
|
@Body('email') email: string,
|
||||||
@Body('password') password: string,
|
@Body('password') password: string,
|
||||||
) {
|
) {
|
||||||
this.logger.debug('Received login request');
|
this.logger.debug('Received login request');
|
||||||
return await this.authService.signIn(name, password);
|
return await this.authService.signIn(email, password);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Post('/register')
|
@Post('/register')
|
||||||
@Public()
|
@Public()
|
||||||
public async new(
|
public async new(
|
||||||
|
@Body('email') email: string,
|
||||||
@Body('name') name: string,
|
@Body('name') name: string,
|
||||||
@Body('password') password: string,
|
@Body('password') password: string,
|
||||||
) {
|
) {
|
||||||
this.logger.debug('Received register request');
|
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);
|
return await this.authService.signIn(name, password);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -17,6 +17,9 @@ export class User {
|
||||||
@PrimaryGeneratedColumn('uuid')
|
@PrimaryGeneratedColumn('uuid')
|
||||||
id: string;
|
id: string;
|
||||||
|
|
||||||
|
@Column()
|
||||||
|
email: string;
|
||||||
|
|
||||||
@Column()
|
@Column()
|
||||||
name: string;
|
name: string;
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -10,8 +10,8 @@ export class AuthService {
|
||||||
private jwtService: JwtService,
|
private jwtService: JwtService,
|
||||||
) {}
|
) {}
|
||||||
|
|
||||||
public async signIn(name: string, password: string) {
|
public async signIn(email: string, password: string) {
|
||||||
const user = await this.usersService.getUserByName(name);
|
const user = await this.usersService.getUserByEmail(email);
|
||||||
if (!(await bcrypt.compare(password, user?.password))) {
|
if (!(await bcrypt.compare(password, user?.password))) {
|
||||||
throw new UnauthorizedException();
|
throw new UnauthorizedException();
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -39,6 +39,7 @@ export class SessionService {
|
||||||
id: sessionId,
|
id: sessionId,
|
||||||
status: In([SessionStatus.SCHEDULED, SessionStatus.OPEN]),
|
status: In([SessionStatus.SCHEDULED, SessionStatus.OPEN]),
|
||||||
},
|
},
|
||||||
|
relations: { users: true },
|
||||||
});
|
});
|
||||||
Promise.all(
|
Promise.all(
|
||||||
session.users.map(
|
session.users.map(
|
||||||
|
|
@ -56,6 +57,7 @@ export class SessionService {
|
||||||
id: sessionId,
|
id: sessionId,
|
||||||
status: In([SessionStatus.SCHEDULED, SessionStatus.OPEN]),
|
status: In([SessionStatus.SCHEDULED, SessionStatus.OPEN]),
|
||||||
},
|
},
|
||||||
|
relations: { users: true },
|
||||||
});
|
});
|
||||||
Promise.all(
|
Promise.all(
|
||||||
session.users.map(
|
session.users.map(
|
||||||
|
|
|
||||||
|
|
@ -11,9 +11,9 @@ export class UserService {
|
||||||
|
|
||||||
constructor(@InjectRepository(User) private userRepo: Repository<User>) {}
|
constructor(@InjectRepository(User) private userRepo: Repository<User>) {}
|
||||||
|
|
||||||
public async getUserByName(name: string) {
|
public async getUserByEmail(email: string) {
|
||||||
this.logger.debug(`Get user by name: ${name}`);
|
this.logger.debug(`Get user by email: ${email}`);
|
||||||
const user = await this.userRepo.findOne({ where: { name: name } });
|
const user = await this.userRepo.findOne({ where: { email: email } });
|
||||||
if (!user) {
|
if (!user) {
|
||||||
throw new NotFoundException();
|
throw new NotFoundException();
|
||||||
}
|
}
|
||||||
|
|
@ -29,17 +29,15 @@ export class UserService {
|
||||||
return user;
|
return user;
|
||||||
}
|
}
|
||||||
|
|
||||||
public async createUser(name: string, password: string) {
|
public async createUser(email: string, name: string, password: string) {
|
||||||
try {
|
try {
|
||||||
await this.getUserByName(name);
|
await this.getUserByEmail(name);
|
||||||
throw new NotFoundException();
|
throw new NotFoundException();
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
if (!(e instanceof NotFoundException)) throw e;
|
if (!(e instanceof NotFoundException)) throw e;
|
||||||
}
|
}
|
||||||
|
|
||||||
this.logger.debug(`Creating user with name: ${name}`);
|
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({
|
const user: User = this.userRepo.create({
|
||||||
name,
|
name,
|
||||||
password: await bcrypt.hash(password, 10),
|
password: await bcrypt.hash(password, 10),
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue