Skip to content

Commit

Permalink
feat: update
Browse files Browse the repository at this point in the history
  • Loading branch information
TzuHanLiang committed Aug 14, 2024
1 parent b270d1b commit 754d522
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 16 deletions.
Binary file added public/images/login_bg.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
52 changes: 37 additions & 15 deletions src/components/login_page_body/login_page_body.beta.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import React, { useEffect, useState } from 'react';
import { signIn, useSession } from 'next-auth/react';
import { getSession, signIn, useSession } from 'next-auth/react';
import Image from 'next/image';
import { useGlobalCtx } from '@/contexts/global_context';
import APIHandler from '@/lib/utils/api_handler';
import { APIName } from '@/constants/api_connection';
import { useRouter } from 'next/router';
import { ISUNFA_ROUTE } from '@/constants/url';
// import { ToastType } from '@/interfaces/toastify';

enum Provider {
GOOGLE = 'google',
Expand Down Expand Up @@ -80,15 +81,23 @@ const Loader = () => {
);
};

const isJwtExpired = (expires: string) => {
const now = new Date();
const expirationDate = new Date(expires);
return now > expirationDate;
};

const LoginPageBody = () => {
const { status, data, update } = useSession();
const [isLoading, setIsLoading] = useState(false);
const [hasShowModal, setHasShowModal] = useState(false);
const [selectedProvider, setSelectedProvider] = useState<Provider | null>(null);
const router = useRouter();
const {
isAgreeWithInfomationConfirmModalVisible,
agreeWithInfomationConfirmModalVisibilityHandler,
TOSNPrivacyPolicyConfirmModalCallbackHandler,
// toastHandler,
} = useGlobalCtx();

const { trigger: agreementAPI } = APIHandler<null>(APIName.AGREE_TO_TERMS);
Expand All @@ -113,6 +122,21 @@ const LoginPageBody = () => {
}
};

const handleUserAuthenticated = async () => {
const user = data?.user;

if (user?.hasReadAgreement) {
agreeWithInfomationConfirmModalVisibilityHandler(false);
router.push(ISUNFA_ROUTE.SELECT_COMPANY);
} else {
TOSNPrivacyPolicyConfirmModalCallbackHandler(() => handleUserAgree(user.id));
if (!isAgreeWithInfomationConfirmModalVisible && !hasShowModal) {
agreeWithInfomationConfirmModalVisibilityHandler(true);
setHasShowModal(true);
}
}
};

useEffect(() => {
if (status === 'loading') {
setIsLoading(true);
Expand All @@ -131,23 +155,19 @@ const LoginPageBody = () => {
}

if (status === 'authenticated') {
const user = data?.user;

if (user?.hasReadAgreement) {
agreeWithInfomationConfirmModalVisibilityHandler(false);
router.push(ISUNFA_ROUTE.SELECT_COMPANY);
} else {
TOSNPrivacyPolicyConfirmModalCallbackHandler(() => handleUserAgree(user.id));
if (!isAgreeWithInfomationConfirmModalVisible && !hasShowModal) {
agreeWithInfomationConfirmModalVisibilityHandler(true);
setHasShowModal(true);
}
}
handleUserAuthenticated();
}
}, [status]);

const authenticateUser = async (provider: Provider) => {
try {
if (selectedProvider === provider && status === 'authenticated') {
const session = await getSession();
if (session && !isJwtExpired(session.expires)) {
handleUserAuthenticated();
return;
}
}
setIsLoading(true);
const response = await signIn(provider, { redirect: false });
setIsLoading(false);
Expand All @@ -159,6 +179,7 @@ const LoginPageBody = () => {
}

update?.();
setSelectedProvider(provider);
} catch (error) {
// eslint-disable-next-line no-console
console.error('Authentication failed', error);
Expand All @@ -170,10 +191,11 @@ const LoginPageBody = () => {
<div className="relative flex h-screen flex-col items-center justify-center text-center">
<div className="absolute inset-0 z-0 h-full w-full blur-md">
<Image
priority
src="/images/login_bg.svg"
alt="login_bg"
layout="fill"
objectFit="cover"
fill
style={{ objectFit: 'cover' }}
className="blur-md"
/>
</div>
Expand Down
1 change: 1 addition & 0 deletions src/pages/api/auth/[...nextauth].ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ export default NextAuth({
hasReadAgreement: token.hasReadAgreement as boolean,
provider: token.provider as string,
accessToken: token.accessToken as string,
expires: token.exp as string,
},
};
// Deprecated: (20240815 - Tzuhan) Remove console.log
Expand Down
3 changes: 2 additions & 1 deletion src/types/next-auth.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@ import NextAuth from 'next-auth';

declare module 'next-auth' {
interface Session {
expires: string;
user: {
id: string;
hasReadAgreement: boolean;
} & DefaultSession['user'];
} & DefaultSession['user'] ;
}

interface User {
Expand Down

0 comments on commit 754d522

Please sign in to comment.