fix(auth): enhance user login response handling and update User schema
- Log the response data on successful login for debugging purposes - Improve error handling in user service by using safeParse for login response validation - Update User schema to allow null values for optional fields, enhancing flexibility in user data handling
This commit is contained in:
@@ -19,7 +19,8 @@ export const useLoginQuery = (callback?: () => void) => {
|
|||||||
mutationFn: userService.login,
|
mutationFn: userService.login,
|
||||||
onSuccess: (response) => {
|
onSuccess: (response) => {
|
||||||
setAuthState(response.data);
|
setAuthState(response.data);
|
||||||
router.replace("/charts");
|
console.log(response);
|
||||||
|
|
||||||
if (callback) {
|
if (callback) {
|
||||||
callback();
|
callback();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -19,7 +19,11 @@ export const userService = {
|
|||||||
try {
|
try {
|
||||||
const response = await api.post(API_ENDPOINTS.USER.LOGIN, credentials);
|
const response = await api.post(API_ENDPOINTS.USER.LOGIN, credentials);
|
||||||
|
|
||||||
return LoginResponseSchema.parse(response.data);
|
const result = LoginResponseSchema.safeParse(response.data);
|
||||||
|
if (!result.success) {
|
||||||
|
throw new Error(result.error.message);
|
||||||
|
}
|
||||||
|
return result.data;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error("ERROR caught in User Service Login:", error);
|
console.error("ERROR caught in User Service Login:", error);
|
||||||
throw error;
|
throw error;
|
||||||
|
|||||||
+14
-14
@@ -5,21 +5,21 @@ export const UserSchema = z.object({
|
|||||||
id: z.string().regex(/^[0-9a-fA-F]{24}$/, {
|
id: z.string().regex(/^[0-9a-fA-F]{24}$/, {
|
||||||
message: "Invalid ObjectId",
|
message: "Invalid ObjectId",
|
||||||
}),
|
}),
|
||||||
full_name: z.string(),
|
full_name: z.string().optional().or(z.null()),
|
||||||
username: z.string().optional(),
|
username: z.string().optional().or(z.null()),
|
||||||
email: z.string().email().optional(),
|
email: z.string().email().optional().or(z.null()),
|
||||||
role: z.string().optional(),
|
role: z.string().optional().or(z.null()),
|
||||||
status: z.string().optional(),
|
status: z.string().optional().or(z.null()),
|
||||||
company_id: z.string().optional(),
|
company_id: z.string().optional().or(z.null()),
|
||||||
department: z.string().optional(),
|
department: z.string().optional().or(z.null()),
|
||||||
position: z.string().optional(),
|
position: z.string().optional().or(z.null()),
|
||||||
phone: z.string().optional(),
|
phone: z.string().optional().or(z.null()),
|
||||||
profile_image: z.string().or(z.null()).optional(),
|
profile_image: z.string().or(z.null()).optional(),
|
||||||
is_verified: z.boolean().optional(),
|
is_verified: z.boolean().optional().or(z.null()),
|
||||||
last_login_at: z.number().or(z.null()).optional(),
|
last_login_at: z.number().or(z.null()).optional().or(z.null()),
|
||||||
created_at: z.number().optional(),
|
created_at: z.number().optional().or(z.null()),
|
||||||
updated_at: z.number().optional(),
|
updated_at: z.number().optional().or(z.null()),
|
||||||
updated_by: z.string().optional(),
|
updated_by: z.string().optional().or(z.null()),
|
||||||
});
|
});
|
||||||
|
|
||||||
export const CreateAdminSchema = z.object({
|
export const CreateAdminSchema = z.object({
|
||||||
|
|||||||
Reference in New Issue
Block a user