Skip to content

Commit

Permalink
fix(fe): ⚡ update error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
lehuygiang28 committed Jun 14, 2024
1 parent 2704fba commit 15a17ec
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 41 deletions.
29 changes: 14 additions & 15 deletions apps/fe/src/app/(auth)/layout.tsx
Original file line number Diff line number Diff line change
@@ -1,22 +1,21 @@
import { getServerSession } from 'next-auth/next';
import { redirect } from 'next/navigation';
'use client';

import { PropsWithChildren } from 'react';
import { authOptions } from '~/libs/next-auth';
import { useIsAuthenticated } from '@refinedev/core';
import { useRouter } from 'next/navigation';
import LoadingPage from '../loading';

export default async function AuthLayout({ children }: PropsWithChildren) {
const data = await getData();
export default function AuthLayout({ children }: PropsWithChildren) {
const router = useRouter();
const { data, isLoading } = useIsAuthenticated();

if (!data.session?.user) {
return redirect('/login');
if (isLoading) {
return <LoadingPage />;
}

return <>{children}</>;
}

async function getData() {
const session = await getServerSession(authOptions);
if (!isLoading && !data?.authenticated) {
return router.replace('/login');
}

return {
session,
};
return <>{children}</>;
}
29 changes: 14 additions & 15 deletions apps/fe/src/app/(no-auth)/layout.tsx
Original file line number Diff line number Diff line change
@@ -1,22 +1,21 @@
import { getServerSession } from 'next-auth/next';
import { redirect } from 'next/navigation';
'use client';

import { PropsWithChildren } from 'react';
import { authOptions } from '~/libs/next-auth';
import { useIsAuthenticated } from '@refinedev/core';
import { useRouter } from 'next/navigation';
import LoadingPage from '../loading';

export default async function NoAuthLayout({ children }: PropsWithChildren) {
const data = await getData();
export default function NoAuthLayout({ children }: PropsWithChildren) {
const router = useRouter();
const { data, isLoading } = useIsAuthenticated();

if (data.session?.user) {
return redirect('/');
if (isLoading) {
return <LoadingPage />;
}

return <>{children}</>;
}

async function getData() {
const session = await getServerSession(authOptions);
if (!isLoading && data?.authenticated) {
return router.replace('/');
}

return {
session,
};
return <>{children}</>;
}
17 changes: 14 additions & 3 deletions apps/fe/src/components/pages/login/index.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use client';

import { useLogin } from '@refinedev/core';
import { useLogin, useNotification } from '@refinedev/core';
import { Space, Form, Input, Typography, Divider, Button } from 'antd';
import { MailOutlined, GithubOutlined, GoogleOutlined, ArrowLeftOutlined } from '@ant-design/icons';
import type { LoginActionPayload } from '~/providers/auth-provider/types';
Expand All @@ -25,6 +25,7 @@ export type LoginProps = {
export default function Login({ onBack }: LoginProps) {
const router = useRouter();
const params = useSearchParams();
const { open } = useNotification();
const { mutate: login } = useLogin();

const {
Expand All @@ -47,14 +48,24 @@ export default function Login({ onBack }: LoginProps) {

useEffect(() => {
const hash = params.get('hash');
if (hash && hash.length > SEEM_SAFE_HASH_LENGTH) {
if (hash && hash.length >= SEEM_SAFE_HASH_LENGTH) {
login({ type: 'login', hash });
} else if (hash) {
const cloneParams = new URLSearchParams(params);
cloneParams.delete('hash');
return router.replace(`/login?${cloneParams.toString()}`);
}
}, [params, login, router]);
}, [params, router, login, open]);

useEffect(() => {
if (params.get('error') === 'failed_to_login') {
open({
type: 'error',
message: 'Failed to login, please try again.',
key: 'failed_to_login',
});
}
}, [params, open]);

if (params.get('hash')) {
return <Loading />;
Expand Down
14 changes: 6 additions & 8 deletions apps/fe/src/libs/next-auth/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,6 @@ export const authOptions = {
async signIn({ user, account }: { user: User; account: Account | null }) {
if (account?.provider === 'google') {
try {
console.log(account);
console.log(account.id_token);

const { data: userData } = await axios.post<LoginResponseDto>(
'/auth/login/google',
{
Expand All @@ -70,13 +67,10 @@ export const authOptions = {
return true;
} catch (error) {
console.error(error);
return false;
throw new Error('failed_to_login');
}
} else if (account?.provider === 'github') {
try {
console.log(account);
console.log(account.id_token);

const { data: userData } = await axios.post<LoginResponseDto>(
'/auth/login/github',
{
Expand All @@ -100,7 +94,7 @@ export const authOptions = {
return true;
} catch (error) {
console.error(error);
return false;
throw new Error('failed_to_login');
}
}
return true;
Expand All @@ -125,6 +119,10 @@ export const authOptions = {
return baseUrl;
},
},
pages: {
signIn: '/login',
error: '/login',
},
logger: {
debug: (...data: unknown[]) => console.debug({ ...data }),
error: (...data: unknown[]) => console.error({ ...data }),
Expand Down

0 comments on commit 15a17ec

Please sign in to comment.