refactor: handle authentication state in middleware
This commit is contained in:
@@ -1,20 +1,25 @@
|
||||
import { LoadingOverlay } from "@mantine/core";
|
||||
import { useRouter } from "next/router";
|
||||
import SignInForm from "../../components/auth/SignInForm";
|
||||
import Meta from "../../components/Meta";
|
||||
import useUser from "../../hooks/user.hook";
|
||||
|
||||
const SignIn = () => {
|
||||
const { user } = useUser();
|
||||
const router = useRouter();
|
||||
const { user } = useUser();
|
||||
|
||||
// If the access token is expired, the middleware redirects to this page.
|
||||
// If the refresh token is still valid, the user will be redirected to the home page.
|
||||
if (user) {
|
||||
router.replace("/");
|
||||
} else {
|
||||
return (
|
||||
<>
|
||||
<Meta title="Sign In" />
|
||||
<SignInForm />
|
||||
</>
|
||||
);
|
||||
return <LoadingOverlay overlayOpacity={1} visible />;
|
||||
}
|
||||
|
||||
return (
|
||||
<>
|
||||
<Meta title="Sign In" />
|
||||
<SignInForm />
|
||||
</>
|
||||
);
|
||||
};
|
||||
export default SignIn;
|
||||
|
||||
@@ -1,24 +1,12 @@
|
||||
import { useRouter } from "next/router";
|
||||
import SignUpForm from "../../components/auth/SignUpForm";
|
||||
import Meta from "../../components/Meta";
|
||||
import useConfig from "../../hooks/config.hook";
|
||||
import useUser from "../../hooks/user.hook";
|
||||
|
||||
const SignUp = () => {
|
||||
const config = useConfig();
|
||||
const { user } = useUser();
|
||||
const router = useRouter();
|
||||
if (user) {
|
||||
router.replace("/");
|
||||
} else if (!config.get("ALLOW_REGISTRATION")) {
|
||||
router.replace("/auth/signIn");
|
||||
} else {
|
||||
return (
|
||||
<>
|
||||
<Meta title="Sign Up" />
|
||||
<SignUpForm />
|
||||
</>
|
||||
);
|
||||
}
|
||||
return (
|
||||
<>
|
||||
<Meta title="Sign Up" />
|
||||
<SignUpForm />
|
||||
</>
|
||||
);
|
||||
};
|
||||
export default SignUp;
|
||||
|
||||
Reference in New Issue
Block a user