Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 23 additions & 20 deletions client/src/app/(commonLayout)/(authRouteGroup)/login/_action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,15 @@ import { setTokenInCookies } from "@/lib/tokenUtils";
import { ApiErrorResponse } from "@/types/api.types";
import { ILoginResponse } from "@/types/auth.types";
import { ILoginPayload, loginZodSchema } from "@/zod/auth.validation";
import { redirect } from "next/navigation";

export const loginAction = async (payload : ILoginPayload, redirectPath ?: string ) : Promise<ILoginResponse | ApiErrorResponse> =>{
export interface LoginActionSuccessResponse {
success: true;
redirectTo: string;
}

export type LoginActionResponse = LoginActionSuccessResponse | ApiErrorResponse;

export const loginAction = async (payload : ILoginPayload, redirectPath ?: string ) : Promise<LoginActionResponse> =>{
const parsedPayload = loginZodSchema.safeParse(payload);

if(!parsedPayload.success){
Expand All @@ -24,34 +30,31 @@ export const loginAction = async (payload : ILoginPayload, redirectPath ?: strin
const response = await httpClient.post<ILoginResponse>("/auth/login", parsedPayload.data);

const { accessToken, refreshToken, token, user} = response.data;
const {role, emailVerified, needPasswordChange, email} = user;
const {role, needPasswordChange, email} = user;
await setTokenInCookies("accessToken", accessToken);
await setTokenInCookies("refreshToken", refreshToken);
await setTokenInCookies("better-auth.session_token", token, 24 * 60 * 60); // 1 day in seconds

// if(!emailVerified){
// redirect("/verify-email");
// }else // in the catch block

if(needPasswordChange){
//TODO : refactoring
redirect(`/reset-password?email=${email}`);
}else{
// redirect(redirectPath || "/dashboard");
const targetPath = redirectPath && isValidRedirectForRole(redirectPath, role as UserRole) ? redirectPath : getDefaultDashboardRoute(role as UserRole);


redirect(targetPath);
return {
success: true,
redirectTo: `/reset-password?email=${email}`,
};
}

const targetPath = redirectPath && isValidRedirectForRole(redirectPath, role as UserRole) ? redirectPath : getDefaultDashboardRoute(role as UserRole);
return {
success: true,
redirectTo: targetPath,
};

} catch (error : any) {
console.log(error, "error");
if(error && typeof error === "object" && "digest" in error && typeof error.digest === "string" && error.digest.startsWith("NEXT_REDIRECT")){
throw error;
}

if (error && error.response && error.response.data.message === "Email not verified") {
redirect(`/verify-email?email=${payload.email}`);
return {
success: true,
redirectTo: `/verify-email?email=${payload.email}`,
};
}
return {
success: false,
Expand Down
6 changes: 6 additions & 0 deletions client/src/components/modules/Auth/LoginForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { useForm } from "@tanstack/react-form";
import { useMutation } from "@tanstack/react-query";
import { Eye, EyeOff } from "lucide-react";
import Link from "next/link";
import { useRouter } from "next/navigation";
import { useState } from "react";

interface LoginFormProps {
Expand All @@ -19,6 +20,7 @@ interface LoginFormProps {

const LoginForm = ({ redirectPath }: LoginFormProps) => {
// const queryClient = useQueryClient();
const router = useRouter();

const [serverError, setServerError] = useState<string | null>(null);
const [showPassword, setShowPassword] = useState(false);
Expand All @@ -42,6 +44,10 @@ const LoginForm = ({ redirectPath }: LoginFormProps) => {
setServerError(result.message || "Login failed");
return ;
}

if(result.redirectTo){
router.push(result.redirectTo);
}
} catch (error : any) {
console.log(`Login failed: ${error.message}`);
setServerError(`Login failed: ${error.message}`);
Expand Down