Skip to content

Commit 5e7d435

Browse files
authored
Merge pull request #20 from badgeteam/feature/jwt
Send JWT token to server
2 parents b156bbb + e4e8149 commit 5e7d435

File tree

16 files changed

+153
-38
lines changed

16 files changed

+153
-38
lines changed

orval.config.ts

+10-1
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,19 @@
11
import { defineConfig } from "orval";
22
import dotenv from "dotenv";
33

4+
/**
5+
* See https://orval.dev/
6+
*/
7+
48
dotenv.config();
59

610
const baseUrl =
711
process.env.BADGEHUB_API_BASEURL || "https://api-staging.badgehub.nl";
12+
13+
const swaggerUrl = `${baseUrl}/swagger.json`;
14+
15+
console.log("Reading swagger from", swaggerUrl);
16+
817
export default defineConfig({
918
badgehub: {
1019
output: {
@@ -22,7 +31,7 @@ export default defineConfig({
2231
},
2332

2433
input: {
25-
target: `${baseUrl}/swagger.json`,
34+
target: swaggerUrl,
2635
},
2736
},
2837
});

package-lock.json

+23-3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
"eslint": "^8.57.1",
2828
"eslint-config-next": "^14.2.15",
2929
"jsonpath-plus": "^10.1.0",
30+
"jose": "^5.9.4",
3031
"orval": "^7.0.1",
3132
"prettier": "3.3.3",
3233
"typescript": "^5"

src/app/actions.ts

+16-4
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,22 @@
1+
"use server";
2+
13
import { GetAppsParams } from "@/badgehub-api-client/generated/models";
24
import {
3-
getDevices,
4-
getCategories,
55
getApps,
6+
getCategories,
7+
getDevices,
68
} from "@/badgehub-api-client/generated/swagger/public/public";
79

8-
export async function getAppData(searchParams: GetAppsParams) {
9-
return Promise.all([getApps(searchParams), getCategories(), getDevices()]);
10+
export async function getAppData(searchParams: GetAppsParams, token: string) {
11+
const headers = new Headers({
12+
Authorization: `Bearer ${token}`,
13+
});
14+
const options: RequestInit = {
15+
headers,
16+
};
17+
return Promise.all([
18+
getApps(searchParams, options),
19+
getCategories(options),
20+
getDevices(options),
21+
]);
1022
}

src/app/apps/layout.tsx

+12-3
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,20 @@
1-
import { ReactNode } from "react";
1+
"use client";
2+
3+
import { SessionProvider } from "next-auth/react";
24
import styles from "./layout.module.css";
5+
import { ReactNode } from "react";
6+
7+
type AppsListingLayoutProps = {
8+
children: ReactNode;
9+
};
310

4-
export default function AppsListingLayout(props: { children: ReactNode }) {
11+
export default function AppsListingLayout({
12+
children,
13+
}: AppsListingLayoutProps) {
514
return (
615
<main>
716
<h1 className={styles.title}>Apps</h1>
8-
{props.children}
17+
<SessionProvider>{children}</SessionProvider>
918
</main>
1019
);
1120
}

src/app/apps/page.tsx

+24-18
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,45 @@
1+
"use client";
2+
13
import { AppList } from "@/components/AppList";
4+
import { SessionProvider, useSession } from "next-auth/react";
25
import { LoginButton } from "@/components/LoginButton";
6+
import { useEffect, useState } from "react";
7+
import {
8+
getAppsResponse,
9+
getCategoriesResponse,
10+
getDevicesResponse,
11+
} from "@/badgehub-api-client/generated/swagger/public/public";
312
import { getAppData } from "../actions";
413

514
export interface SearchParams {
615
category: string;
716
device: string;
817
}
918

10-
export default async function Listing({
19+
export default function Listing({
1120
searchParams,
1221
}: {
1322
searchParams: Partial<SearchParams>;
1423
}) {
15-
let data;
16-
try {
17-
// TODO add caching
18-
data = await getAppData(searchParams);
19-
} catch (e) {
20-
if (!(e instanceof Error)) {
21-
return <p>Caught object that wasn&amp;t an error.</p>;
24+
const { data: session } = useSession();
25+
const [data, setData] = useState<
26+
[getAppsResponse, getCategoriesResponse, getDevicesResponse] | null
27+
>(null);
28+
29+
useEffect(() => {
30+
async function getData() {
31+
const token = (session as any)?.accessToken;
32+
const data = await getAppData(searchParams, token);
33+
setData(data);
2234
}
23-
return (
24-
<>
25-
<p>Error while rendering</p>
26-
<code>
27-
<pre>{JSON.stringify(e.message)}</pre>
28-
</code>
29-
</>
30-
);
31-
}
35+
36+
getData();
37+
}, [searchParams, session]);
3238

3339
return (
3440
<>
3541
<LoginButton />
36-
<AppList data={data} />
42+
{data ? <AppList data={data} /> : <p>Loading new data...</p>}
3743
</>
3844
);
3945
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/**
2+
* Generated by orval v7.1.1 🍺
3+
* Do not edit manually.
4+
* badgehub-api
5+
* Node project for the BadgeHub API
6+
* OpenAPI spec version: 3
7+
*/
8+
9+
export interface App {
10+
category_slug: string;
11+
name: string;
12+
slug: string;
13+
user_name: string;
14+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/**
2+
* Generated by orval v7.1.1 🍺
3+
* Do not edit manually.
4+
* badgehub-api
5+
* Node project for the BadgeHub API
6+
* OpenAPI spec version: 3
7+
*/
8+
9+
export interface AppDetails {
10+
category_slug: string;
11+
description: string;
12+
devices: string[];
13+
name: string;
14+
slug: string;
15+
user_name: string;
16+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
/**
2+
* Generated by orval v7.1.1 🍺
3+
* Do not edit manually.
4+
* badgehub-api
5+
* Node project for the BadgeHub API
6+
* OpenAPI spec version: 3
7+
*/
8+
9+
export interface Device {
10+
name: string;
11+
slug: string;
12+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
/**
2+
* Generated by orval v7.1.1 🍺
3+
* Do not edit manually.
4+
* badgehub-api
5+
* Node project for the BadgeHub API
6+
* OpenAPI spec version: 3
7+
*/
8+
9+
export type GetAppDetails404 = {
10+
reason: string;
11+
};

src/badgehub-api-client/generated/models/index.ts

+4
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,20 @@
66
* OpenAPI spec version: 3
77
*/
88

9+
export * from './app';
910
export * from './appCategoryName';
11+
export * from './appDetails';
1012
export * from './appMetadataJSON';
1113
export * from './appMetadataJSONFileMappingsItem';
1214
export * from './badge';
1315
export * from './category';
1416
export * from './dbInsertAppMetadataJSONPartial';
1517
export * from './dbInsertAppMetadataJSONPartialFileMappingsItem';
1618
export * from './dependency';
19+
export * from './device';
1720
export * from './fileMetadata';
1821
export * from './getApp404';
22+
export * from './getAppDetails404';
1923
export * from './getAppsParams';
2024
export * from './pickDBInsertProjectExcludeKeyofDBInsertProjectSlug';
2125
export * from './pickDBInsertUserExcludeKeyofDBInsertUserId';

src/badgehub-api-client/generated/swagger/private/private.ts

+1-2
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import type {
1212
ProjectSlug,
1313
Uint8Array,
1414
UserProps,
15-
Version,
1615
WriteFileBody
1716
} from '../../models'
1817
import { fetchWithBaseUrl } from '../../../../fetch-from-api';
@@ -243,7 +242,7 @@ export const changeAppMetadata = async (slug: string,
243242
* Upload a file to the latest draft version of the project.
244243
*/
245244
export type writeZipResponse = {
246-
data: Version;
245+
data: void;
247246
status: number;
248247
}
249248

src/components/Account/index.tsx

+1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ export function Account() {
2020
return (
2121
<>
2222
<h1>Account</h1>
23+
<p>JWT: {(session as any)?.accessToken}</p>
2324
{html}
2425
</>
2526
);

src/components/Filter/index.tsx

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@
33
import styles from "./Filter.module.css";
44
import { useRef } from "react";
55
import { useSearchParams, useRouter } from "next/navigation";
6-
import { Category, Badge } from "@/badgehub-api-client/generated/models";
6+
import { Category, Device } from "@/badgehub-api-client/generated/models";
77

88
type FilterProps = {
99
categories: Category[];
10-
devices: Badge[];
10+
devices: Device[];
1111
};
1212

1313
export function Filter({ categories, devices }: FilterProps) {

src/components/MainNav/index.tsx

-2
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@ import clsx from "clsx";
77
export function MainNav() {
88
const pathname = usePathname();
99

10-
console.log("pathname", pathname);
11-
1210
return (
1311
<nav className={styles.mainNav}>
1412
<Link

src/fetch-from-api.ts

+6-3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
"use server";
2+
13
const getBody = <T>(c: Response | Request): Promise<T> => {
24
const contentType = c.headers.get("content-type");
35

@@ -19,8 +21,9 @@ export const fetchWithBaseUrl = async <T>(
1921
options: RequestInit,
2022
): Promise<T> => {
2123
const requestUrl = getUrl(url);
22-
const res = await fetch(requestUrl, options);
23-
const data = await getBody(res);
24+
const request = new Request(requestUrl, options);
25+
const response = await fetch(request);
26+
const data = await getBody(response);
2427

25-
return { status: res.status, data } as T;
28+
return { status: response.status, data } as T;
2629
};

0 commit comments

Comments
 (0)