@battle-racing/br-common-lib
Version:
Common library for all Battle Racing Repositorios
90 lines (89 loc) • 3.76 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.userLoginInputSchema = exports.userRegistrationInputSchema = exports.userCompleteProfileSchema = exports.userVerifySchema = exports.userInitialRegistrationSchema = exports.userWithPasswordSchema = exports.authUserSchema = exports.userSchema = exports.emailOrPhoneSchema = exports.PHONE_REGEX = exports.userTypeSchema = void 0;
const zod_1 = require("zod");
const role_1 = require("../role");
const User_const_1 = require("./User.const");
exports.userTypeSchema = zod_1.z.enum(User_const_1.USER_TYPE);
exports.PHONE_REGEX = /^([+]?[\\s0-9]+)?(\\d{3}|[(]?[0-9]+[)])?([-]?[\\s]?[0-9])+$/;
const MIN_PASSWORD_LENGTH = 6;
exports.emailOrPhoneSchema = zod_1.z
.string()
.min(1, 'Email or phone is required')
.refine((val) => {
const isEmail = zod_1.z.email().safeParse(val).success;
const isPhone = exports.PHONE_REGEX.test(val);
return isEmail || isPhone;
}, { message: 'Must be a valid email or phone number' });
const baseUserObj = zod_1.z.object({
id: zod_1.z.uuidv4(),
name: zod_1.z.string(),
lastName: zod_1.z.string(),
email: zod_1.z.email().toLowerCase().optional().nullable(),
phoneNumber: zod_1.z.string().regex(exports.PHONE_REGEX, 'Invalid phone number format').optional().nullable(),
// --- ACCESS CONTROL ---
role: role_1.userRoleSchema,
type: exports.userTypeSchema,
isActive: zod_1.z.boolean().default(true),
// TODO: Implement mechanism to validate users later by email or phone
isEmailVerified: zod_1.z.boolean().default(false),
isPhoneVerified: zod_1.z.boolean().default(false),
defaultPlayerId: zod_1.z.uuidv4().optional(),
createdAt: zod_1.z.date(),
lastLoginAt: zod_1.z.date().optional(),
});
exports.userSchema = baseUserObj.refine((data) => data.email || data.phoneNumber, {
message: 'User must have either an email or a phone number',
path: ['email', 'phoneNumber'],
});
/**
* The schema for the user returned on login
*/
exports.authUserSchema = baseUserObj.extend({
authToken: zod_1.z.string(),
});
exports.userWithPasswordSchema = exports.userSchema.and(zod_1.z.object({
password: zod_1.z.string().min(MIN_PASSWORD_LENGTH),
}));
exports.userInitialRegistrationSchema = zod_1.z.object({
emailOrPhone: exports.emailOrPhoneSchema,
password: zod_1.z.string().min(MIN_PASSWORD_LENGTH, `Password must be at least ${MIN_PASSWORD_LENGTH} characters`),
});
exports.userVerifySchema = zod_1.z.object({
emailOrPhone: exports.emailOrPhoneSchema,
code: zod_1.z.string().min(6, 'Code must be at least 6 characters').max(10, 'Code is too long'),
});
exports.userCompleteProfileSchema = baseUserObj
.pick({
name: true,
})
.extend({
lastName: zod_1.z.string().optional(),
nickname: zod_1.z.string().min(2),
birthDate: zod_1.z.coerce.date(),
waiverSignature: zod_1.z.boolean().refine((val) => val === true, {
message: 'You must accept the waiver signature',
}),
teamName: zod_1.z.string().optional(),
});
// Deprecated in favor of multi-step registration
exports.userRegistrationInputSchema = baseUserObj
.pick({
name: true,
lastName: true,
email: true,
phoneNumber: true,
})
.extend({
password: zod_1.z.string().min(6),
nickname: zod_1.z.string().min(2),
birthDate: zod_1.z.coerce.date(),
})
.refine((data) => data.email || data.phoneNumber, {
message: 'User must have either an email or a phone number',
path: ['email', 'phoneNumber'],
});
exports.userLoginInputSchema = zod_1.z.object({
emailOrPhone: exports.emailOrPhoneSchema,
password: zod_1.z.string().min(MIN_PASSWORD_LENGTH, `Password must be at least ${MIN_PASSWORD_LENGTH} characters`),
});