52 lines
1.7 KiB
TypeScript
52 lines
1.7 KiB
TypeScript
import { APIGatewayProxyEvent, APIGatewayProxyResult, Context } from 'aws-lambda';
|
|
import { safeHandler } from '../../../../common/utils/handlers/safeHandler';
|
|
import { prismaClient } from '../../../../common/database/prisma.lambda.service';
|
|
import { UserService } from '../../services/user.service';
|
|
import ApiError from '../../../../common/utils/helper/ApiError';
|
|
import { TokenService } from '../../../host/services/token.service';
|
|
|
|
const userService = new UserService(prismaClient);
|
|
const tokenService = new TokenService(prismaClient);
|
|
|
|
export const handler = safeHandler(async (
|
|
event: APIGatewayProxyEvent,
|
|
context?: Context
|
|
): Promise<APIGatewayProxyResult> => {
|
|
// Parse request body
|
|
let body: { mobileNumber?: string; otp?: string };
|
|
|
|
try {
|
|
body = event.body ? JSON.parse(event.body) : {};
|
|
} catch (error) {
|
|
throw new ApiError(400, 'Invalid JSON in request body');
|
|
}
|
|
|
|
const { mobileNumber, otp } = body;
|
|
|
|
if (!mobileNumber || !otp) {
|
|
throw new ApiError(400, 'Mobile number and OTP are required');
|
|
}
|
|
|
|
await userService.verifyHostOtp(mobileNumber, otp);
|
|
const user = await userService.getUserByMobileNumber(mobileNumber);
|
|
const generateTokenForUser = await tokenService.generateAuthToken(
|
|
user.id
|
|
);
|
|
|
|
return {
|
|
statusCode: 200,
|
|
headers: {
|
|
'Content-Type': 'application/json',
|
|
'Access-Control-Allow-Origin': '*',
|
|
},
|
|
body: JSON.stringify({
|
|
success: true,
|
|
message: 'OTP verified successfully',
|
|
accessToken: generateTokenForUser.access.token,
|
|
refreshToken: generateTokenForUser.refresh.token,
|
|
data: null,
|
|
}),
|
|
};
|
|
});
|
|
|