www: add feature to enforce login on transcripts record/past/browse

This commit is contained in:
2023-10-18 17:18:16 +02:00
committed by Mathieu Virbel
parent 168e4d6fa4
commit ef2f579fc1
3 changed files with 77 additions and 61 deletions

View File

@@ -9,3 +9,7 @@ export function featPrivacy() {
export function featBrowse() { export function featBrowse() {
return process.env.NEXT_PUBLIC_FEAT_BROWSE === "1"; return process.env.NEXT_PUBLIC_FEAT_BROWSE === "1";
} }
export function featRequireLogin() {
return process.env.NEXT_PUBLIC_FEAT_LOGIN_REQUIRED === "1";
}

View File

@@ -4,18 +4,19 @@ import useAudioDevice from "../useAudioDevice";
import "react-select-search/style.css"; import "react-select-search/style.css";
import "../../styles/button.css"; import "../../styles/button.css";
import "../../styles/form.scss"; import "../../styles/form.scss";
import getApi from "../../lib/getApi";
import About from "../../(aboutAndPrivacy)/about"; import About from "../../(aboutAndPrivacy)/about";
import Privacy from "../../(aboutAndPrivacy)/privacy"; import Privacy from "../../(aboutAndPrivacy)/privacy";
import { useRouter } from "next/navigation"; import { useRouter } from "next/navigation";
import useCreateTranscript from "../createTranscript"; import useCreateTranscript from "../createTranscript";
import SelectSearch from "react-select-search"; import SelectSearch from "react-select-search";
import { supportedLatinLanguages } from "../../supportedLanguages"; import { supportedLatinLanguages } from "../../supportedLanguages";
import { featRequireLogin } from "../../lib/utils";
import { useFiefIsAuthenticated } from "@fief/fief/nextjs/react";
const TranscriptCreate = () => { const TranscriptCreate = () => {
// const transcript = useTranscript(stream, api);
const router = useRouter(); const router = useRouter();
const api = getApi(); const isAuthenticated = useFiefIsAuthenticated();
const requireLogin = featRequireLogin();
const [name, setName] = useState<string>(); const [name, setName] = useState<string>();
const nameChange = (event: React.ChangeEvent<HTMLInputElement>) => { const nameChange = (event: React.ChangeEvent<HTMLInputElement>) => {
@@ -77,57 +78,66 @@ const TranscriptCreate = () => {
</div> </div>
</section> </section>
<section className="flex flex-col justify-center items-center w-full h-full"> <section className="flex flex-col justify-center items-center w-full h-full">
<div className="rounded-xl md:bg-blue-200 md:w-96 p-4 lg:p-6 flex flex-col mb-4 md:mb-10"> {requireLogin && !isAuthenticated ? (
<h2 className="text-2xl font-bold mt-2 mb-2"> Try Reflector</h2>
<label className="mb-3">
<p>Recording name</p>
<div className="select-search-container">
<input
className="select-search-input"
type="text"
onChange={nameChange}
placeholder="Optional"
/>
</div>
</label>
<label className="mb-3">
<p>Do you want to enable live translation?</p>
<SelectSearch
search
options={supportedLatinLanguages}
value={targetLanguage}
onChange={onLanguageChange}
placeholder="Choose your language"
/>
</label>
{loading ? (
<p className="">Checking permissions...</p>
) : permissionOk ? (
<p className=""> Microphone permission granted </p>
) : permissionDenied ? (
<p className="">
Permission to use your microphone was denied, please change the
permission setting in your browser and refresh this page.
</p>
) : (
<button
className="mt-4 bg-blue-400 hover:bg-blue-500 focus-visible:bg-blue-500 text-white font-bold py-2 px-4 rounded"
onClick={requestPermission}
disabled={permissionDenied}
>
Request Microphone Permission
</button>
)}
<button <button
className="mt-4 bg-blue-400 hover:bg-blue-500 focus-visible:bg-blue-500 text-white font-bold py-2 px-4 rounded" className="mt-4 bg-blue-400 hover:bg-blue-500 focus-visible:bg-blue-500 text-white font-bold py-2 px-4 rounded"
onClick={send} onClick={() => router.push("/login")}
disabled={!permissionOk || loadingSend}
> >
{loadingSend ? "Loading..." : "Confirm"} Log in
</button> </button>
</div> ) : (
<div className="rounded-xl md:bg-blue-200 md:w-96 p-4 lg:p-6 flex flex-col mb-4 md:mb-10">
<h2 className="text-2xl font-bold mt-2 mb-2">Try Reflector</h2>
<label className="mb-3">
<p>Recording name</p>
<div className="select-search-container">
<input
className="select-search-input"
type="text"
onChange={nameChange}
placeholder="Optional"
/>
</div>
</label>
<label className="mb-3">
<p>Do you want to enable live translation?</p>
<SelectSearch
search
options={supportedLatinLanguages}
value={targetLanguage}
onChange={onLanguageChange}
placeholder="Choose your language"
/>
</label>
{loading ? (
<p className="">Checking permissions...</p>
) : permissionOk ? (
<p className=""> Microphone permission granted </p>
) : permissionDenied ? (
<p className="">
Permission to use your microphone was denied, please change
the permission setting in your browser and refresh this page.
</p>
) : (
<button
className="mt-4 bg-blue-400 hover:bg-blue-500 focus-visible:bg-blue-500 text-white font-bold py-2 px-4 rounded"
onClick={requestPermission}
disabled={permissionDenied}
>
Request Microphone Permission
</button>
)}
<button
className="mt-4 bg-blue-400 hover:bg-blue-500 focus-visible:bg-blue-500 text-white font-bold py-2 px-4 rounded"
onClick={send}
disabled={!permissionOk || loadingSend}
>
{loadingSend ? "Loading..." : "Confirm"}
</button>
</div>
)}
</section> </section>
</div> </div>
</> </>

View File

@@ -2,19 +2,21 @@ import type { NextRequest } from "next/server";
import { fiefAuth } from "./app/lib/fief"; import { fiefAuth } from "./app/lib/fief";
const authMiddleware = fiefAuth.middleware([ let protectedPath: any = [];
{ if (process.env.NEXT_PUBLIC_FEAT_LOGIN_REQUIRED === "1") {
matcher: "/private", protectedPath = [
parameters: {}, {
}, matcher: "/transcripts/((?!new).*)",
{ parameters: {},
matcher: "/castles/:path*",
parameters: {
permissions: ["castles:read"],
}, },
}, {
]); matcher: "/browse",
parameters: {},
},
];
}
const authMiddleware = fiefAuth.middleware(protectedPath);
export async function middleware(request: NextRequest) { export async function middleware(request: NextRequest) {
return authMiddleware(request); return authMiddleware(request);
} }