test
This commit is contained in:
parent
496b0257b0
commit
bd79394c89
|
|
@ -11,41 +11,53 @@ import LotteryCountDown from "@/components/lottery/countDown/LotteryCountDown";
|
||||||
import { Queries } from "@/api/queries";
|
import { Queries } from "@/api/queries";
|
||||||
import Link from "next/link";
|
import Link from "next/link";
|
||||||
import { useRouter } from "next/navigation";
|
import { useRouter } from "next/navigation";
|
||||||
|
import Loader from "@/components/Loader";
|
||||||
|
|
||||||
const LotteryPage = () => {
|
const LotteryPage = () => {
|
||||||
const { lotteryData, setAuth } = useLotteryAuth();
|
const { lotteryData, setAuth } = useLotteryAuth();
|
||||||
const [status, setStatus] = useState<"not-started" | "started" | "ended">(
|
const [status, setStatus] = useState<"not-started" | "started" | "ended">(
|
||||||
"not-started"
|
"not-started"
|
||||||
);
|
);
|
||||||
|
const [isLoading, setIsLoading] = useState(true);
|
||||||
const router = useRouter();
|
const router = useRouter();
|
||||||
|
|
||||||
// ✅ Fetch fresh data on page load
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
|
const checkAuth = async () => {
|
||||||
|
// ✅ Check credentials from localStorage
|
||||||
const phone = localStorage.getItem("lotteryPhone");
|
const phone = localStorage.getItem("lotteryPhone");
|
||||||
const code = localStorage.getItem("lotteryCode");
|
const code = localStorage.getItem("lotteryCode");
|
||||||
|
|
||||||
if (phone && code) {
|
if (phone && code) {
|
||||||
Queries.authenticateLottery(phone, code)
|
try {
|
||||||
.then((response) => {
|
// ✅ Authenticate using stored credentials
|
||||||
|
const response = await Queries.authenticateLottery(phone, code);
|
||||||
|
|
||||||
if (response.errorMessage) {
|
if (response.errorMessage) {
|
||||||
// If authentication fails, redirect to the auth page
|
// If authentication fails, redirect to the auth page
|
||||||
router.replace("/lottery/auth");
|
router.replace("/lottery/auth");
|
||||||
} else {
|
} else {
|
||||||
// ✅ Set the authenticated state
|
// ✅ Set the authenticated state
|
||||||
setAuth(response, phone, code);
|
setAuth(response, phone, code);
|
||||||
|
setIsLoading(false);
|
||||||
}
|
}
|
||||||
})
|
} catch (err) {
|
||||||
.catch((err) => {
|
|
||||||
console.error("Failed to fetch lottery data:", err);
|
|
||||||
console.error("Authentication failed:", err);
|
console.error("Authentication failed:", err);
|
||||||
router.replace("/lottery/auth");
|
router.replace("/lottery/auth");
|
||||||
});
|
|
||||||
}
|
}
|
||||||
}, [setAuth]);
|
} else {
|
||||||
|
// Redirect to the auth page if no credentials are found
|
||||||
|
router.replace("/lottery/auth");
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
// if (!lotteryData?.errorMessage) {
|
checkAuth();
|
||||||
// router.replace("/lottery/auth");
|
}, [router, setAuth]);
|
||||||
// }
|
|
||||||
|
if (isLoading) {
|
||||||
|
<div className="flex w-full h-[90vh] justify-center items-center">
|
||||||
|
<Loader />
|
||||||
|
</div>;
|
||||||
|
}
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<ProtectedRoute>
|
<ProtectedRoute>
|
||||||
|
|
|
||||||
|
|
@ -7,7 +7,7 @@ import { Queries } from "@/api/queries";
|
||||||
|
|
||||||
const ProtectedRoute = ({ children }: { children: React.ReactNode }) => {
|
const ProtectedRoute = ({ children }: { children: React.ReactNode }) => {
|
||||||
const router = useRouter();
|
const router = useRouter();
|
||||||
const { isAuthenticated, setAuth } = useLotteryAuth();
|
const { setAuth } = useLotteryAuth();
|
||||||
const [isLoading, setIsLoading] = useState(true);
|
const [isLoading, setIsLoading] = useState(true);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue