Skip to content

Commit 8adeb2b

Browse files
Refactor magazine link visibility based on user authentication and improve origin handling for magazine iframe
1 parent e04731e commit 8adeb2b

File tree

3 files changed

+36
-21
lines changed

3 files changed

+36
-21
lines changed

src/components/Header.tsx

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -113,22 +113,25 @@ const HeaderLinks: React.FC<{ library: LibraryData }> = ({ library }) => {
113113
{libraryWebsite.title ?? `${libraryName} Home`}
114114
</AnchorButton>
115115
)}
116-
<NavButton
117-
variant="ghost"
118-
color="ui.black"
119-
href="/magazines"
120-
sx={{ mr: 1 }}
121-
>
122-
Magazines (logged in users)
123-
</NavButton>
124-
<NavButton
125-
variant="ghost"
126-
color="ui.black"
127-
href="/magazines-preview"
128-
sx={{ mr: 1 }}
129-
>
130-
Magazines (anonymous users)
131-
</NavButton>
116+
{isAuthenticated ? (
117+
<NavButton
118+
variant="ghost"
119+
color="ui.black"
120+
href="/magazines"
121+
sx={{ mr: 1 }}
122+
>
123+
Magazines
124+
</NavButton>
125+
) : (
126+
<NavButton
127+
variant="ghost"
128+
color="ui.black"
129+
href="/magazines-preview"
130+
sx={{ mr: 1 }}
131+
>
132+
Magazines
133+
</NavButton>
134+
)}
132135
<NavButton
133136
variant="ghost"
134137
color="ui.black"

src/config/magazines.ts

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,21 @@ export function getMagazineApiUrl(): string {
4444
* Get the allowed origin for magazine iframe communication
4545
*/
4646
export function getMagazineAllowedOrigin(): string {
47-
const baseUrl = getMagazineReaderUrl();
48-
return process.env.NEXT_PUBLIC_MAGAZINES_ORIGIN || new URL(baseUrl).origin;
47+
const overrideOrigin = process.env.NEXT_PUBLIC_MAGAZINES_ORIGIN;
48+
49+
if (overrideOrigin) {
50+
try {
51+
return new URL(overrideOrigin).origin;
52+
} catch {
53+
// Ignore invalid override and fall back to reader base URL origin.
54+
}
55+
}
56+
57+
try {
58+
return new URL(getMagazineReaderUrl()).origin;
59+
} catch {
60+
return MAGAZINE_READER_URLS.production;
61+
}
4962
}
5063

5164
/**

src/pages/[library]/magazines/index.tsx

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import * as React from "react";
77
import { NextPage, GetStaticProps, GetStaticPaths } from "next";
88
import LayoutPage from "components/LayoutPage";
99
import withAppProps, { AppProps } from "dataflow/withAppProps";
10-
// import useUser from "components/context/UserContext";
10+
import useUser from "components/context/UserContext";
1111
import useLogin from "auth/useLogin";
1212
import useLibraryContext from "components/context/LibraryContext";
1313
import {
@@ -20,8 +20,7 @@ import BreadcrumbBar from "components/BreadcrumbBar";
2020

2121
const MagazinesFixedContent: React.FC = () => {
2222
const iframeRef = React.useRef<HTMLIFrameElement | null>(null);
23-
const token = "MOCK_TOKEN";
24-
// const { token } = useUser();
23+
const { token } = useUser();
2524
const { initLogin } = useLogin();
2625
const { slug } = useLibraryContext();
2726

0 commit comments

Comments
 (0)