From c80f2dddb328a2df49204237aaf6b70e969b15fb Mon Sep 17 00:00:00 2001 From: benthecarman Date: Thu, 19 Dec 2024 01:04:11 -0600 Subject: [PATCH] Require gh login --- src/api/client.ts | 23 +++++++++++++++++ src/components/AuthButton.tsx | 13 ++++++++++ src/components/Faucet.tsx | 2 ++ src/components/LnChannel.tsx | 2 ++ src/components/LnFaucet.tsx | 2 ++ src/components/NWC.tsx | 2 ++ src/root.tsx | 9 ++++--- src/routes/index.tsx | 47 +++++++++++++++++++++++++++-------- src/stores/auth.ts | 21 ++++++++++++++++ 9 files changed, 108 insertions(+), 13 deletions(-) create mode 100644 src/api/client.ts create mode 100644 src/components/AuthButton.tsx create mode 100644 src/stores/auth.ts diff --git a/src/api/client.ts b/src/api/client.ts new file mode 100644 index 0000000..1153e5d --- /dev/null +++ b/src/api/client.ts @@ -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(); + }, +}; \ No newline at end of file diff --git a/src/components/AuthButton.tsx b/src/components/AuthButton.tsx new file mode 100644 index 0000000..595a491 --- /dev/null +++ b/src/components/AuthButton.tsx @@ -0,0 +1,13 @@ +import { Component } from "solid-js"; +import { token, login, logout } from "~/stores/auth"; + +export const AuthButton: Component = () => { + return ( + + ); +}; \ No newline at end of file diff --git a/src/components/Faucet.tsx b/src/components/Faucet.tsx index 26365bd..9ebef41 100644 --- a/src/components/Faucet.tsx +++ b/src/components/Faucet.tsx @@ -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; @@ -68,6 +69,7 @@ export function Faucet() { body: JSON.stringify({ sats: howMuchSats, address: toAddress }), headers: { "Content-Type": "application/json", + Authorization: `Bearer ${token()}`, }, }); diff --git a/src/components/LnChannel.tsx b/src/components/LnChannel.tsx index 2847445..bedbc04 100644 --- a/src/components/LnChannel.tsx +++ b/src/components/LnChannel.tsx @@ -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; @@ -99,6 +100,7 @@ export function LnChannel() { }), headers: { "Content-Type": "application/json", + "Authorization": `Bearer ${token()}`, }, }); diff --git a/src/components/LnFaucet.tsx b/src/components/LnFaucet.tsx index e9676fe..18b04be 100644 --- a/src/components/LnFaucet.tsx +++ b/src/components/LnFaucet.tsx @@ -1,3 +1,4 @@ +import { token } from "~/stores/auth"; import { Match, Switch } from "solid-js"; import { createRouteAction } from "solid-start"; @@ -71,6 +72,7 @@ export function LnFaucet() { body: JSON.stringify({ bolt11 }), headers: { "Content-Type": "application/json", + "Authorization": `Bearer ${token()}`, }, }); diff --git a/src/components/NWC.tsx b/src/components/NWC.tsx index 09c9432..d55008a 100644 --- a/src/components/NWC.tsx +++ b/src/components/NWC.tsx @@ -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"; @@ -111,6 +112,7 @@ async function fetchBolt11(): Promise<{ bolt11: string }> { body: JSON.stringify({ amount_sats: 21 }), headers: { "Content-Type": "application/json", + "Authorization": `Bearer ${token()}`, }, }); diff --git a/src/root.tsx b/src/root.tsx index 5544371..aa3a27c 100644 --- a/src/root.tsx +++ b/src/root.tsx @@ -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, @@ -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 ( @@ -24,6 +24,9 @@ export default function Root() { + + + diff --git a/src/routes/index.tsx b/src/routes/index.tsx index 8359c9f..69329f4 100644 --- a/src/routes/index.tsx +++ b/src/routes/index.tsx @@ -2,6 +2,24 @@ 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 ( @@ -9,10 +27,17 @@ export default function Home() {

mutinynet

- - - - + + + + + + + + + + +

Send back your unused sats

@@ -28,12 +53,14 @@ export default function Home() {
             02465ed5be53d04fde66c9418ff14a5f2267723810176c9212b722e542dc1afb1b@45.79.52.207:9735
         
-
-

Infinite LNURL Withdrawal!

-
-          lnurl1dp68gurn8ghj7enpw43k2apwd46hg6tw09hx2apwvdhk6tmpwp5j7mrww4excac7utxd6
-        
-
+ +
+

Infinite LNURL Withdrawal!

+
+              lnurl1dp68gurn8ghj7enpw43k2apwd46hg6tw09hx2apwvdhk6tmpwp5j7mrww4excac7utxd6
+            
+
+

Join the Federation

diff --git a/src/stores/auth.ts b/src/stores/auth.ts
new file mode 100644
index 0000000..7794bf9
--- /dev/null
+++ b/src/stores/auth.ts
@@ -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(
+    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);
+};