Stage 2: integrate administrative login and protect dashboard
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
// pages/admin/storage.js
|
||||
import { useState } from "react";
|
||||
import Head from "next/head";
|
||||
import { useRouter } from "next/router";
|
||||
import CookieBanner from "../../components/cookieBanner";
|
||||
import Header from "../../components/header";
|
||||
import Footer from "../../components/footer";
|
||||
@@ -29,6 +30,10 @@ import {
|
||||
setSearchResults
|
||||
} from "../../store/searchOutput/action";
|
||||
import { setCurrentPage } from "../../store/currentView/action";
|
||||
import {
|
||||
readAdminSessionCookie,
|
||||
validateAdminSession
|
||||
} from "../../lib/admin/adminAuth";
|
||||
|
||||
const StoragePage = (props) => {
|
||||
const {
|
||||
@@ -42,6 +47,27 @@ const StoragePage = (props) => {
|
||||
statusLPAObj
|
||||
} = props;
|
||||
const [whichTab, setWhichTab] = useState("appealDashboard");
|
||||
const [signingOut, setSigningOut] = useState(false);
|
||||
const router = useRouter();
|
||||
|
||||
const handleSignOut = async () => {
|
||||
setSigningOut(true);
|
||||
|
||||
try {
|
||||
const response = await fetch("/api/admin/auth/logout", {
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
credentials: "same-origin"
|
||||
});
|
||||
|
||||
if (response.ok) {
|
||||
await router.replace("/admin/login");
|
||||
return;
|
||||
}
|
||||
} finally {
|
||||
setSigningOut(false);
|
||||
}
|
||||
};
|
||||
|
||||
return (
|
||||
<div>
|
||||
@@ -57,6 +83,16 @@ const StoragePage = (props) => {
|
||||
style={{ width: "90%", margin: "auto" }}
|
||||
>
|
||||
<div className="js-enabled govuk-template__body govuk-frontend-supported">
|
||||
<div className="govuk-!-margin-top-4 govuk-!-text-align-right">
|
||||
<button
|
||||
type="button"
|
||||
className="govuk-button govuk-button--secondary"
|
||||
onClick={handleSignOut}
|
||||
disabled={signingOut}
|
||||
>
|
||||
{signingOut ? "Signing out..." : "Sign out"}
|
||||
</button>
|
||||
</div>
|
||||
<div className="govuk-tabs" data-module="govuk-tabs">
|
||||
<h2 className="govuk-tabs__title">Contents</h2>
|
||||
|
||||
@@ -199,6 +235,14 @@ const StoragePage = (props) => {
|
||||
export const getServerSideProps = wrapper.getServerSideProps(
|
||||
(store) => async (ctx) => {
|
||||
const { query, req, res } = ctx;
|
||||
const adminSession = readAdminSessionCookie(req);
|
||||
|
||||
if (!validateAdminSession(adminSession)) {
|
||||
return {
|
||||
redirect: { destination: "/admin/login", permanent: false }
|
||||
};
|
||||
}
|
||||
|
||||
// IP restriction check
|
||||
const ALLOWED_IPS = process.env.ALLOWED_IPS
|
||||
? process.env.ALLOWED_IPS.split(",").map((ip) => ip.trim())
|
||||
|
||||
Reference in New Issue
Block a user