Skip to content

Commit

Permalink
Require gh login
Browse files Browse the repository at this point in the history
  • Loading branch information
benthecarman committed Dec 19, 2024
1 parent dc6445e commit c80f2dd
Show file tree
Hide file tree
Showing 9 changed files with 108 additions and 13 deletions.
23 changes: 23 additions & 0 deletions src/api/client.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import {setToken, token} from "~/stores/auth";

const FAUCET_API_URL = import.meta.env.VITE_FAUCET_API;

export const api = {
get: async (url: string) => {
const response = await fetch(`${FAUCET_API_URL}/${url}`, {
headers: {
Authorization: `Bearer ${token()}`,
},
});

if (response.status === 401) {
// Handle unauthorized
setToken(null);
localStorage.removeItem("token");
window.location.href = "/";
return;
}

return response.json();
},
};
13 changes: 13 additions & 0 deletions src/components/AuthButton.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { Component } from "solid-js";
import { token, login, logout } from "~/stores/auth";

export const AuthButton: Component = () => {
return (
<button
onClick={() => token() ? logout() : login()}
class="px-4 py-2 bg-blue-500 text-white rounded"
>
{token() ? "Logout" : "Login with GitHub"}
</button>
);
};
2 changes: 2 additions & 0 deletions src/components/Faucet.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Match, Switch, createSignal } from "solid-js";
import { createRouteAction, useSearchParams } from "solid-start";
import { token } from "~/stores/auth";

const FAUCET_API_URL = import.meta.env.VITE_FAUCET_API;

