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())
|
||||
|
||||
@@ -0,0 +1,155 @@
|
||||
import Head from "next/head";
|
||||
import { useRouter } from "next/router";
|
||||
import { useState } from "react";
|
||||
import CookieBanner from "../../components/cookieBanner";
|
||||
import Footer from "../../components/footer";
|
||||
import Header from "../../components/header";
|
||||
import {
|
||||
readAdminSessionCookie,
|
||||
validateAdminSession
|
||||
} from "../../lib/admin/adminAuth";
|
||||
|
||||
const AdminLoginPage = () => {
|
||||
const router = useRouter();
|
||||
const [passphrase, setPassphrase] = useState("");
|
||||
const [error, setError] = useState("");
|
||||
const [submitting, setSubmitting] = useState(false);
|
||||
|
||||
const handleSubmit = async (event) => {
|
||||
event.preventDefault();
|
||||
setError("");
|
||||
setSubmitting(true);
|
||||
|
||||
try {
|
||||
const response = await fetch("/api/admin/auth/login", {
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
credentials: "same-origin",
|
||||
body: JSON.stringify({ passphrase })
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
setError("Sign in failed. Check the details and try again.");
|
||||
return;
|
||||
}
|
||||
|
||||
await router.replace("/admin");
|
||||
} catch (_error) {
|
||||
setError("Sign in failed. Check the details and try again.");
|
||||
} finally {
|
||||
setSubmitting(false);
|
||||
}
|
||||
};
|
||||
|
||||
return (
|
||||
<div>
|
||||
<Head>
|
||||
<title>Administrative sign in</title>
|
||||
</Head>
|
||||
<div id="page_wrapper">
|
||||
<CookieBanner />
|
||||
<Header />
|
||||
<main
|
||||
className="govuk-width-container govuk-main-wrapper govuk-!-padding-bottom-9"
|
||||
id="main-content"
|
||||
>
|
||||
<div className="govuk-grid-row">
|
||||
<div className="govuk-grid-column-two-thirds">
|
||||
<h1 className="govuk-heading-l">
|
||||
Administrative sign in
|
||||
</h1>
|
||||
|
||||
{error && (
|
||||
<div
|
||||
className="govuk-error-summary"
|
||||
role="alert"
|
||||
aria-labelledby="admin-login-error-title"
|
||||
tabIndex={-1}
|
||||
>
|
||||
<h2
|
||||
className="govuk-error-summary__title"
|
||||
id="admin-login-error-title"
|
||||
>
|
||||
There is a problem
|
||||
</h2>
|
||||
<div className="govuk-error-summary__body">
|
||||
<p>{error}</p>
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
|
||||
<form onSubmit={handleSubmit} noValidate>
|
||||
<div
|
||||
className={`govuk-form-group${
|
||||
error ? " govuk-form-group--error" : ""
|
||||
}`}
|
||||
>
|
||||
<label
|
||||
className="govuk-label govuk-label--m"
|
||||
htmlFor="admin-passphrase"
|
||||
>
|
||||
Password
|
||||
</label>
|
||||
{error && (
|
||||
<p
|
||||
id="admin-passphrase-error"
|
||||
className="govuk-error-message"
|
||||
>
|
||||
<span className="govuk-visually-hidden">
|
||||
Error:
|
||||
</span>{" "}
|
||||
{error}
|
||||
</p>
|
||||
)}
|
||||
<input
|
||||
className={`govuk-input${
|
||||
error ? " govuk-input--error" : ""
|
||||
}`}
|
||||
id="admin-passphrase"
|
||||
name="passphrase"
|
||||
type="password"
|
||||
autoComplete="current-password"
|
||||
value={passphrase}
|
||||
onChange={(event) =>
|
||||
setPassphrase(event.target.value)
|
||||
}
|
||||
aria-describedby={
|
||||
error
|
||||
? "admin-passphrase-error"
|
||||
: undefined
|
||||
}
|
||||
aria-invalid={error ? "true" : undefined}
|
||||
disabled={submitting}
|
||||
required
|
||||
/>
|
||||
</div>
|
||||
<button
|
||||
className="govuk-button"
|
||||
type="submit"
|
||||
disabled={submitting}
|
||||
>
|
||||
{submitting ? "Signing in..." : "Sign in"}
|
||||
</button>
|
||||
</form>
|
||||
</div>
|
||||
</div>
|
||||
</main>
|
||||
<Footer />
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
};
|
||||
|
||||
export const getServerSideProps = async ({ req }) => {
|
||||
const adminSession = readAdminSessionCookie(req);
|
||||
|
||||
if (validateAdminSession(adminSession)) {
|
||||
return {
|
||||
redirect: { destination: "/admin", permanent: false }
|
||||
};
|
||||
}
|
||||
|
||||
return { props: {} };
|
||||
};
|
||||
|
||||
export default AdminLoginPage;
|
||||
Reference in New Issue
Block a user