generated from muhtadeetaron/nextjs-template
fix(api): fix api endpoint logic #2
This commit is contained in:
@ -10,13 +10,14 @@ import { login } from "@/lib/auth";
|
|||||||
import DestructibleAlert from "@/components/DestructibleAlert";
|
import DestructibleAlert from "@/components/DestructibleAlert";
|
||||||
import { useAuth } from "@/context/AuthContext";
|
import { useAuth } from "@/context/AuthContext";
|
||||||
import { LoginForm } from "@/types/auth";
|
import { LoginForm } from "@/types/auth";
|
||||||
|
import { CircleAlert } from "lucide-react";
|
||||||
|
|
||||||
const LoginPage = () => {
|
const LoginPage = () => {
|
||||||
const router = useRouter();
|
const router = useRouter();
|
||||||
const { setToken } = useAuth();
|
const { setToken } = useAuth();
|
||||||
|
|
||||||
const [form, setForm] = useState<LoginForm>({
|
const [form, setForm] = useState<LoginForm>({
|
||||||
email: "",
|
identifier: "",
|
||||||
password: "",
|
password: "",
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -71,11 +72,11 @@ const LoginPage = () => {
|
|||||||
<div className="flex flex-col justify-between gap-10">
|
<div className="flex flex-col justify-between gap-10">
|
||||||
<div className="flex flex-col w-full gap-5">
|
<div className="flex flex-col w-full gap-5">
|
||||||
<FormField
|
<FormField
|
||||||
title="Email Address"
|
title="Email \ Username"
|
||||||
value={form.email}
|
value={form.identifier}
|
||||||
placeholder="Enter your email address..."
|
placeholder="Enter your email address..."
|
||||||
handleChangeText={(value) =>
|
handleChangeText={(value) =>
|
||||||
setForm({ ...form, email: value })
|
setForm({ ...form, identifier: value })
|
||||||
}
|
}
|
||||||
/>
|
/>
|
||||||
<FormField
|
<FormField
|
||||||
@ -87,9 +88,13 @@ const LoginPage = () => {
|
|||||||
}
|
}
|
||||||
/>
|
/>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
{error && <DestructibleAlert text={error} extraStyles="" />}
|
{error && <DestructibleAlert text={error} extraStyles="" />}
|
||||||
|
|
||||||
|
<h1 className="flex justify-center items-center gap-2 bg-green-200 p-4 rounded-full">
|
||||||
|
<CircleAlert size={20} />
|
||||||
|
Your login details will be remembered.
|
||||||
|
</h1>
|
||||||
|
|
||||||
<button
|
<button
|
||||||
onClick={loginUser}
|
onClick={loginUser}
|
||||||
disabled={isLoading}
|
disabled={isLoading}
|
||||||
|
|||||||
@ -28,34 +28,34 @@ const HomePage = () => {
|
|||||||
const [linkedViews, setLinkedViews] = useState<GalleryViews[]>();
|
const [linkedViews, setLinkedViews] = useState<GalleryViews[]>();
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
let isMounted = true;
|
// let isMounted = true;
|
||||||
|
|
||||||
const fetchBoardData = async () => {
|
// const fetchBoardData = async () => {
|
||||||
try {
|
// try {
|
||||||
const response = await fetch(`${API_URL}/leaderboard`);
|
// const response = await fetch(`${API_URL}/leaderboard`);
|
||||||
if (!response.ok) {
|
// if (!response.ok) {
|
||||||
throw new Error("Failed to fetch leaderboard data");
|
// throw new Error("Failed to fetch leaderboard data");
|
||||||
}
|
// }
|
||||||
|
|
||||||
const data: LeaderboardEntry[] = await response.json();
|
// const data: LeaderboardEntry[] = await response.json();
|
||||||
if (isMounted) setBoardData(data);
|
// if (isMounted) setBoardData(data);
|
||||||
} catch (err) {
|
// } catch (err) {
|
||||||
if (isMounted) {
|
// if (isMounted) {
|
||||||
const message =
|
// const message =
|
||||||
err instanceof Error ? err.message : "An unexpected error occurred";
|
// err instanceof Error ? err.message : "An unexpected error occurred";
|
||||||
setBoardError(message);
|
// setBoardError(message);
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
};
|
// };
|
||||||
|
|
||||||
const fetchedLinkedViews: GalleryViews[] = getLinkedViews();
|
const fetchedLinkedViews: GalleryViews[] = getLinkedViews();
|
||||||
setLinkedViews(fetchedLinkedViews);
|
setLinkedViews(fetchedLinkedViews);
|
||||||
|
|
||||||
fetchBoardData();
|
// fetchBoardData();
|
||||||
|
|
||||||
return () => {
|
// return () => {
|
||||||
isMounted = false;
|
// isMounted = false;
|
||||||
};
|
// };
|
||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
@ -144,7 +144,8 @@ const HomePage = () => {
|
|||||||
{/* Leaderboard Section */}
|
{/* Leaderboard Section */}
|
||||||
<div className={styles.leaderboardWrapper}>
|
<div className={styles.leaderboardWrapper}>
|
||||||
<h2 className={styles.sectionTitle}>Leaderboard</h2>
|
<h2 className={styles.sectionTitle}>Leaderboard</h2>
|
||||||
<div className={styles.leaderboardContainer}>
|
<p className="text-center text-xl">Coming Soon.</p>
|
||||||
|
{/* <div className={styles.leaderboardContainer}>
|
||||||
<div className={styles.topThreeHeader}>
|
<div className={styles.topThreeHeader}>
|
||||||
<span className={styles.topThreeTitle}>Top 3</span>
|
<span className={styles.topThreeTitle}>Top 3</span>
|
||||||
<button
|
<button
|
||||||
@ -175,7 +176,7 @@ const HomePage = () => {
|
|||||||
))
|
))
|
||||||
)}
|
)}
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div> */}
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
{/* Performance Summary Section */}
|
{/* Performance Summary Section */}
|
||||||
|
|||||||
@ -59,8 +59,6 @@ export const AuthProvider: React.FC<{ children: React.ReactNode }> = ({
|
|||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const initializeAuth = () => {
|
const initializeAuth = () => {
|
||||||
const storedToken = getCookie("authToken");
|
const storedToken = getCookie("authToken");
|
||||||
console.log("Current pathname:", pathname);
|
|
||||||
console.log("Stored token:", storedToken);
|
|
||||||
|
|
||||||
if (storedToken) {
|
if (storedToken) {
|
||||||
setTokenState(storedToken);
|
setTokenState(storedToken);
|
||||||
@ -69,7 +67,6 @@ export const AuthProvider: React.FC<{ children: React.ReactNode }> = ({
|
|||||||
pathname === "/login" ||
|
pathname === "/login" ||
|
||||||
pathname === "/register"
|
pathname === "/register"
|
||||||
) {
|
) {
|
||||||
console.log("Redirecting to /home");
|
|
||||||
router.replace("/home");
|
router.replace("/home");
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
14
lib/auth.ts
14
lib/auth.ts
@ -1,4 +1,6 @@
|
|||||||
export const API_URL = "https://examjam-api.pptx704.com";
|
import { LoginForm, RegisterForm } from "@/types/auth";
|
||||||
|
|
||||||
|
export const API_URL = "https://examjam-backend.omukk.dev";
|
||||||
|
|
||||||
// Cookie utility function
|
// Cookie utility function
|
||||||
const setCookie = (name: string, value: string | null, days: number = 7) => {
|
const setCookie = (name: string, value: string | null, days: number = 7) => {
|
||||||
@ -15,12 +17,6 @@ const setCookie = (name: string, value: string | null, days: number = 7) => {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
interface AuthForm {
|
|
||||||
email: string;
|
|
||||||
password: string;
|
|
||||||
[key: string]: any; // for flexibility
|
|
||||||
}
|
|
||||||
|
|
||||||
type SetTokenFn = (token: string) => void;
|
type SetTokenFn = (token: string) => void;
|
||||||
|
|
||||||
// Optional: Create a custom error type to carry extra data
|
// Optional: Create a custom error type to carry extra data
|
||||||
@ -29,7 +25,7 @@ interface APIError extends Error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export const login = async (
|
export const login = async (
|
||||||
form: AuthForm,
|
form: LoginForm,
|
||||||
setToken: SetTokenFn
|
setToken: SetTokenFn
|
||||||
): Promise<void> => {
|
): Promise<void> => {
|
||||||
const response = await fetch(`${API_URL}/auth/login`, {
|
const response = await fetch(`${API_URL}/auth/login`, {
|
||||||
@ -51,7 +47,7 @@ export const login = async (
|
|||||||
};
|
};
|
||||||
|
|
||||||
export const register = async (
|
export const register = async (
|
||||||
form: AuthForm,
|
form: RegisterForm,
|
||||||
setToken: SetTokenFn
|
setToken: SetTokenFn
|
||||||
): Promise<void> => {
|
): Promise<void> => {
|
||||||
const response = await fetch(`${API_URL}/auth/register`, {
|
const response = await fetch(`${API_URL}/auth/register`, {
|
||||||
|
|||||||
2
types/auth.d.ts
vendored
2
types/auth.d.ts
vendored
@ -22,6 +22,6 @@ export interface RegisterForm {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export interface LoginForm {
|
export interface LoginForm {
|
||||||
email: string;
|
identifier: string;
|
||||||
password: string;
|
password: string;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user