Skip to content

Commit

Permalink
修复 bug user-repository
Browse files Browse the repository at this point in the history
  • Loading branch information
Tonysmark committed Mar 23, 2020
1 parent 0036aa7 commit 2fc4700
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 13 deletions.
1 change: 1 addition & 0 deletions src/database/typeormconfig.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export class TypeormConfigService implements TypeOrmOptionsFactory {
database: this.configService.get('sql.database'),
synchronize: this.configService.get('sql.synchronize'),
entities,
logging: true, // FIXME 临时放在这里
} as TypeOrmModuleOptions;
}
}
6 changes: 3 additions & 3 deletions src/modules/user/user.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ export class UserRepository extends Repository<User> {

async findUserByUniKey(identifier: { username?: string; email?: string; id?: string }): Promise<User> {
const user = await this.createQueryBuilder()
.where('user.username', { username: identifier['username'] })
.orWhere('user.email', { email: identifier['email'] })
.orWhere('user.id', { id: identifier['id'] })
.where('user.username=:username', { username: identifier['username'] })
.orWhere('user.email=:email', { email: identifier['email'] })
.orWhere('user.id=:id', { id: identifier['id'] })
.getOne();
if (user) {
return user;
Expand Down
16 changes: 6 additions & 10 deletions src/modules/user/user.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,14 @@ export class UserService {
}

async validateUser({ username, password }) {
// => 用户不存在, 用户存在但是密码错误, 用户存在但是没有授权
// TODO 这里肯定还能改
// 1. repository 负责用户是否存在,如果用户不存在会抛出 404
// 2. 根据 compare 计算出密码是否正确,如果不正确抛出 401 未授权
const user = await this.userRepository.findUserByUniKey({ username });
if (user) {
if (await HashCode.compare(password, user.password)) {
Logger.log(`User: ${user.username} is verified`, 'UserService');
return await this.authService.tokenGenerator(user);
} else {
throw new UnauthorizedException('密码错误');
}
if (await HashCode.compare(password, user.password)) {
Logger.log(`User: ${user.username} is verified`, 'UserService');
return await this.authService.tokenGenerator(user);
} else {
throw new NotFoundException('用户不存在');
throw new UnauthorizedException('密码错误');
}
}

Expand Down

0 comments on commit 2fc4700

Please sign in to comment.