mediahub-fe/types/auth.ts

172 lines
3.9 KiB
TypeScript

import { z } from "zod";
// Base schemas for validation
export const loginSchema = z.object({
username: z
.string()
.min(1, { message: "Username is required" })
.min(3, { message: "Username must be at least 3 characters" })
.max(50, { message: "Username must be less than 50 characters" }),
password: z
.string()
.min(1, { message: "Password is required" })
.min(6, { message: "Password must be at least 6 characters" })
.max(100, { message: "Password must be less than 100 characters" }),
});
export const emailValidationSchema = z.object({
oldEmail: z
.string()
.min(1, { message: "Old email is required" })
.email({ message: "Please enter a valid email address" }),
newEmail: z
.string()
.min(1, { message: "New email is required" })
.email({ message: "Please enter a valid email address" }),
});
export const otpSchema = z.object({
otp: z
.string()
.length(6, { message: "OTP must be exactly 6 digits" })
.regex(/^\d{6}$/, { message: "OTP must contain only numbers" }),
});
// Inferred types from schemas
export type LoginFormData = z.infer<typeof loginSchema>;
export type EmailValidationData = z.infer<typeof emailValidationSchema>;
export type OTPData = z.infer<typeof otpSchema>;
// API response types
export interface LoginResponse {
data?: {
access_token: string;
refresh_token: string;
};
error?: boolean;
message?: string;
}
export interface ProfileData {
id: string;
username: string;
fullname: string;
email: string;
roleId: number;
role: {
name: string;
};
userLevel: {
id: number;
name: string;
levelNumber: number;
parentLevelId: number;
province?: {
provName: string;
};
};
profilePictureUrl?: string;
homePath?: string;
instituteId?: string;
isInternational: boolean;
isActive: boolean;
isDelete: boolean;
}
export interface ProfileResponse {
data: {
data: ProfileData;
};
}
export interface EmailValidationResponse {
data?: {
message: string;
};
error?: boolean;
message?: string;
}
export interface OTPResponse {
message: string;
error?: boolean;
}
// Component props types
export interface AuthLayoutProps {
children: React.ReactNode;
showSidebar?: boolean;
className?: string;
}
export interface LoginFormProps {
onSuccess?: (data: LoginFormData) => void;
onError?: (error: string) => void;
className?: string;
}
export interface EmailSetupFormProps {
loginCredentials?: LoginFormData | null;
onSuccess?: () => void;
onError?: (error: string) => void;
onBack?: () => void;
className?: string;
}
export interface OTPFormProps {
loginCredentials?: LoginFormData | null;
onSuccess?: () => void;
onError?: (error: string) => void;
onResend?: () => void;
className?: string;
}
// Auth state types
export interface AuthState {
isAuthenticated: boolean;
user: ProfileData | null;
loading: boolean;
error: string | null;
}
export interface AuthContextType extends AuthState {
login: (credentials: LoginFormData) => Promise<void>;
logout: () => void;
refreshToken: () => Promise<void>;
}
// Role types
export interface Role {
id: number;
name: string;
description?: string;
}
// Navigation types
export interface NavigationConfig {
roleId: number;
path: string;
label: string;
}
// Cookie types
export interface AuthCookies {
access_token: string;
refresh_token: string;
time_refresh: string;
is_first_login: string;
home_path: string;
profile_picture: string;
state: string;
"state-prov": string;
uie: string; // user id encrypted
urie: string; // user role id encrypted
urne: string; // user role name encrypted
ulie: string; // user level id encrypted
uplie: string; // user parent level id encrypted
ulne: string; // user level number encrypted
ufne: string; // user fullname encrypted
ulnae: string; // user level name encrypted
uinse: string; // user institute id encrypted
status: string;
}