Skip to content

Commit

Permalink
AuthenticationGuard
Browse files Browse the repository at this point in the history
  • Loading branch information
FoundTheWOUT committed Mar 7, 2023
1 parent ff4f0fe commit 862f48d
Show file tree
Hide file tree
Showing 9 changed files with 91 additions and 49 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,5 @@ next-env.d.ts

todo
dist
uploads
uploads
dump-files
17 changes: 17 additions & 0 deletions packages/web/src/app/components/AuthenticationGuard.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
"use client";

import { useAuthentication } from "@/hooks";
import { PropsWithChildren, Suspense } from "react";
import TokenInput from "./Token";

function AuthenticationGuard({ children }: PropsWithChildren<{}>) {
const { data } = useAuthentication();

if (!data) return <div>loading</div>;
if (data.role !== "master") {
return <TokenInput />;
}
return <Suspense>{children}</Suspense>;
}

export default AuthenticationGuard;
20 changes: 0 additions & 20 deletions packages/web/src/app/list-music/page.tsx

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,8 @@ import { nanoid } from "nanoid";
import { atom, useAtom } from "jotai";
import { TrashIcon } from "@heroicons/react/24/outline";
import { Music } from "@/music";
import AuthenticationGuard from "@/app/components/authenticationGuard";
import { tokenAtom } from "@/state";
import useSWR from "swr";
import { useAuthentication } from "@/hooks";

type MusicInput = {
nanoId: string;
Expand All @@ -29,9 +28,7 @@ const musicInputAtom = atom<MusicInput>({
function AddMusic() {
const [musics, setMusics] = useState<MusicInput[]>([]);
const [musicInput, setMusicInput] = useAtom(musicInputAtom);
const [token, setToken] = useAtom(tokenAtom);
const [tokenInput, setTokenInput] = useState("");
const { data, mutate } = useAuthentication();
const [token] = useAtom(tokenAtom);

const handleSubmit = async () => {
if (!musics.length) return;
Expand Down Expand Up @@ -88,27 +85,6 @@ function AddMusic() {
});
};

if (!data) return <div>loading</div>;

if (data.role !== "master") {
return (
<>
<Input
value={tokenInput}
onChange={(e) => setTokenInput(e.currentTarget.value)}
/>
<Button
onClick={() => {
setToken(tokenInput);
mutate(tokenInput);
}}
>
Token
</Button>
</>
);
}

return (
<div>
{musics.map((music, idx) => (
Expand Down Expand Up @@ -200,4 +176,12 @@ function AddMusic() {
);
}

export default AddMusic;
function AddMusicPage() {
return (
<AuthenticationGuard>
<AddMusic />
</AuthenticationGuard>
);
}

export default AddMusicPage;
38 changes: 38 additions & 0 deletions packages/web/src/app/music/list/list.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { Music } from "@/music";

async function MusicList() {
const data: { musics: Music[] } = await fetch(
`${process.env.NEXT_PUBLIC_API_GATE}/music/list`,
{ cache: "no-cache" }
).then((res) => res.json());

return (
<table className="table-auto">
<thead>
<tr className="border">
<th>ID</th>
<th>Name</th>
<th>censored</th>
</tr>
</thead>
<tbody>
{data.musics.map((music) => (
<tr key={music.id} className="border">
<td>{music.id}</td>
<td>{music.name}</td>
<td>
<input
className="checked:bg-red-500"
type="checkbox"
defaultChecked={music.censored}
readOnly
/>
</td>
</tr>
))}
</tbody>
</table>
);
}

export default MusicList;
13 changes: 13 additions & 0 deletions packages/web/src/app/music/list/page.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import List from "./list";
import AuthenticationGuard from "@/app/components/authenticationGuard";

async function MusicList() {
return (
<AuthenticationGuard>
{/* @ts-ignore */}
<List />
</AuthenticationGuard>
);
}

export default MusicList;
2 changes: 1 addition & 1 deletion packages/web/src/music.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export type { Music } from "@15s-music/server/entry";
export type { Music } from "@15s-music/server/src/entry";

let audio: HTMLAudioElement | null = null;
export const playMusic = (blob: Blob): Promise<HTMLAudioElement | null> => {
Expand Down
1 change: 1 addition & 0 deletions packages/web/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"extends": "../../tsconfig.json",
"compilerOptions": {
"target": "es5",
"lib": ["dom", "dom.iterable", "esnext"],
Expand Down
8 changes: 8 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"compilerOptions": {
"baseUrl": ".",
"paths": {
"@15s-music/server/*": ["packages/server/src/*"]
}
}
}

0 comments on commit 862f48d

Please sign in to comment.