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