Browse Source

api用户登录

wangqifan 3 years ago
parent
commit
6540da7cbb
1 changed files with 8 additions and 1 deletions
  1. 8 1
      src/users/users.service.ts

+ 8 - 1
src/users/users.service.ts

@@ -22,6 +22,7 @@ import { ApiUserService } from '../api-users/api-user.service'
 import { paginate, Pagination } from 'nestjs-typeorm-paginate'
 import { Role } from '../model/role.enum'
 import { PageRequest } from '../common/dto/page-request'
+import { th } from 'date-fns/locale'
 
 @Injectable()
 export class UsersService {
@@ -105,9 +106,15 @@ export class UsersService {
         if (!isMatch) {
             throw new UnauthorizedException('用户名或密码错误')
         }
-        if (!user.roles.includes(Role.Admin)) {
+        if (!user.roles.includes(Role.Admin) && !user.roles.includes(Role.Api)) {
             throw new UnauthorizedException('用户名或密码错误')
         }
+        if (user.roles.includes(Role.Api)) {
+            let apiUser = await this.apiUserService.findById(user.apiUserId)
+            if (apiUser.userId != apiUser.id) {
+                throw new UnauthorizedException('用户名或密码错误')
+            }
+        }
         return user
     }