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

Private and Signed Out only routes #261

Merged
merged 4 commits into from
Jul 24, 2023
Merged
Show file tree
Hide file tree
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
7 changes: 4 additions & 3 deletions src/pages/signin.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { useClearCurrentComponent } from '@/hooks/useClearCurrentComponent';
import { useDefaultLayout } from '@/hooks/useLayout';
import { useSignInRedirect } from '@/hooks/useSignInRedirect';
import { useAuthStore } from '@/stores/auth';
import signedOutRoute from '@/utils/route/signedOutRoute';
import type { NextPageWithLayout } from '@/utils/types';

import { handleCreateAccount } from '../utils/auth';
Expand All @@ -21,14 +22,14 @@ const SignInPage: NextPageWithLayout = () => {
const signedIn = useAuthStore((store) => store.signedIn);
const { redirect } = useSignInRedirect();

useClearCurrentComponent();

useEffect(() => {
if (signedIn) {
redirect();
}
}, [redirect, signedIn]);

useClearCurrentComponent();

const onSubmit = handleSubmit(async (data) => {
if (!data.email) return;

Expand Down Expand Up @@ -85,7 +86,7 @@ const SignInPage: NextPageWithLayout = () => {
};
SignInPage.getLayout = useDefaultLayout;

export default SignInPage;
export default signedOutRoute(SignInPage);

const StyledContainer = styled.div`
width: 100%;
Expand Down
7 changes: 4 additions & 3 deletions src/pages/signup.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,12 @@ const SignUpPage: NextPageWithLayout = () => {
openToast({
title: '',
type: 'INFO',
description: 'Passkey support is required for account creation. Try using an updated version of Chrome or Safari to create an account.',
description:
'Passkey support is required for account creation. Try using an updated version of Chrome or Safari to create an account.',
duration: 5000,
})
});
}
}
};
checkPassKey();
}, []);

Expand Down
20 changes: 20 additions & 0 deletions src/utils/route/privateRoute.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { useAuthStore } from '@/stores/auth';
import type { NextComponentType } from 'next';
import { useRouter } from 'next/router';

const privateRoute = (Component: NextComponentType) => {
const Private = (props: any) => {
const signedIn = useAuthStore((store) => store.signedIn);
const router = useRouter();

if (!signedIn && router) {
// `signin` or `signup`?
router.push('/signup');
}
return <Component {...props} />;
};

return Private;
};

export default privateRoute;
21 changes: 21 additions & 0 deletions src/utils/route/signedOutRoute.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { useRouter } from 'next/router';

import { useAuthStore } from '@/stores/auth';

import type { NextPageWithLayout } from '../types';

const signedOutRoute = (Component: NextPageWithLayout) => {
const SignedOut = (props: NextPageWithLayout) => {
const signedIn = useAuthStore((store) => store.signedIn);
const router = useRouter();

if (signedIn && router) {
router.push('/');
}
return <Component {...props} />;
};

return SignedOut;
};

export default signedOutRoute;