feat: add simple login logic

This commit is contained in:
2026-02-18 09:13:23 +03:30
parent b9779f7cb6
commit e657264807
16 changed files with 259 additions and 13 deletions
+19
View File
@@ -0,0 +1,19 @@
import { Body, Controller, HttpCode, Post } from '@nestjs/common';
import { LoginDTO } from './dtos/login.dto';
import { AuthService } from './providers/auth.service';
@Controller('users')
export class AuthController {
constructor(
/**
* Inject Auth Service
*/
private readonly authService: AuthService,
) {}
@Post('login')
@HttpCode(200)
public async logIn(@Body() loginDto: LoginDTO) {
return await this.authService.logIn(loginDto);
}
}
+11
View File
@@ -0,0 +1,11 @@
import { Module } from '@nestjs/common';
import { AuthController } from './auth.controller';
import { AuthService } from './providers/auth.service';
import { UsersModule } from '../users/users.module';
@Module({
imports: [UsersModule],
controllers: [AuthController],
providers: [AuthService],
})
export class AuthModule {}
+41
View File
@@ -0,0 +1,41 @@
import Utils from '@/common/utils';
import { ApiProperty } from '@nestjs/swagger';
import { Transform } from 'class-transformer';
import { IsNotEmpty, IsOptional, IsString, Matches } from 'class-validator';
import { i18nValidationMessage as t } from 'nestjs-i18n';
export class LoginDTO {
@ApiProperty({
type: 'string',
description: `User's Phone Number`,
example: '09121111111',
required: true,
})
@IsString()
@IsNotEmpty({
message: t('validation.requiredField', {
field: '$t(auth.fields.phone)',
}),
})
@Matches(/^(?:09|9)[0-9]{9}$/, {
message: t('validation.wrongField', {
field: '$t(auth.fields.phone)',
}),
})
@Transform(({ value }) => Utils.StringUtils.normalizePhone(value as string))
phone: string;
@ApiProperty({
type: 'string',
description: 'Password for User Account',
example: 'Password1@',
required: false,
})
@IsOptional()
@IsString({
message: t('validation.wrongFieldFormat', {
field: '$t(auth.fields.password)',
}),
})
password?: string;
}
@@ -0,0 +1,7 @@
POST http://localhost:3000/users/login
Content-Type: application/json
{
"phone": "09333026363",
"password": "rad1.3.5"
}
@@ -0,0 +1,45 @@
import { UsersService } from '@/modules/users/providers/users.service';
import { Injectable, UnauthorizedException } from '@nestjs/common';
import { LoginDTO } from '../dtos/login.dto';
@Injectable()
export class AuthService {
constructor(
/**
* Inject Users Service
*/
private readonly usersService: UsersService,
) {}
public async logIn(loginDto: LoginDTO) {
const { phone, password } = loginDto;
const user = await this.usersService.findOneByPhone(phone);
if (!user) {
// TODO: Send OTP
return {
newUser: true,
message: `Otp sent to ${phone}`,
};
}
if (!password) {
return {
newUser: false,
message: 'Please send password',
};
}
// TODO: Hash password and compare
if (password === user.password) {
// TODO: Create JWT
return { access: 'ACCESS_TOKEN', refresh: 'REFRESH_TOKEN' };
}
throw new UnauthorizedException(
'Entered phone number or password is wrong.',
);
}
}