Skip to content

Commit

Permalink
feat: modify process
Browse files Browse the repository at this point in the history
  • Loading branch information
TzuHanLiang committed Aug 15, 2024
1 parent 08301a9 commit ec737bb
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 17 deletions.
31 changes: 26 additions & 5 deletions src/components/login_page_body/login_page_body.beta.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ const Loader = () => {
);
};

const isJwtExpired = (expires: string) => {
const isJwtExpired = (expires: string | undefined) => {
if (!expires) return true;
const now = new Date();
const expirationDate = new Date(expires);
return now > expirationDate;
Expand Down Expand Up @@ -92,11 +93,15 @@ const LoginPageBody = () => {
}
};

const handleUserAuthenticated = async () => {
const handleUserAuthenticated = async (force?: boolean) => {
// Deprecate: (20240816-Tzuhan) dev
// eslint-disable-next-line no-console
console.log('user:', user);
setSelectedProvider(user.provider);
if (user?.hasReadAgreement) {
agreeWithInfomationConfirmModalVisibilityHandler(false);
router.push(ISUNFA_ROUTE.SELECT_COMPANY);
} else if (!isAgreeWithInfomationConfirmModalVisible && !hasShowModal) {
} else if ((!isAgreeWithInfomationConfirmModalVisible && !hasShowModal) || force) {
agreeWithInfomationConfirmModalVisibilityHandler(true);
setHasShowModal(true);
}
Expand All @@ -106,7 +111,7 @@ const LoginPageBody = () => {
// Deprecate: (20240816-Tzuhan) dev
// eslint-disable-next-line no-console
console.log(
'userAgreeWithInfomationANDTOSNPrivacyPolicy',
'useEffect userAgreeWithInfomationANDTOSNPrivacyPolicy',
userAgreeWithInfomationANDTOSNPrivacyPolicy
);
if (userAgreeWithInfomationANDTOSNPrivacyPolicy) handleUserAgree(user.id);
Expand All @@ -131,16 +136,30 @@ const LoginPageBody = () => {
}

if (status === 'authenticated') {
// Deprecate: (20240816-Tzuhan) dev
// eslint-disable-next-line no-console
console.log(`useEffect: status === 'authenticated'`);
handleUserAuthenticated();
}
}, [status]);

const authenticateUser = async (provider: Provider) => {
try {
// Deprecate: (20240816-Tzuhan) dev
// eslint-disable-next-line no-console
console.log(
`(selectedProvider === provider: ${selectedProvider === provider}) provider: ${provider}, selectedProvider: ${selectedProvider}, status: ${status}`
);
if (selectedProvider === provider && status === 'authenticated') {
const session = await getSession();
// Deprecate: (20240816-Tzuhan) dev
// eslint-disable-next-line no-console
console.log(
`!isJwtExpired(session.expires): ${!isJwtExpired(session?.expires)}, session:`,
session
);
if (session && !isJwtExpired(session.expires)) {
handleUserAuthenticated();
handleUserAuthenticated(true);
return;
}
}
Expand Down Expand Up @@ -178,11 +197,13 @@ const LoginPageBody = () => {
</div>
<div className="flex flex-col space-y-4">
<AuthButton onClick={() => authenticateUser(Provider.GOOGLE)} provider="Google" />
{/* Info: (20240813 - Tzuhan) Apple login is not provided in the beta version
<AuthButton
onClick={() => authenticateUser(Provider.APPLE)}
provider="Apple"
disabled
/>
*/}
</div>
</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 @@ -71,6 +71,7 @@ export default NextAuth({
clientId: process.env.GOOGLE_CLIENT_ID as string,
clientSecret: process.env.GOOGLE_CLIENT_SECRET as string,
}),
// Info: (20240813 - Tzuhan) Apple login is not provided in the beta version
// AppleProvider({
// clientId: process.env.APPLE_CLIENT_ID as string,
// clientSecret: generateAppleClientSecret(),
Expand Down
23 changes: 11 additions & 12 deletions src/pages/users/login-beta.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,21 @@ import { serverSideTranslations } from 'next-i18next/serverSideTranslations';
import NavBar from '@/components/nav_bar/nav_bar';
import LoginPageBody from '@/components/login_page_body/login_page_body.beta';
import { GetServerSideProps } from 'next';
// import { useUserCtx } from '@/contexts/user_context';
// import { SkeletonList } from '@/components/skeleton/skeleton';
// import { DEFAULT_SKELETON_COUNT_FOR_PAGE } from '@/constants/display';
import { useUserCtx } from '@/contexts/user_context';
import { SkeletonList } from '@/components/skeleton/skeleton';
import { DEFAULT_SKELETON_COUNT_FOR_PAGE } from '@/constants/display';
import { useTranslation } from 'next-i18next';
import { ISUNFA_ROUTE } from '@/constants/url';

const LoginPage = () => {
const { t } = useTranslation('common');
// const { isAuthLoading } = useUserCtx();
const { isAuthLoading } = useUserCtx();

const displayedBody = (
// isAuthLoading ? (
// <div className="flex h-screen w-full items-center justify-center">
// <SkeletonList count={DEFAULT_SKELETON_COUNT_FOR_PAGE} />
// </div>
// ) :
const displayedBody = isAuthLoading ? (
<div className="flex h-screen w-full items-center justify-center">
<SkeletonList count={DEFAULT_SKELETON_COUNT_FOR_PAGE} />
</div>
) : (
<div className="pt-60px">
<LoginPageBody />
</div>
Expand Down Expand Up @@ -55,10 +54,10 @@ const LoginPage = () => {

export const getServerSideProps: GetServerSideProps = async ({ locale, query }) => {
try {
// const session = await getSession({ req });
const { invitation = '', action = '' } = query;

// 如果没有 session,重定向到登录页面
// Info: (20240815 - Tzuhan) Deprecate: 如果没有 session,重定向到登录页面
// const session = await getSession({ req });
// if (!session) {
// return {
// props: {
Expand Down

0 comments on commit ec737bb

Please sign in to comment.