|
|
@@ -1,3 +1,4 @@
|
|
|
+import { UserRegisterDto } from './dto/user-register.dto'
|
|
|
import {
|
|
|
Injectable,
|
|
|
NotFoundException,
|
|
|
@@ -58,6 +59,24 @@ export class UsersService {
|
|
|
return user
|
|
|
}
|
|
|
|
|
|
+ public async register(userRegister: UserRegisterDto) {
|
|
|
+ const user = await this.userRepository.findOneBy({
|
|
|
+ username: userRegister.username
|
|
|
+ })
|
|
|
+
|
|
|
+ if (user) {
|
|
|
+ throw new BadRequestException('Username already exists')
|
|
|
+ }
|
|
|
+
|
|
|
+ const newUser = new Users()
|
|
|
+ newUser.username = userRegister.username
|
|
|
+ newUser.name = userRegister.username
|
|
|
+ newUser.password = await this.hashingService.hash(userRegister.password)
|
|
|
+ newUser.invitor = userRegister.invitor
|
|
|
+
|
|
|
+ return await this.userRepository.save(newUser)
|
|
|
+ }
|
|
|
+
|
|
|
public async loginByPhone(phone: string, code: string, invitor: number | null): Promise<Users> {
|
|
|
const verified = await this.smsService.verify(phone, code)
|
|
|
if (!verified) {
|
|
|
@@ -77,17 +96,14 @@ export class UsersService {
|
|
|
return user
|
|
|
}
|
|
|
|
|
|
- public async loginAdmin(username: string, password: string): Promise<Users> {
|
|
|
+ public async login(username: string, password: string): Promise<Users> {
|
|
|
let user = await this.userRepository.findOneBy({ username })
|
|
|
if (!user) {
|
|
|
- throw new UnauthorizedException('用户名或密码错误')
|
|
|
+ throw new UnauthorizedException('Username and password doesn\'t match')
|
|
|
}
|
|
|
const isMatch = await this.hashingService.compare(password, user.password)
|
|
|
if (!isMatch) {
|
|
|
- throw new UnauthorizedException('用户名或密码错误')
|
|
|
- }
|
|
|
- if (!user.roles.includes(Role.Admin)) {
|
|
|
- throw new UnauthorizedException('用户名或密码错误')
|
|
|
+ throw new UnauthorizedException('Username and password doesn\'t match')
|
|
|
}
|
|
|
return user
|
|
|
}
|