-
-
Notifications
You must be signed in to change notification settings - Fork 200
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: use Next.js app router + upgrade to next-auth@5 (beta) (#366)
- Loading branch information
1 parent
46a2fa8
commit d24b123
Showing
67 changed files
with
1,059 additions
and
970 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { GET, POST } from "../../../auth"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import { type Metadata } from "next"; | ||
import { redirect } from "next/navigation"; | ||
|
||
import { List, type Props as ListProps } from "../../components/bookmark/List"; | ||
import { type Bookmark } from "../../types/Bookmark"; | ||
import { type PagedCollection } from "../../types/collection"; | ||
import { type FetchResponse, fetchApi } from "../../utils/dataAccess"; | ||
import { type Session, auth } from "../auth"; | ||
|
||
interface Query extends URLSearchParams { | ||
page?: number|string|null; | ||
} | ||
|
||
export const metadata: Metadata = { | ||
title: 'Bookmarks', | ||
} | ||
async function getServerSideProps({ page = 1 }: Query, session: Session): Promise<ListProps> { | ||
try { | ||
const response: FetchResponse<PagedCollection<Bookmark>> | undefined = await fetchApi(`/bookmarks?page=${Number(page)}`, { | ||
next: { revalidate: 3600 }, | ||
}, session); | ||
if (!response?.data) { | ||
throw new Error('Unable to retrieve data from /bookmarks.'); | ||
} | ||
|
||
return { data: response.data, hubURL: response.hubURL, page: Number(page) }; | ||
} catch (error) { | ||
console.error(error); | ||
} | ||
|
||
return { data: null, hubURL: null, page: Number(page) }; | ||
} | ||
|
||
export default async function Page({ searchParams }: { searchParams: Query }) { | ||
// @ts-ignore | ||
const session: Session|null = await auth(); | ||
if (!session || session?.error === "RefreshAccessTokenError") { | ||
// todo find a way to redirect directly to keycloak from here | ||
// Can't use next-auth/middleware because of https://github.com/nextauthjs/next-auth/discussions/7488 | ||
redirect("/api/auth/signin?callbackUrl=/bookmarks"); | ||
} | ||
|
||
const props = await getServerSideProps(searchParams, session); | ||
|
||
return <List {...props}/>; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import { type Metadata } from "next"; | ||
import { notFound } from "next/navigation"; | ||
|
||
import { Show, type Props as ShowProps } from "../../../../components/book/Show"; | ||
import { Book } from "../../../../types/Book"; | ||
import { type FetchResponse, fetchApi } from "../../../../utils/dataAccess"; | ||
import { type Session, auth } from "../../../auth"; | ||
|
||
interface Props { | ||
params: { id: string }; | ||
} | ||
|
||
export async function generateMetadata({ params }: Props): Promise<Metadata|undefined> { | ||
const id = params.id; | ||
// @ts-ignore | ||
const session: Session|null = await auth(); | ||
try { | ||
const response: FetchResponse<Book> | undefined = await fetchApi(`/books/${id}`, { | ||
next: { revalidate: 3600 }, | ||
}, session); | ||
if (!response?.data) { | ||
throw new Error(`Unable to retrieve data from /books/${id}.`); | ||
} | ||
const item = response.data; | ||
|
||
return { | ||
title: `${item["title"]}${!!item["author"] && ` - ${item["author"]}`}`, | ||
}; | ||
} catch (error) { | ||
console.error(error); | ||
} | ||
|
||
return undefined; | ||
} | ||
|
||
async function getServerSideProps(id: string, session: Session|null): Promise<ShowProps|undefined> { | ||
try { | ||
const response: FetchResponse<Book> | undefined = await fetchApi(`/books/${id}`, { | ||
headers: { | ||
Preload: "/books/*/reviews", | ||
}, | ||
next: { revalidate: 3600 }, | ||
}, session); | ||
if (!response?.data) { | ||
throw new Error(`Unable to retrieve data from /books/${id}.`); | ||
} | ||
|
||
return { data: response.data, hubURL: response.hubURL }; | ||
} catch (error) { | ||
console.error(error); | ||
} | ||
|
||
return undefined; | ||
} | ||
|
||
export default async function Page({ params }: Props) { | ||
// @ts-ignore | ||
const session: Session|null = await auth(); | ||
const props = await getServerSideProps(params.id, session); | ||
if (!props) { | ||
notFound(); | ||
} | ||
|
||
return <Show {...props}/>; | ||
} |
Oops, something went wrong.