forked from Yara724/api
Added linkToken and linkContext to OTP
This commit is contained in:
46
src/auth/auth-services/user-auth-error.ts
Normal file
46
src/auth/auth-services/user-auth-error.ts
Normal file
@@ -0,0 +1,46 @@
|
||||
import {
|
||||
BadRequestException,
|
||||
ForbiddenException,
|
||||
UnauthorizedException,
|
||||
} from "@nestjs/common";
|
||||
|
||||
export enum UserAuthErrorCode {
|
||||
USER_NOT_FOUND = "USER_NOT_FOUND",
|
||||
OTP_REQUIRED = "OTP_REQUIRED",
|
||||
OTP_EXPIRED = "OTP_EXPIRED",
|
||||
OTP_INVALID = "OTP_INVALID",
|
||||
OTP_REQUEST_TOO_SOON = "OTP_REQUEST_TOO_SOON",
|
||||
LINK_NOT_FOUND = "LINK_NOT_FOUND",
|
||||
LINK_MOBILE_MISMATCH = "LINK_MOBILE_MISMATCH",
|
||||
}
|
||||
|
||||
const messages: Record<UserAuthErrorCode, string> = {
|
||||
[UserAuthErrorCode.USER_NOT_FOUND]: "User not found",
|
||||
[UserAuthErrorCode.OTP_REQUIRED]: "Please request an OTP first",
|
||||
[UserAuthErrorCode.OTP_EXPIRED]: "OTP has expired",
|
||||
[UserAuthErrorCode.OTP_INVALID]: "OTP is invalid",
|
||||
[UserAuthErrorCode.OTP_REQUEST_TOO_SOON]:
|
||||
"Wait for expiry time to finish before requesting another OTP",
|
||||
[UserAuthErrorCode.LINK_NOT_FOUND]: "Linked SMS token was not found",
|
||||
[UserAuthErrorCode.LINK_MOBILE_MISMATCH]:
|
||||
"This mobile number is not allowed to use this SMS link",
|
||||
};
|
||||
|
||||
export function userAuthErrorBody(code: UserAuthErrorCode) {
|
||||
return {
|
||||
code,
|
||||
message: messages[code],
|
||||
};
|
||||
}
|
||||
|
||||
export function throwUserAuthError(code: UserAuthErrorCode): never {
|
||||
if (code === UserAuthErrorCode.OTP_REQUEST_TOO_SOON) {
|
||||
throw new BadRequestException(userAuthErrorBody(code));
|
||||
}
|
||||
|
||||
if (code === UserAuthErrorCode.LINK_MOBILE_MISMATCH) {
|
||||
throw new ForbiddenException(userAuthErrorBody(code));
|
||||
}
|
||||
|
||||
throw new UnauthorizedException(userAuthErrorBody(code));
|
||||
}
|
||||
Reference in New Issue
Block a user