|
|
@@ -4,7 +4,8 @@ import {
|
|
|
HttpException,
|
|
|
HttpStatus,
|
|
|
BadRequestException,
|
|
|
- InternalServerErrorException
|
|
|
+ InternalServerErrorException,
|
|
|
+ UnauthorizedException
|
|
|
} from '@nestjs/common'
|
|
|
import { Repository, UpdateResult } from 'typeorm'
|
|
|
import { InjectRepository } from '@nestjs/typeorm'
|
|
|
@@ -18,6 +19,7 @@ import { SmsService } from '../sms/sms.service'
|
|
|
import * as randomstring from 'randomstring'
|
|
|
import { MembershipService } from '../membership/membership.service'
|
|
|
import { paginate, Pagination, IPaginationOptions } from 'nestjs-typeorm-paginate'
|
|
|
+import { Role } from 'src/model/role.enum'
|
|
|
|
|
|
@Injectable()
|
|
|
export class UsersService {
|
|
|
@@ -79,6 +81,21 @@ export class UsersService {
|
|
|
return user
|
|
|
}
|
|
|
|
|
|
+ public async loginAdmin(username: string, password: string): Promise<Users> {
|
|
|
+ let user = await this.userRepository.findOneBy({ username })
|
|
|
+ if (!user) {
|
|
|
+ throw new UnauthorizedException('用户名或密码错误')
|
|
|
+ }
|
|
|
+ const isMatch = await this.hashingService.compare(password, user.password)
|
|
|
+ if (!isMatch) {
|
|
|
+ throw new UnauthorizedException('用户名或密码错误')
|
|
|
+ }
|
|
|
+ if (!user.roles.includes(Role.Admin)) {
|
|
|
+ throw new UnauthorizedException('用户名或密码错误')
|
|
|
+ }
|
|
|
+ return user
|
|
|
+ }
|
|
|
+
|
|
|
public async create(userDto: UserCreateDto): Promise<IUsers> {
|
|
|
try {
|
|
|
return await this.userRepository.save(userDto)
|