feat( auth ): make email optional in signup/login

This commit is contained in:
Abubeker Yasin
2026-07-10 13:38:53 +03:00
parent 4705f5c478
commit 8321c697ec
4 changed files with 60 additions and 21 deletions

View File

@@ -1,4 +1,4 @@
import { IsEmail, IsString, ValidateNested } from 'class-validator';
import { IsEmail, IsNotEmpty, IsString, ValidateNested } from 'class-validator';
import { Type } from 'class-transformer';
import { ApiProperty } from '@nestjs/swagger';
@@ -13,8 +13,13 @@ export class NameDto {
}
export class RegisterDto {
@ApiProperty({ example: 'kelemu@email.com' })
@IsEmail()
// Accepts an email OR a phone number. When a passenger signs up without an email,
// the portal passes the phone number here (and as `username`) — the IAM only requires
// a non-empty string, so a phone value is a valid account identifier. Kept as
// @IsString/@IsNotEmpty (not @IsEmail) so that phone-as-email passes the ValidationPipe.
@ApiProperty({ example: 'kelemu@email.com', description: 'Email or, when the user has no email, their phone number' })
@IsString()
@IsNotEmpty()
email: string;
@ApiProperty({ example: 'kelemu.ketsela' })
@@ -42,8 +47,12 @@ export class ResendRegistrationCodeDto {
}
export class LoginDto {
@ApiProperty({ example: 'kelemu@email.com' })
@IsEmail()
// Accepts an email OR a phone number in the same field. Passengers who registered
// without an email log in with their phone number, which the IAM matches. Kept as
// @IsString/@IsNotEmpty (not @IsEmail) so a phone value passes the ValidationPipe.
@ApiProperty({ example: 'kelemu@email.com', description: 'Email or phone number' })
@IsString()
@IsNotEmpty()
email: string;
@ApiProperty({ example: 'password123', format: 'password' })

View File

@@ -184,8 +184,11 @@ export class PassengerAuthService {
const { token, refreshToken } = iamResult as { token: string; refreshToken: string };
// `dto.email` may hold an email OR a phone number (passengers without an email log in
// with their phone). Match on either so the post-auth lookup works regardless of which
// identifier was used.
const iamRows = await this.dataSource.query<IamUserRow[]>(
`SELECT id, email, name, phone_number, metadata FROM iam.users WHERE email = $1 LIMIT 1`,
`SELECT id, email, name, phone_number, metadata FROM iam.users WHERE email = $1 OR phone_number = $1 LIMIT 1`,
[dto.email],
);
const iamUser = iamRows[0];
@@ -210,7 +213,7 @@ export class PassengerAuthService {
return {
token,
refreshToken,
user: { id: iamUser.id, iamUserId: iamUser.id, email: dto.email, passengerId: passenger.id },
user: { id: iamUser.id, iamUserId: iamUser.id, email: iamUser.email, passengerId: passenger.id },
};
}