From f14e6f5a7f9e137ff9d43cf87e25c3ecd32688d4 Mon Sep 17 00:00:00 2001
From: Sara
Date: Wed, 22 Nov 2023 13:20:11 +0100
Subject: [PATCH] fix auth
---
www/app/(auth)/fiefWrapper.tsx | 15 +++++++++++----
www/app/[domain]/layout.tsx | 5 ++++-
.../[domain]/transcripts/[transcriptId]/page.tsx | 11 +++--------
.../transcripts/[transcriptId]/record/page.tsx | 6 +++---
www/app/[domain]/transcripts/createTranscript.ts | 2 +-
www/app/[domain]/transcripts/finalSummary.tsx | 3 +--
www/app/[domain]/transcripts/shareLink.tsx | 3 +--
www/app/[domain]/transcripts/transcriptTitle.tsx | 3 +--
www/app/[domain]/transcripts/useMp3.ts | 2 +-
www/app/[domain]/transcripts/useTopics.ts | 4 ++--
www/app/[domain]/transcripts/useTranscript.ts | 3 +--
www/app/[domain]/transcripts/useTranscriptList.ts | 2 +-
www/app/[domain]/transcripts/useWaveform.ts | 4 ++--
www/app/[domain]/transcripts/useWebRTC.ts | 3 +--
www/app/lib/fief.ts | 4 ----
www/app/lib/getApi.ts | 8 +++++---
16 files changed, 38 insertions(+), 40 deletions(-)
diff --git a/www/app/(auth)/fiefWrapper.tsx b/www/app/(auth)/fiefWrapper.tsx
index 187fef7c..bb38f5ee 100644
--- a/www/app/(auth)/fiefWrapper.tsx
+++ b/www/app/(auth)/fiefWrapper.tsx
@@ -1,11 +1,18 @@
"use client";
import { FiefAuthProvider } from "@fief/fief/nextjs/react";
+import { createContext } from "react";
-export default function FiefWrapper({ children }) {
+export const CookieContext = createContext<{ hasAuthCookie: boolean }>({
+ hasAuthCookie: false,
+});
+
+export default function FiefWrapper({ children, hasAuthCookie }) {
return (
-
- {children}
-
+
+
+ {children}
+
+
);
}
diff --git a/www/app/[domain]/layout.tsx b/www/app/[domain]/layout.tsx
index 3e881ac3..73cc4841 100644
--- a/www/app/[domain]/layout.tsx
+++ b/www/app/[domain]/layout.tsx
@@ -12,6 +12,8 @@ import Privacy from "../(aboutAndPrivacy)/privacy";
import { DomainContextProvider } from "./domainContext";
import { getConfig } from "../lib/edgeConfig";
import { ErrorBoundary } from "@sentry/nextjs";
+import { cookies } from "next/dist/client/components/headers";
+import { SESSION_COOKIE_NAME } from "../lib/fief";
const poppins = Poppins({ subsets: ["latin"], weight: ["200", "400", "600"] });
@@ -71,11 +73,12 @@ type LayoutProps = {
export default async function RootLayout({ children, params }: LayoutProps) {
const config = await getConfig(params.domain);
const { requireLogin, privacy, browse } = config.features;
+ const hasAuthCookie = !!cookies().get(SESSION_COOKIE_NAME);
return (
-
+
"something went really wrong"
}>
diff --git a/www/app/[domain]/transcripts/[transcriptId]/page.tsx b/www/app/[domain]/transcripts/[transcriptId]/page.tsx
index 23634b95..472c573b 100644
--- a/www/app/[domain]/transcripts/[transcriptId]/page.tsx
+++ b/www/app/[domain]/transcripts/[transcriptId]/page.tsx
@@ -22,15 +22,13 @@ type TranscriptDetails = {
};
};
-const protectedPath = false;
-
export default function TranscriptDetails(details: TranscriptDetails) {
const transcriptId = details.params.transcriptId;
const router = useRouter();
- const transcript = useTranscript(protectedPath, transcriptId);
- const topics = useTopics(protectedPath, transcriptId);
- const waveform = useWaveform(protectedPath, transcriptId);
+ const transcript = useTranscript(transcriptId);
+ const topics = useTopics(transcriptId);
+ const waveform = useWaveform(transcriptId);
const useActiveTopic = useState(null);
const mp3 = useMp3(transcriptId);
@@ -71,7 +69,6 @@ export default function TranscriptDetails(details: TranscriptDetails) {
{transcript?.response?.title && (
@@ -101,7 +98,6 @@ export default function TranscriptDetails(details: TranscriptDetails) {
{transcript.response.longSummary ? (
{
}
}, []);
- const transcript = useTranscript(true, details.params.transcriptId);
- const webRTC = useWebRTC(stream, details.params.transcriptId, true);
+ const transcript = useTranscript(details.params.transcriptId);
+ const webRTC = useWebRTC(stream, details.params.transcriptId);
const webSockets = useWebSockets(details.params.transcriptId);
const { audioDevices, getAudioStream } = useAudioDevice();
diff --git a/www/app/[domain]/transcripts/createTranscript.ts b/www/app/[domain]/transcripts/createTranscript.ts
index 0d96b8db..9ad1abe0 100644
--- a/www/app/[domain]/transcripts/createTranscript.ts
+++ b/www/app/[domain]/transcripts/createTranscript.ts
@@ -19,7 +19,7 @@ const useCreateTranscript = (): CreateTranscript => {
const [loading, setLoading] = useState(false);
const [error, setErrorState] = useState(null);
const { setError } = useError();
- const api = getApi(true);
+ const api = getApi();
const create = (params: V1TranscriptsCreateRequest["createTranscript"]) => {
if (loading || !api) return;
diff --git a/www/app/[domain]/transcripts/finalSummary.tsx b/www/app/[domain]/transcripts/finalSummary.tsx
index e0d0f1c9..47c757bf 100644
--- a/www/app/[domain]/transcripts/finalSummary.tsx
+++ b/www/app/[domain]/transcripts/finalSummary.tsx
@@ -5,7 +5,6 @@ import "../../styles/markdown.css";
import getApi from "../../lib/getApi";
type FinalSummaryProps = {
- protectedPath: boolean;
summary: string;
fullTranscript: string;
transcriptId: string;
@@ -18,7 +17,7 @@ export default function FinalSummary(props: FinalSummaryProps) {
const [isEditMode, setIsEditMode] = useState(false);
const [preEditSummary, setPreEditSummary] = useState(props.summary);
const [editedSummary, setEditedSummary] = useState(props.summary);
- const api = getApi(props.protectedPath);
+ const api = getApi();
const updateSummary = async (newSummary: string, transcriptId: string) => {
if (!api) return;
diff --git a/www/app/[domain]/transcripts/shareLink.tsx b/www/app/[domain]/transcripts/shareLink.tsx
index 82ef52c9..e6449bd3 100644
--- a/www/app/[domain]/transcripts/shareLink.tsx
+++ b/www/app/[domain]/transcripts/shareLink.tsx
@@ -8,7 +8,6 @@ import "../../styles/button.css";
import "../../styles/form.scss";
type ShareLinkProps = {
- protectedPath: boolean;
transcriptId: string;
userId: string | null;
shareMode: string;
@@ -21,8 +20,8 @@ const ShareLink = (props: ShareLinkProps) => {
const requireLogin = featureEnabled("requireLogin");
const [isOwner, setIsOwner] = useState(false);
const [shareMode, setShareMode] = useState(props.shareMode);
- const api = getApi(props.protectedPath);
const userinfo = useFiefUserinfo();
+ const api = getApi();
useEffect(() => {
setCurrentUrl(window.location.href);
diff --git a/www/app/[domain]/transcripts/transcriptTitle.tsx b/www/app/[domain]/transcripts/transcriptTitle.tsx
index d2f901fa..afc29e51 100644
--- a/www/app/[domain]/transcripts/transcriptTitle.tsx
+++ b/www/app/[domain]/transcripts/transcriptTitle.tsx
@@ -2,7 +2,6 @@ import { useState } from "react";
import getApi from "../../lib/getApi";
type TranscriptTitle = {
- protectedPath: boolean;
title: string;
transcriptId: string;
};
@@ -11,7 +10,7 @@ const TranscriptTitle = (props: TranscriptTitle) => {
const [displayedTitle, setDisplayedTitle] = useState(props.title);
const [preEditTitle, setPreEditTitle] = useState(props.title);
const [isEditing, setIsEditing] = useState(false);
- const api = getApi(props.protectedPath);
+ const api = getApi();
const updateTitle = async (newTitle: string, transcriptId: string) => {
if (!api) return;
diff --git a/www/app/[domain]/transcripts/useMp3.ts b/www/app/[domain]/transcripts/useMp3.ts
index 58e0209d..363a4190 100644
--- a/www/app/[domain]/transcripts/useMp3.ts
+++ b/www/app/[domain]/transcripts/useMp3.ts
@@ -13,7 +13,7 @@ const useMp3 = (id: string, waiting?: boolean): Mp3Response => {
const [media, setMedia] = useState(null);
const [later, setLater] = useState(waiting);
const [loading, setLoading] = useState(false);
- const api = getApi(true);
+ const api = getApi();
const { api_url } = useContext(DomainContext);
const accessTokenInfo = useFiefAccessTokenInfo();
const [serviceWorker, setServiceWorker] =
diff --git a/www/app/[domain]/transcripts/useTopics.ts b/www/app/[domain]/transcripts/useTopics.ts
index 01053019..de4097b3 100644
--- a/www/app/[domain]/transcripts/useTopics.ts
+++ b/www/app/[domain]/transcripts/useTopics.ts
@@ -14,12 +14,12 @@ type TranscriptTopics = {
error: Error | null;
};
-const useTopics = (protectedPath, id: string): TranscriptTopics => {
+const useTopics = (id: string): TranscriptTopics => {
const [topics, setTopics] = useState(null);
const [loading, setLoading] = useState(false);
const [error, setErrorState] = useState(null);
const { setError } = useError();
- const api = getApi(protectedPath);
+ const api = getApi();
useEffect(() => {
if (!id || !api) return;
diff --git a/www/app/[domain]/transcripts/useTranscript.ts b/www/app/[domain]/transcripts/useTranscript.ts
index 987e57f3..91700d7a 100644
--- a/www/app/[domain]/transcripts/useTranscript.ts
+++ b/www/app/[domain]/transcripts/useTranscript.ts
@@ -24,14 +24,13 @@ type SuccessTranscript = {
};
const useTranscript = (
- protectedPath: boolean,
id: string | null,
): ErrorTranscript | LoadingTranscript | SuccessTranscript => {
const [response, setResponse] = useState(null);
const [loading, setLoading] = useState(true);
const [error, setErrorState] = useState(null);
const { setError } = useError();
- const api = getApi(protectedPath);
+ const api = getApi();
useEffect(() => {
if (!id || !api) return;
diff --git a/www/app/[domain]/transcripts/useTranscriptList.ts b/www/app/[domain]/transcripts/useTranscriptList.ts
index cc8f4701..7b5abb37 100644
--- a/www/app/[domain]/transcripts/useTranscriptList.ts
+++ b/www/app/[domain]/transcripts/useTranscriptList.ts
@@ -15,7 +15,7 @@ const useTranscriptList = (page: number): TranscriptList => {
const [loading, setLoading] = useState(true);
const [error, setErrorState] = useState(null);
const { setError } = useError();
- const api = getApi(true);
+ const api = getApi();
useEffect(() => {
if (!api) return;
diff --git a/www/app/[domain]/transcripts/useWaveform.ts b/www/app/[domain]/transcripts/useWaveform.ts
index d2bd0fd6..f80ad78c 100644
--- a/www/app/[domain]/transcripts/useWaveform.ts
+++ b/www/app/[domain]/transcripts/useWaveform.ts
@@ -11,12 +11,12 @@ type AudioWaveFormResponse = {
error: Error | null;
};
-const useWaveform = (protectedPath, id: string): AudioWaveFormResponse => {
+const useWaveform = (id: string): AudioWaveFormResponse => {
const [waveform, setWaveform] = useState(null);
const [loading, setLoading] = useState(true);
const [error, setErrorState] = useState(null);
const { setError } = useError();
- const api = getApi(protectedPath);
+ const api = getApi();
useEffect(() => {
if (!id || !api) return;
diff --git a/www/app/[domain]/transcripts/useWebRTC.ts b/www/app/[domain]/transcripts/useWebRTC.ts
index f4421e4d..edd3bef0 100644
--- a/www/app/[domain]/transcripts/useWebRTC.ts
+++ b/www/app/[domain]/transcripts/useWebRTC.ts
@@ -10,11 +10,10 @@ import getApi from "../../lib/getApi";
const useWebRTC = (
stream: MediaStream | null,
transcriptId: string | null,
- protectedPath,
): Peer => {
const [peer, setPeer] = useState(null);
const { setError } = useError();
- const api = getApi(protectedPath);
+ const api = getApi();
useEffect(() => {
if (!stream || !transcriptId) {
diff --git a/www/app/lib/fief.ts b/www/app/lib/fief.ts
index 176aa847..3af5c30f 100644
--- a/www/app/lib/fief.ts
+++ b/www/app/lib/fief.ts
@@ -66,10 +66,6 @@ export const getFiefAuthMiddleware = async (url) => {
matcher: "/transcripts",
parameters: {},
},
- {
- matcher: "/transcripts/((?!new))",
- parameters: {},
- },
{
matcher: "/browse",
parameters: {},
diff --git a/www/app/lib/getApi.ts b/www/app/lib/getApi.ts
index 7392cc90..e1ece2a9 100644
--- a/www/app/lib/getApi.ts
+++ b/www/app/lib/getApi.ts
@@ -4,17 +4,19 @@ import { DefaultApi } from "../api/apis/DefaultApi";
import { useFiefAccessTokenInfo } from "@fief/fief/nextjs/react";
import { useContext, useEffect, useState } from "react";
import { DomainContext, featureEnabled } from "../[domain]/domainContext";
+import { CookieContext } from "../(auth)/fiefWrapper";
-export default function getApi(protectedPath: boolean): DefaultApi | undefined {
+export default function getApi(): DefaultApi | undefined {
const accessTokenInfo = useFiefAccessTokenInfo();
const api_url = useContext(DomainContext).api_url;
const requireLogin = featureEnabled("requireLogin");
const [api, setApi] = useState();
+ const { hasAuthCookie } = useContext(CookieContext);
if (!api_url) throw new Error("no API URL");
useEffect(() => {
- if (protectedPath && requireLogin && !accessTokenInfo) {
+ if (hasAuthCookie && requireLogin && !accessTokenInfo) {
return;
}
@@ -25,7 +27,7 @@ export default function getApi(protectedPath: boolean): DefaultApi | undefined {
: undefined,
});
setApi(new DefaultApi(apiConfiguration));
- }, [!accessTokenInfo, protectedPath]);
+ }, [!accessTokenInfo, hasAuthCookie]);
return api;
}