rename user/profile

This commit is contained in:
秦秋旭 2023-02-17 14:21:08 +08:00
parent b166294b6b
commit faee8db0f2
5 changed files with 31 additions and 25 deletions

View File

@ -1,34 +1,25 @@
import { Body, Controller, Get, Post } from '@nestjs/common'
import { Body, Controller, Post } from '@nestjs/common'
import { AuthService } from './auth.service'
import { CreateUserDto } from 'src/users/dto/create-user.dto'
import { ApiTags } from '@nestjs/swagger'
import { LoginInputDto } from './dto/login-input.dto'
import { Token } from 'src/common/decorators/token.decorator'
import { NeedAuth } from 'src/common/decorators/need-auth.decorator'
import { UsersService } from 'src/users/users.service'
@ApiTags('auth')
@Controller()
@ApiTags('Auth')
@Controller('api/auth')
export class AuthController {
constructor(
private readonly authService: AuthService,
private readonly userService: UsersService,
) {}
@Post('api/register')
@Post('register')
async register(@Body() userData: CreateUserDto) {
return this.authService.register(userData)
}
@Post('api/login')
@Post('login')
async login(@Body() user: LoginInputDto) {
return this.authService.login(user.email, user.password)
}
@NeedAuth()
@Get('api/profile')
async getUserInfo(@Token('userId') userId: string) {
const user = await this.userService.findUser({ id: userId })
return user
}
}

View File

@ -0,0 +1,12 @@
import { createParamDecorator, ExecutionContext } from '@nestjs/common'
import { type Request } from 'express'
import { TokenPayload } from 'src/auth/dto/token.dto'
export const User = createParamDecorator(
(key: keyof TokenPayload, ctx: ExecutionContext) => {
const request = ctx.switchToHttp().getRequest<Request>()
const token = request.user as TokenPayload
return key ? token?.[key] : token
},
)

View File

@ -1,17 +1,10 @@
import { User } from '@prisma/client'
import { ApiProperty } from '@nestjs/swagger'
export class UserEntity implements User {
@ApiProperty()
id: string
@ApiProperty()
email: string
@ApiProperty()
username: string | null
@ApiProperty()
password: string
@ApiProperty()
createdAt: Date
@ApiProperty()
updatedAt: Date
}

View File

@ -1,9 +1,18 @@
import { Controller } from '@nestjs/common'
import { Controller, Get } from '@nestjs/common'
import { UsersService } from './users.service'
import { ApiTags } from '@nestjs/swagger'
import { User } from 'src/common/decorators/user.decorator'
import { NeedAuth } from 'src/common/decorators/need-auth.decorator'
@ApiTags('user')
@Controller()
@ApiTags('User')
@Controller('api/user')
export class UsersController {
constructor(private readonly userService: UsersService) {}
@NeedAuth()
@Get('profile')
async getUserInfo(@User('userId') userId: string) {
const user = await this.userService.findUser({ id: userId })
return user
}
}

View File

@ -3,6 +3,7 @@ import { PrismaService } from 'nestjs-prisma'
import { CreateUserDto } from './dto/create-user.dto'
import { PasswordService } from './password.service'
import { Prisma } from '@prisma/client'
import { UserEntity } from './entities/user.entity'
@Injectable()
export class UsersService {
@ -11,7 +12,7 @@ export class UsersService {
private passwordService: PasswordService,
) {}
async findUser(where: Prisma.UserWhereUniqueInput) {
async findUser(where: Prisma.UserWhereUniqueInput): Promise<UserEntity> {
const user = await this.prisma.user.findUnique({
where,
})