Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions api/src/auth/otp/otp.controller.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { Body, Controller, Logger, Post } from '@nestjs/common';
import { Body, Controller, Inject, Logger, Post } from '@nestjs/common';
import { ApiCreatedResponse, ApiProperty, ApiResponse, ApiResponseProperty, ApiTags } from '@nestjs/swagger';
import { IsMobilePhone, IsNumberString, IsUUID, Length } from 'class-validator';
import { v4 as uuid4 } from 'uuid';
import { UsersService } from '../../database/users/users.service';

class OtpSendBody {
@ApiProperty({required: true, example: '+918800233266'})
Expand Down Expand Up @@ -47,6 +48,9 @@ class OtpVerifyResponse {
@Controller('otp')
export class OtpController {

@Inject() private readonly users!: UsersService;


@ApiCreatedResponse({type: OtpSendResponse})
@Post('/')
async requestOtp(@Body() reqOtp: OtpSendBody): Promise<OtpSendResponse> {
Expand All @@ -60,10 +64,11 @@ export class OtpController {
@ApiResponse({type: OtpVerifyResponse})
@Post('/verify')
async verifyOtp(@Body() otpVerify: OtpVerifyBody): Promise<OtpVerifyResponse> {
Logger.debug(JSON.stringify(otpVerify), 'OTP_VERIFY')
Logger.debug(JSON.stringify(otpVerify), 'OTP_VERIFY');
const [ user, isNewUser ] = await this.users.findOrCreate(otpVerify.phno);
return new OtpVerifyResponse(
uuid4().toString(),
true
isNewUser
);
}
}
12 changes: 11 additions & 1 deletion api/src/database/users/users.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export class UsersService {
private authRepo!: AuthtokenRepository;

async findByAuthToken(token: string): Promise<User> {
return await this.authRepo.validateToken(token)
return await this.authRepo.validateToken(token);
}

async findById(id: number): Promise<User> {
Expand All @@ -27,4 +27,14 @@ export class UsersService {
return await this.repo.findAll();
}

async findOrCreate(number: string): Promise<[ User, boolean ]> {
const user = await this.repo.findByPhNo(number);
if (user != null)
if (user.email == null || user.player == null)
return [ user, true ];
else
return [ user, false ];
else return [ await this.repo.createUser({emailid: '', phno: number}), true ];
}

}