Skip to content
This repository has been archived by the owner on Aug 9, 2023. It is now read-only.

Commit

Permalink
feat: protect Baan-selection
Browse files Browse the repository at this point in the history
  • Loading branch information
crimsonf09 committed Jul 12, 2023
1 parent f49a4ee commit 6e78870
Showing 1 changed file with 39 additions and 0 deletions.
39 changes: 39 additions & 0 deletions src/pages/protectRoutes.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { exchangeTicketForToken } from '@/utils/auth';
import { useRouter } from 'next/router';
import { useCallback, useEffect, useState } from 'react';

const Login = () => {
const { query, replace, isReady } = useRouter();
const timeCloseRegister = new Date('2022-07-24T12:00:00+07:00');
const time = new Date();
const handleLogin = useCallback(async (ticket: string) => {
const token = await exchangeTicketForToken(ticket);
localStorage.setItem('token', JSON.stringify(token));

window.location.href = '/register';
}, []);

useEffect(() => {
if (isReady) {
if (query?.ticket) {
if (time >= timeCloseRegister) {
//window.location.href = '/registerClose';
} else {
handleLogin(query.ticket as string);
}
} else {
replace('/');
if (time < timeCloseRegister) {
window.location.href = '/baan-selection';
}
}
}
}, [query, replace, handleLogin, isReady]);

return (
<h1>hi</h1>
//content
);
};

export default Login;

0 comments on commit 6e78870

Please sign in to comment.