Skip to content

Commit

Permalink
Merge pull request #20 from badgeteam/feature/jwt
Browse files Browse the repository at this point in the history
Send JWT token to server
  • Loading branch information
edwinm authored Jan 6, 2025
2 parents b156bbb + e4e8149 commit 5e7d435
Show file tree
Hide file tree
Showing 16 changed files with 153 additions and 38 deletions.
11 changes: 10 additions & 1 deletion orval.config.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,19 @@
import { defineConfig } from "orval";
import dotenv from "dotenv";

/**
* See https://orval.dev/
*/

dotenv.config();

const baseUrl =
process.env.BADGEHUB_API_BASEURL || "https://api-staging.badgehub.nl";

const swaggerUrl = `${baseUrl}/swagger.json`;

console.log("Reading swagger from", swaggerUrl);

export default defineConfig({
badgehub: {
output: {
Expand All @@ -22,7 +31,7 @@ export default defineConfig({
},

input: {
target: `${baseUrl}/swagger.json`,
target: swaggerUrl,
},
},
});
26 changes: 23 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
"eslint": "^8.57.1",
"eslint-config-next": "^14.2.15",
"jsonpath-plus": "^10.1.0",
"jose": "^5.9.4",
"orval": "^7.0.1",
"prettier": "3.3.3",
"typescript": "^5"
Expand Down
20 changes: 16 additions & 4 deletions src/app/actions.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,22 @@
"use server";

import { GetAppsParams } from "@/badgehub-api-client/generated/models";
import {
getDevices,
getCategories,
getApps,
getCategories,
getDevices,
} from "@/badgehub-api-client/generated/swagger/public/public";

export async function getAppData(searchParams: GetAppsParams) {
return Promise.all([getApps(searchParams), getCategories(), getDevices()]);
export async function getAppData(searchParams: GetAppsParams, token: string) {
const headers = new Headers({
Authorization: `Bearer ${token}`,
});
const options: RequestInit = {
headers,
};
return Promise.all([
getApps(searchParams, options),
getCategories(options),
getDevices(options),
]);
}
15 changes: 12 additions & 3 deletions src/app/apps/layout.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,20 @@
import { ReactNode } from "react";
"use client";

import { SessionProvider } from "next-auth/react";
import styles from "./layout.module.css";
import { ReactNode } from "react";

type AppsListingLayoutProps = {
children: ReactNode;
};

export default function AppsListingLayout(props: { children: ReactNode }) {
export default function AppsListingLayout({
children,
}: AppsListingLayoutProps) {
return (
<main>
<h1 className={styles.title}>Apps</h1>
{props.children}
<SessionProvider>{children}</SessionProvider>
</main>
);
}
42 changes: 24 additions & 18 deletions src/app/apps/page.tsx
Original file line number Diff line number Diff line change
@@ -1,39 +1,45 @@
"use client";

import { AppList } from "@/components/AppList";
import { SessionProvider, useSession } from "next-auth/react";
import { LoginButton } from "@/components/LoginButton";
import { useEffect, useState } from "react";
import {
getAppsResponse,
getCategoriesResponse,
getDevicesResponse,
} from "@/badgehub-api-client/generated/swagger/public/public";
import { getAppData } from "../actions";

export interface SearchParams {
category: string;
device: string;
}

export default async function Listing({
export default function Listing({
searchParams,
}: {
searchParams: Partial<SearchParams>;
}) {
let data;
try {
// TODO add caching
data = await getAppData(searchParams);
} catch (e) {
if (!(e instanceof Error)) {
return <p>Caught object that wasn&amp;t an error.</p>;
const { data: session } = useSession();
const [data, setData] = useState<
[getAppsResponse, getCategoriesResponse, getDevicesResponse] | null
>(null);

useEffect(() => {
async function getData() {
const token = (session as any)?.accessToken;
const data = await getAppData(searchParams, token);
setData(data);
}
return (
<>
<p>Error while rendering</p>
<code>
<pre>{JSON.stringify(e.message)}</pre>
</code>
</>
);
}

getData();
}, [searchParams, session]);

return (
<>
<LoginButton />
<AppList data={data} />
{data ? <AppList data={data} /> : <p>Loading new data...</p>}
</>
);
}
14 changes: 14 additions & 0 deletions src/badgehub-api-client/generated/models/app.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/**
* Generated by orval v7.1.1 🍺
* Do not edit manually.
* badgehub-api
* Node project for the BadgeHub API
* OpenAPI spec version: 3
*/

export interface App {
category_slug: string;
name: string;
slug: string;
user_name: string;
}
16 changes: 16 additions & 0 deletions src/badgehub-api-client/generated/models/appDetails.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/**
* Generated by orval v7.1.1 🍺
* Do not edit manually.
* badgehub-api
* Node project for the BadgeHub API
* OpenAPI spec version: 3
*/

export interface AppDetails {
category_slug: string;
description: string;
devices: string[];
name: string;
slug: string;
user_name: string;
}
12 changes: 12 additions & 0 deletions src/badgehub-api-client/generated/models/device.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/**
* Generated by orval v7.1.1 🍺
* Do not edit manually.
* badgehub-api
* Node project for the BadgeHub API
* OpenAPI spec version: 3
*/

export interface Device {
name: string;
slug: string;
}
11 changes: 11 additions & 0 deletions src/badgehub-api-client/generated/models/getAppDetails404.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/**
* Generated by orval v7.1.1 🍺
* Do not edit manually.
* badgehub-api
* Node project for the BadgeHub API
* OpenAPI spec version: 3
*/

export type GetAppDetails404 = {
reason: string;
};
4 changes: 4 additions & 0 deletions src/badgehub-api-client/generated/models/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,20 @@
* OpenAPI spec version: 3
*/

export * from './app';
export * from './appCategoryName';
export * from './appDetails';
export * from './appMetadataJSON';
export * from './appMetadataJSONFileMappingsItem';
export * from './badge';
export * from './category';
export * from './dbInsertAppMetadataJSONPartial';
export * from './dbInsertAppMetadataJSONPartialFileMappingsItem';
export * from './dependency';
export * from './device';
export * from './fileMetadata';
export * from './getApp404';
export * from './getAppDetails404';
export * from './getAppsParams';
export * from './pickDBInsertProjectExcludeKeyofDBInsertProjectSlug';
export * from './pickDBInsertUserExcludeKeyofDBInsertUserId';
Expand Down
3 changes: 1 addition & 2 deletions src/badgehub-api-client/generated/swagger/private/private.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import type {
ProjectSlug,
Uint8Array,
UserProps,
Version,
WriteFileBody
} from '../../models'
import { fetchWithBaseUrl } from '../../../../fetch-from-api';
Expand Down Expand Up @@ -243,7 +242,7 @@ export const changeAppMetadata = async (slug: string,
* Upload a file to the latest draft version of the project.
*/
export type writeZipResponse = {
data: Version;
data: void;
status: number;
}

Expand Down
1 change: 1 addition & 0 deletions src/components/Account/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export function Account() {
return (
<>
<h1>Account</h1>
<p>JWT: {(session as any)?.accessToken}</p>
{html}
</>
);
Expand Down
4 changes: 2 additions & 2 deletions src/components/Filter/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
import styles from "./Filter.module.css";
import { useRef } from "react";
import { useSearchParams, useRouter } from "next/navigation";
import { Category, Badge } from "@/badgehub-api-client/generated/models";
import { Category, Device } from "@/badgehub-api-client/generated/models";

type FilterProps = {
categories: Category[];
devices: Badge[];
devices: Device[];
};

export function Filter({ categories, devices }: FilterProps) {
Expand Down
2 changes: 0 additions & 2 deletions src/components/MainNav/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ import clsx from "clsx";
export function MainNav() {
const pathname = usePathname();

console.log("pathname", pathname);

return (
<nav className={styles.mainNav}>
<Link
Expand Down
9 changes: 6 additions & 3 deletions src/fetch-from-api.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
"use server";

const getBody = <T>(c: Response | Request): Promise<T> => {
const contentType = c.headers.get("content-type");

Expand All @@ -19,8 +21,9 @@ export const fetchWithBaseUrl = async <T>(
options: RequestInit,
): Promise<T> => {
const requestUrl = getUrl(url);
const res = await fetch(requestUrl, options);
const data = await getBody(res);
const request = new Request(requestUrl, options);
const response = await fetch(request);
const data = await getBody(response);

return { status: res.status, data } as T;
return { status: response.status, data } as T;
};

0 comments on commit 5e7d435

Please sign in to comment.