Redirect 401 to password page instead error page

This commit is contained in:
mbaharip
2023-05-25 22:53:40 +07:00
parent e06acf1ed7
commit a820c746d7
4 changed files with 71 additions and 4 deletions
+10 -2
View File
@@ -1,12 +1,20 @@
"use client";
import { useRouter } from "next/navigation";
import { useState } from "react";
import passwordHash from "utils/encryptionHelper/passwordHash";
import { Constant } from "types/general/constant";
function Password({ path }: { path: string }) {
function Password({
path,
redirect,
}: {
path: string;
redirect: string;
}) {
const router = useRouter();
const [password, setPassword] = useState<string>("");
const handlePasswordSubmit = (
@@ -42,7 +50,7 @@ function Password({ path }: { path: string }) {
)}; expires=${expires.toUTCString()}; path=/`;
}
window.location.reload();
router.push(redirect);
};
return (
+11 -1
View File
@@ -1,6 +1,6 @@
import { Metadata, ResolvingMetadata } from "next";
import { cookies } from "next/headers";
import { notFound } from "next/navigation";
import { notFound, redirect } from "next/navigation";
import Breadcrumb from "components/compBreadcrumb";
import FileLayout from "components/compFileLayout";
@@ -120,6 +120,16 @@ async function FilePage({ params }: Props) {
if (!pathValidation.success) {
const errorData = pathValidation as API_Error;
if (errorData.code === 401) {
const protectedPath = errorData.reason
.split('"')[1]
.split('"')[0];
redirect(
`/password?redirect=${params.path.join(
"/",
)}&path=${protectedPath}`,
);
}
if (errorData.code === 404) {
notFound();
}
+9 -1
View File
@@ -1,6 +1,6 @@
import { Metadata } from "next";
import { cookies } from "next/headers";
import { notFound } from "next/navigation";
import { notFound, redirect } from "next/navigation";
import Breadcrumb from "components/compBreadcrumb";
import FileLayout from "components/compFileLayout";
@@ -75,6 +75,14 @@ async function RootPage() {
if (!pathValidation.success) {
const errorData = pathValidation as API_Error;
if (errorData.code === 401) {
const protectedPath = errorData.reason
.split('"')[1]
.split('"')[0];
redirect(
`/password?redirect=/&path=${protectedPath}`,
);
}
if (errorData.code === 404) {
notFound();
}
+41
View File
@@ -0,0 +1,41 @@
import { redirect } from "next/navigation";
import CompPassword from "components/compPassword";
type Props = {
searchParams: {
[key: string]: string | string[] | undefined;
};
};
function Password({ searchParams }: Props) {
if (
searchParams["redirect"] === undefined ||
searchParams["path"] === undefined
) {
redirect("/");
}
return (
<div
className={
"relative flex h-full min-h-[80dvh] w-full flex-grow flex-col items-center justify-center gap-8"
}
>
{/* Image placeholder */}
<div
className={
"grid h-48 w-48 place-items-center rounded-lg bg-blue-300 font-semibold text-zinc-900"
}
>
Placeholder Image
</div>
<CompPassword
path={searchParams["path"] as string}
redirect={searchParams["redirect"] as string}
/>
</div>
);
}
export default Password;