Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add freshy-night and baan protect route #173

Merged
merged 1 commit into from
Jul 24, 2024
Merged
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
145 changes: 91 additions & 54 deletions src/context/AuthContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import Spinner from '@/components/firstdate/Spinner';
import { User } from '@/types/user';
import { getReceiveGiftCondition } from '@/utils/reward';
import { getCurrentTime } from '@/utils/time';
import { getUser, isUserRegistered } from '@/utils/user';

import { usePathname, useRouter } from 'next/navigation';
Expand Down Expand Up @@ -49,75 +50,111 @@ const AuthProvider: React.FC<{ children: ReactNode }> = ({
}, []);

useEffect(() => {
setisReady(false);
const protectRoute = async () => {
setisReady(false);

// check if user is logged in
const userStr = localStorage.getItem('user');
if (!userStr) {
setisReady(true);
return router.push('/');
}

if (path == '/healthz') {
return setisReady(true);
}
const userObj: User = JSON.parse(userStr);
setUser(userObj);

const userStr = localStorage.getItem('user');
if (!userStr) {
setisReady(true);
return router.push('/');
}
const isStaff = userObj.role == 'staff';
const isStaffPage = path.includes('/staff');
const isRegistered = isUserRegistered(userObj);
const isRegisterPage = path.includes('register');

const userObj: User = JSON.parse(userStr);
setUser(userObj);
if (isStaff) {
// staff route protection
if (path == '/') {
return router.push('firstdate/staff/home');
}

const isStaff = userObj.role == 'staff';
const isStaffPage = path.includes('/staff');
const isRegistered = isUserRegistered(userObj);
const isRegisterPage = path.includes('register');
if (!isRegistered && !isRegisterPage) {
return router.push('/staff/register');
}

if (isStaff) {
if (path == '/') {
return router.push('firstdate/staff/home');
}
if (!isStaffPage) {
return router.push('/firstdate/staff/home');
}
} else {
// user route protection
if (path == '/') {
return router.push('/home');
}

if (!isRegistered && !isRegisterPage) {
return router.push('/staff/register');
}
if (!isStaffPage) {
return router.push('/firstdate/staff/home');
}
} else {
if (path == '/') {
return router.push('/home');
}
//check if user is registered
if (!isRegistered && !isRegisterPage) {
return router.push('/register');
}

if (!isRegistered && !isRegisterPage) {
return router.push('/register');
}
// check if user is eligible to receive gift
if (path.split('/').at(-1) == 'reward') {
const condition = getReceiveGiftCondition(userObj);

if (path.split('/').at(-1) == 'reward') {
const condition = getReceiveGiftCondition(userObj);
if (condition.status != 'ready') {
return router.push('/firstdate/home');
}
}

if (condition.status != 'ready') {
// check if it is staff page
if (isStaffPage) {
return router.push('/firstdate/home');
}
}

if (isStaffPage) {
return router.push('/firstdate/home');
}

if (path !== 'home') {
const firstdate = new Date(
process.env.NEXT_PUBLIC_FIRST_DATE_DATE as string
);
const rpkm = new Date(process.env.NEXT_PUBLIC_RUP_PEUN_DATE as string);
const current = new Date();

if (
(path.includes('firstdate') && current < firstdate) ||
(path.includes('rpkm') && current < rpkm)
) {
return router.push('/home');
if (path !== 'home') {
const firstdate = new Date(
process.env.NEXT_PUBLIC_FIRST_DATE_DATE as string
);
const startRPKM = new Date(
process.env.NEXT_PUBLIC_RUP_PEUN_DATE as string
);
const endBannSelect = new Date(
process.env.NEXT_PUBLIC_CLOSED_BAAN_SELECTION_DATE as string
);
const freshyNight = new Date(
process.env.NEXT_PUBLIC_FRESHY_NIGHT_DATE as string
);
const now = (await getCurrentTime()).currentTime;

//firstdate
if (path.includes('firstdate')) {
if (now < firstdate) {
return router.push('/home');
}
}

//RPKM
if (path.includes('rpkm')) {
if (now < startRPKM) {
return router.push('/home');
}

// end baan select
if (path.includes('/baan')) {
if (now > endBannSelect) {
return router.push('/rpkm/activities/home');
}
}

// start freshy night
if (path.includes('/freshy-night')) {
console.log(now, freshyNight);
if (now < freshyNight) {
return router.push('/rpkm/activities/home');
}
}
}
}
}
}

setisReady(true);
setisReady(true);
};
protectRoute();
}, [router, path]);

return (
Expand Down
Loading