login & register
This commit is contained in:
parent
43b750313a
commit
0bd884a9fa
@ -9,9 +9,9 @@ import { LoginInputDto } from './dto/login-input.dto'
|
||||
export class AuthController {
|
||||
constructor(private readonly authService: AuthService) {}
|
||||
|
||||
@Post('api/signup')
|
||||
@Post('api/register')
|
||||
async register(@Body() userData: CreateUserDto) {
|
||||
return this.authService.createUser(userData)
|
||||
return this.authService.register(userData)
|
||||
}
|
||||
|
||||
@Post('api/login')
|
||||
|
@ -2,12 +2,11 @@ import { Module } from '@nestjs/common'
|
||||
import { AuthService } from './auth.service'
|
||||
import { AuthController } from './auth.controller'
|
||||
import { UsersModule } from 'src/users/users.module'
|
||||
import { PasswordService } from './password.service'
|
||||
import { JwtService } from '@nestjs/jwt'
|
||||
|
||||
@Module({
|
||||
controllers: [AuthController],
|
||||
providers: [AuthService, PasswordService, JwtService],
|
||||
providers: [AuthService, JwtService],
|
||||
imports: [UsersModule],
|
||||
})
|
||||
export class AuthModule {}
|
||||
|
@ -1,47 +1,26 @@
|
||||
import {
|
||||
Injectable,
|
||||
NotFoundException,
|
||||
BadRequestException,
|
||||
} from '@nestjs/common'
|
||||
import { PrismaService } from 'nestjs-prisma'
|
||||
import { Prisma } from '@prisma/client'
|
||||
import { PasswordService } from './password.service'
|
||||
import { Token, UserToken } from './models/token.model'
|
||||
import { Injectable, BadRequestException } from '@nestjs/common'
|
||||
import { PasswordService } from 'src/users/password.service'
|
||||
import { Token } from './models/token.model'
|
||||
import { JwtService } from '@nestjs/jwt'
|
||||
import { ConfigService } from '@nestjs/config'
|
||||
|
||||
import { UsersService } from 'src/users/users.service'
|
||||
import { CreateUserDto } from 'src/users/dto/create-user.dto'
|
||||
@Injectable()
|
||||
export class AuthService {
|
||||
constructor(
|
||||
private prisma: PrismaService,
|
||||
private passwordService: PasswordService,
|
||||
private jwtService: JwtService,
|
||||
private configService: ConfigService,
|
||||
private userService: UsersService,
|
||||
) {}
|
||||
|
||||
async createUser(payload: Prisma.UserCreateInput): Promise<UserToken> {
|
||||
const hashedPassword = await this.passwordService.hashPassword(
|
||||
payload.password,
|
||||
)
|
||||
const user = await this.prisma.user.create({
|
||||
data: {
|
||||
...payload,
|
||||
password: hashedPassword,
|
||||
},
|
||||
})
|
||||
|
||||
return {
|
||||
...this.generateTokens({ userId: user.id }),
|
||||
...this.exclude(user, ['password']),
|
||||
}
|
||||
async register(payload: CreateUserDto) {
|
||||
const user = await this.userService.createUser(payload)
|
||||
return this.generateTokens({ userId: user.id })
|
||||
}
|
||||
|
||||
async login(email: string, password: string): Promise<UserToken> {
|
||||
const user = await this.prisma.user.findUnique({ where: { email } })
|
||||
|
||||
if (!user) {
|
||||
throw new NotFoundException(`No user found for email: ${email}`)
|
||||
}
|
||||
async login(email: string, password: string) {
|
||||
const user = await this.userService.findUser({ email })
|
||||
|
||||
const passwordValid = await this.passwordService.validatePassword(
|
||||
password,
|
||||
@ -52,20 +31,7 @@ export class AuthService {
|
||||
throw new BadRequestException('Invalid password')
|
||||
}
|
||||
|
||||
return {
|
||||
...this.generateTokens({ userId: user.id }),
|
||||
...this.exclude(user, ['password']),
|
||||
}
|
||||
}
|
||||
|
||||
private exclude<User, Key extends keyof User>(
|
||||
user: User,
|
||||
keys: Key[],
|
||||
): Omit<User, Key> {
|
||||
for (const key of keys) {
|
||||
delete user[key]
|
||||
}
|
||||
return user
|
||||
return this.generateTokens({ userId: user.id })
|
||||
}
|
||||
|
||||
private generateTokens(payload: { userId: string }): Token {
|
||||
|
@ -1,6 +1,5 @@
|
||||
import '@nestjs/mapped-types'
|
||||
import { IntersectionType, ApiProperty, OmitType } from '@nestjs/swagger'
|
||||
import { UserEntity } from 'src/users/entities/user.entity'
|
||||
import { ApiProperty } from '@nestjs/swagger'
|
||||
|
||||
export class Token {
|
||||
@ApiProperty()
|
||||
@ -8,8 +7,3 @@ export class Token {
|
||||
@ApiProperty()
|
||||
refreshToken: string
|
||||
}
|
||||
|
||||
export class UserToken extends IntersectionType(
|
||||
Token,
|
||||
OmitType(UserEntity, ['password']),
|
||||
) {}
|
||||
|
@ -1,10 +1,17 @@
|
||||
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
|
||||
}
|
||||
|
@ -1,10 +1,11 @@
|
||||
import { Module } from '@nestjs/common'
|
||||
import { UsersService } from './users.service'
|
||||
import { UsersController } from './users.controller'
|
||||
import { PasswordService } from './password.service'
|
||||
|
||||
@Module({
|
||||
controllers: [UsersController],
|
||||
providers: [UsersService],
|
||||
exports: [UsersService],
|
||||
providers: [UsersService, PasswordService],
|
||||
exports: [UsersService, PasswordService],
|
||||
})
|
||||
export class UsersModule {}
|
||||
|
@ -1,7 +1,37 @@
|
||||
import { Injectable } from '@nestjs/common'
|
||||
import { Injectable, NotFoundException } from '@nestjs/common'
|
||||
import { PrismaService } from 'nestjs-prisma'
|
||||
import { CreateUserDto } from './dto/create-user.dto'
|
||||
import { PasswordService } from './password.service'
|
||||
import { Prisma } from '@prisma/client'
|
||||
|
||||
@Injectable()
|
||||
export class UsersService {
|
||||
constructor(private prisma: PrismaService) {}
|
||||
constructor(
|
||||
private prisma: PrismaService,
|
||||
private passwordService: PasswordService,
|
||||
) {}
|
||||
|
||||
async findUser(where: Prisma.UserWhereUniqueInput) {
|
||||
const user = await this.prisma.user.findUnique({
|
||||
where,
|
||||
})
|
||||
if (!user) {
|
||||
throw new NotFoundException(`No user found`)
|
||||
}
|
||||
return user
|
||||
}
|
||||
|
||||
async createUser(payload: CreateUserDto) {
|
||||
const hashedPassword = await this.passwordService.hashPassword(
|
||||
payload.password,
|
||||
)
|
||||
const user = await this.prisma.user.create({
|
||||
data: {
|
||||
...payload,
|
||||
password: hashedPassword,
|
||||
},
|
||||
})
|
||||
delete user.password
|
||||
return user
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user