xiongzhu il y a 2 ans
Parent
commit
b2ef8beae1

+ 15 - 1
src/auth/auth.controller.ts

@@ -5,6 +5,7 @@ import { ApiTags } from '@nestjs/swagger'
 import { Public } from './public.decorator'
 import { HasRoles } from './roles.decorator'
 import { Role } from '../model/role.enum'
+import { UserRegisterDto } from 'src/users/dto/user-register.dto'
 
 @ApiTags('auth')
 @Controller('/auth')
@@ -18,8 +19,14 @@ export class AuthController {
     }
 
     @Public()
-    @Post('/admin/login')
+    @Post('/login')
     async login(@Body() { username, password }) {
+        return await this.authService.login(username, password)
+    }
+
+    @Public()
+    @Post('/admin/login')
+    async loginAdmin(@Body() { username, password }) {
         return await this.authService.loginAdmin(username, password)
     }
 
@@ -28,4 +35,11 @@ export class AuthController {
     async getToken(@Param('userId') userId: string) {
         return await this.authService.getToken(Number(userId))
     }
+
+    @Public()
+    @Post('/register')
+    async register(@Body() register: UserRegisterDto) {
+        return await this.authService.register(register)
+    }
+
 }

+ 24 - 2
src/auth/auth.service.ts

@@ -1,7 +1,9 @@
 import { PhoneLoginDto } from './dto/login.dto'
-import { Injectable } from '@nestjs/common'
+import { Injectable, UnauthorizedException } from '@nestjs/common'
 import { JwtService } from '@nestjs/jwt'
 import { UsersService } from '../users/users.service'
+import { Role } from 'src/model/role.enum'
+import { UserRegisterDto } from 'src/users/dto/user-register.dto'
 
 @Injectable()
 export class AuthService {
@@ -20,8 +22,24 @@ export class AuthService {
         }
     }
 
+    async login(username: string, password: string) {
+        let user = await this.usersService.login(username, password)
+        const payload = {
+            username: user.username,
+            sub: user.id,
+            roles: user.roles
+        }
+        this.usersService.updateIat(user)
+        return {
+            access_token: this.jwtService.sign(payload)
+        }
+    }
+
     async loginAdmin(username: string, password: string) {
-        let user = await this.usersService.loginAdmin(username, password)
+        let user = await this.usersService.login(username, password)
+        if (!user.roles.includes(Role.Admin)) {
+            throw new UnauthorizedException('Permission denied')
+        }
         const payload = {
             username: user.username,
             sub: user.id,
@@ -44,4 +62,8 @@ export class AuthService {
             access_token: this.jwtService.sign(payload)
         }
     }
+
+    async register(register: UserRegisterDto) {
+        return await this.usersService.register(register)
+    }
 }

+ 3 - 2
src/sys-config/entities/sys-config.entity.ts

@@ -4,7 +4,8 @@ export enum SysConfigType {
     String = 'string',
     Date = 'date',
     Number = 'number',
-    Boolean = 'boolean'
+    Boolean = 'boolean',
+    Object = 'object'
 }
 
 @Entity()
@@ -19,7 +20,7 @@ export class SysConfig {
     @Column({ length: 50 })
     type: string
 
-    @Column()
+    @Column({ type: 'longtext' })
     value: string
 
     @Column({ length: 255 })

+ 13 - 0
src/users/dto/user-register.dto.ts

@@ -0,0 +1,13 @@
+import { IsOptional, IsString } from 'class-validator'
+
+export class UserRegisterDto {
+    @IsString()
+    readonly username: string
+
+    @IsString()
+    readonly password: string
+
+    @IsOptional()
+    @IsString()
+    readonly invitor?: number
+}

+ 22 - 6
src/users/users.service.ts

@@ -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
     }