Expand Down Expand Up @@ -68,6 +69,7 @@ export function Faucet() {
body: JSON.stringify({ sats: howMuchSats, address: toAddress }),
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${token()}`,
},
});

Expand Down
2 changes: 2 additions & 0 deletions src/components/LnChannel.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { createSignal, Match, Switch } from "solid-js";
import { createRouteAction } from "solid-start";
import { token } from "~/stores/auth";

const FAUCET_API_URL = import.meta.env.VITE_FAUCET_API;

Expand Down Expand Up @@ -99,6 +100,7 @@ export function LnChannel() {
}),
headers: {
"Content-Type": "application/json",
"Authorization": `Bearer ${token()}`,
},
});

Expand Down
2 changes: 2 additions & 0 deletions src/components/LnFaucet.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { token } from "~/stores/auth";
import { Match, Switch } from "solid-js";
import { createRouteAction } from "solid-start";

Expand Down Expand Up @@ -71,6 +72,7 @@ export function LnFaucet() {
body: JSON.stringify({ bolt11 }),
headers: {
"Content-Type": "application/json",
"Authorization": `Bearer ${token()}`,
},
});

Expand Down
2 changes: 2 additions & 0 deletions src/components/NWC.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Match, Switch, createSignal } from "solid-js";
import { createRouteAction } from "solid-start";
import { token } from "~/stores/auth";

import NDK, { NDKEvent, NDKPrivateKeySigner } from "@nostr-dev-kit/ndk";

Expand Down Expand Up @@ -111,6 +112,7 @@ async function fetchBolt11(): Promise<{ bolt11: string }> {
body: JSON.stringify({ amount_sats: 21 }),
headers: {
"Content-Type": "application/json",
"Authorization": `Bearer ${token()}`,
},
});

Expand Down
9 changes: 6 additions & 3 deletions src/root.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
// @refresh reload
import { Suspense } from "solid-js";
import {Match, Show, Suspense, Switch} from "solid-js";
import {
useLocation,
A,
Body,
ErrorBoundary,
FileRoutes,
Expand All @@ -15,6 +13,8 @@ import {
Link,
} from "solid-start";
import "./root.css";
import { AuthButton } from "./components/AuthButton";
import {token} from "~/stores/auth";

export default function Root() {
return (
Expand All @@ -24,6 +24,9 @@ export default function Root() {
<Meta charset="utf-8" />
<Meta name="viewport" content="width=device-width, initial-scale=1" />
<Link rel="icon" href="/favicon.svg" />
<Show when={token()}>
<AuthButton />
</Show>
</Head>
<Body>
<Suspense>
Expand Down
47 changes: 37 additions & 10 deletions src/routes/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,42 @@ import { Faucet } from "~/components/Faucet";
import { LnChannel } from "~/components/LnChannel";
import { LnFaucet } from "~/components/LnFaucet";
import { NWC } from "~/components/NWC";
import {Match, onMount, Show, Switch} from "solid-js";
import {setToken, token} from "~/stores/auth";
import {AuthButton} from "~/components/AuthButton";

onMount(() => {
// Handle auth callback
const params = new URLSearchParams(window.location.search);
const token = params.get("token");

if (token) {
// Store token in localStorage and state
localStorage.setItem("token", token);
setToken(token);

// Clean up URL
window.history.replaceState({}, document.title, window.location.pathname);
}
});

export default function Home() {
return (
<main class="flex flex-col gap-4 items-center w-full max-w-[40rem] mx-auto">
<h1 class="font-mono text-4xl drop-shadow-text-glow p-8 font-bold">
mutinynet
</h1>
<Faucet />
<LnFaucet />
<LnChannel />
<NWC />
<Switch>
<Match when={token()}>
<Faucet />
<LnFaucet />
<LnChannel />
<NWC />
</Match>
<Match when={true}>
<AuthButton />
</Match>
</Switch>
<div class="border border-white/50 rounded-xl p-4 w-full gap-2 flex flex-col">
<h1 class="font-bold text-xl font-mono">Send back your unused sats</h1>
<pre class="overflow-x-auto whitespace-pre-line break-all p-4 bg-white/10 rounded-lg">
Expand All @@ -28,12 +53,14 @@ export default function Home() {
02465ed5be53d04fde66c9418ff14a5f2267723810176c9212b722e542dc1afb1b@45.79.52.207:9735
</pre>
</div>
<div class="border border-white/50 rounded-xl p-4 w-full gap-2 flex flex-col">
<h1 class="font-bold text-xl font-mono">Infinite LNURL Withdrawal!</h1>
<pre class="overflow-x-auto whitespace-pre-line break-all p-4 bg-white/10 rounded-lg">
lnurl1dp68gurn8ghj7enpw43k2apwd46hg6tw09hx2apwvdhk6tmpwp5j7mrww4excac7utxd6
</pre>
</div>
<Show when={token()}>
<div class="border border-white/50 rounded-xl p-4 w-full gap-2 flex flex-col">
<h1 class="font-bold text-xl font-mono">Infinite LNURL Withdrawal!</h1>
<pre class="overflow-x-auto whitespace-pre-line break-all p-4 bg-white/10 rounded-lg">
lnurl1dp68gurn8ghj7enpw43k2apwd46hg6tw09hx2apwvdhk6tmpwp5j7mrww4excac7utxd6
</pre>
</div>
</Show>
<div class="border border-white/50 rounded-xl p-4 w-full gap-2 flex flex-col">
<h1 class="font-bold text-xl font-mono">Join the Federation</h1>
<pre class="overflow-x-auto whitespace-pre-line break-all p-4 bg-white/10 rounded-lg">
Expand Down
21 changes: 21 additions & 0 deletions src/stores/auth.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import {createEffect, createSignal} from "solid-js";

// Check if window is defined (client-side)
const isClient = typeof window !== 'undefined';

// Create signals for auth state
export const [token, setToken] = createSignal<string | null>(
isClient ? localStorage.getItem("token") : null
);


const FAUCET_API_URL = import.meta.env.VITE_FAUCET_API;

export const login = () => {
window.location.href = `${FAUCET_API_URL}/auth/github`;
};

export const logout = () => {
localStorage.removeItem("token");
setToken(null);
};

0 comments on commit c80f2dd

Please sign in to comment.