rename user/profile
This commit is contained in:
parent
b166294b6b
commit
faee8db0f2
@ -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 { AuthService } from './auth.service'
|
||||||
import { CreateUserDto } from 'src/users/dto/create-user.dto'
|
import { CreateUserDto } from 'src/users/dto/create-user.dto'
|
||||||
import { ApiTags } from '@nestjs/swagger'
|
import { ApiTags } from '@nestjs/swagger'
|
||||||
import { LoginInputDto } from './dto/login-input.dto'
|
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'
|
import { UsersService } from 'src/users/users.service'
|
||||||
|
|
||||||
@ApiTags('auth')
|
@ApiTags('Auth')
|
||||||
@Controller()
|
@Controller('api/auth')
|
||||||
export class AuthController {
|
export class AuthController {
|
||||||
constructor(
|
constructor(
|
||||||
private readonly authService: AuthService,
|
private readonly authService: AuthService,
|
||||||
private readonly userService: UsersService,
|
private readonly userService: UsersService,
|
||||||
) {}
|
) {}
|
||||||
|
|
||||||
@Post('api/register')
|
@Post('register')
|
||||||
async register(@Body() userData: CreateUserDto) {
|
async register(@Body() userData: CreateUserDto) {
|
||||||
return this.authService.register(userData)
|
return this.authService.register(userData)
|
||||||
}
|
}
|
||||||
|
|
||||||
@Post('api/login')
|
@Post('login')
|
||||||
async login(@Body() user: LoginInputDto) {
|
async login(@Body() user: LoginInputDto) {
|
||||||
return this.authService.login(user.email, user.password)
|
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
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
12
src/common/decorators/user.decorator.ts
Normal file
12
src/common/decorators/user.decorator.ts
Normal 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
|
||||||
|
},
|
||||||
|
)
|
@ -1,17 +1,10 @@
|
|||||||
import { User } from '@prisma/client'
|
import { User } from '@prisma/client'
|
||||||
import { ApiProperty } from '@nestjs/swagger'
|
|
||||||
|
|
||||||
export class UserEntity implements User {
|
export class UserEntity implements User {
|
||||||
@ApiProperty()
|
|
||||||
id: string
|
id: string
|
||||||
@ApiProperty()
|
|
||||||
email: string
|
email: string
|
||||||
@ApiProperty()
|
|
||||||
username: string | null
|
username: string | null
|
||||||
@ApiProperty()
|
|
||||||
password: string
|
password: string
|
||||||
@ApiProperty()
|
|
||||||
createdAt: Date
|
createdAt: Date
|
||||||
@ApiProperty()
|
|
||||||
updatedAt: Date
|
updatedAt: Date
|
||||||
}
|
}
|
||||||
|
@ -1,9 +1,18 @@
|
|||||||
import { Controller } from '@nestjs/common'
|
import { Controller, Get } from '@nestjs/common'
|
||||||
import { UsersService } from './users.service'
|
import { UsersService } from './users.service'
|
||||||
import { ApiTags } from '@nestjs/swagger'
|
import { ApiTags } from '@nestjs/swagger'
|
||||||
|
import { User } from 'src/common/decorators/user.decorator'
|
||||||
|
import { NeedAuth } from 'src/common/decorators/need-auth.decorator'
|
||||||
|
|
||||||
@ApiTags('user')
|
@ApiTags('User')
|
||||||
@Controller()
|
@Controller('api/user')
|
||||||
export class UsersController {
|
export class UsersController {
|
||||||
constructor(private readonly userService: UsersService) {}
|
constructor(private readonly userService: UsersService) {}
|
||||||
|
|
||||||
|
@NeedAuth()
|
||||||
|
@Get('profile')
|
||||||
|
async getUserInfo(@User('userId') userId: string) {
|
||||||
|
const user = await this.userService.findUser({ id: userId })
|
||||||
|
return user
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -3,6 +3,7 @@ import { PrismaService } from 'nestjs-prisma'
|
|||||||
import { CreateUserDto } from './dto/create-user.dto'
|
import { CreateUserDto } from './dto/create-user.dto'
|
||||||
import { PasswordService } from './password.service'
|
import { PasswordService } from './password.service'
|
||||||
import { Prisma } from '@prisma/client'
|
import { Prisma } from '@prisma/client'
|
||||||
|
import { UserEntity } from './entities/user.entity'
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class UsersService {
|
export class UsersService {
|
||||||
@ -11,7 +12,7 @@ export class UsersService {
|
|||||||
private passwordService: PasswordService,
|
private passwordService: PasswordService,
|
||||||
) {}
|
) {}
|
||||||
|
|
||||||
async findUser(where: Prisma.UserWhereUniqueInput) {
|
async findUser(where: Prisma.UserWhereUniqueInput): Promise<UserEntity> {
|
||||||
const user = await this.prisma.user.findUnique({
|
const user = await this.prisma.user.findUnique({
|
||||||
where,
|
where,
|
||||||
})
|
})
|
||||||
|
Loading…
Reference in New Issue
Block a user