Graphical PDS migrator for AT Protocol

auth middleware

Changed files
+30 -27
islands
routes
-16
islands/Counter.tsx
···
-
import type { Signal } from "@preact/signals";
-
import { Button } from "../components/Button.tsx";
-
-
interface CounterProps {
-
count: Signal<number>;
-
}
-
-
export default function Counter(props: CounterProps) {
-
return (
-
<div class="flex gap-8 py-6">
-
<Button onClick={() => props.count.value -= 1}>-1</Button>
-
<p class="text-3xl tabular-nums">{props.count}</p>
-
<Button onClick={() => props.count.value += 1}>+1</Button>
-
</div>
-
);
-
}
···
+3 -2
islands/CredLogin.tsx
···
import { useState } from 'preact/hooks'
import { JSX } from 'preact'
-
export default function CredLogin() {
const [handle, setHandle] = useState('')
const [password, setPassword] = useState('')
const [error, setError] = useState<string | null>(null)
···
await new Promise((resolve) => setTimeout(resolve, 500))
// Redirect to home page after successful login
-
globalThis.location.href = '/'
} catch (err) {
const message = err instanceof Error ? err.message : 'Login failed'
setError(message)
···
import { useState } from 'preact/hooks'
import { JSX } from 'preact'
+
import { LoginProps } from "./LoginSelector.tsx";
+
export default function CredLogin({ redirect }: LoginProps) {
const [handle, setHandle] = useState('')
const [password, setPassword] = useState('')
const [error, setError] = useState<string | null>(null)
···
await new Promise((resolve) => setTimeout(resolve, 500))
// Redirect to home page after successful login
+
globalThis.location.href = '/login/callback?redirect=' + encodeURIComponent(redirect)
} catch (err) {
const message = err instanceof Error ? err.message : 'Login failed'
setError(message)
+5 -3
islands/Ticket.tsx
···
</p>
<p>
Think you might need to migrate in the future but your PDS might be
-
hostile or offline? No worries! You can head to the ticket booth and
-
get a PLC key free of charge and use it for account recovery in the
-
future. You can also go to baggage claim (take the air shuttle to
terminal four) and get a downloadable backup of all your current PDS
data in case that were to happen.
</p>
···
</p>
<p>
Think you might need to migrate in the future but your PDS might be
+
hostile or offline? No worries! Soon you'll be able to go to the
+
ticket booth and get a PLC key free of charge and use it for account
+
recovery in the future. You can also go to baggage claim (take the air
+
shuttle to terminal four) and get a downloadable backup of all your
+
current PDS data in case that were to happen.
terminal four) and get a downloadable backup of all your current PDS
data in case that were to happen.
</p>
+17 -3
main.ts
···
app.use(staticFiles());
// this can also be defined via a file. feel free to delete this!
-
const exampleLoggerMiddleware = define.middleware((ctx) => {
-
console.log(`${ctx.req.method} ${ctx.req.url}`);
return ctx.next();
});
-
app.use(exampleLoggerMiddleware);
await fsRoutes(app, {
loadIsland: (path) => import(`./islands/${path}`),
···
app.use(staticFiles());
// this can also be defined via a file. feel free to delete this!
+
const authMiddleware = define.middleware(async (ctx) => {
+
const url = new URL(ctx.req.url);
+
if (url.pathname.startsWith("/migrate")) {
+
ctx.state.auth = true
+
}
+
if (ctx.state.auth) {
+
const me = await fetch(`${url.origin}/api/me`, {
+
credentials: "include",
+
});
+
const json = await me.json();
+
if (json !== null && json.did) {
+
return ctx.next();
+
} else {
+
return ctx.redirect("/login");
+
}
+
}
return ctx.next();
});
+
app.use(authMiddleware);
await fsRoutes(app, {
loadIsland: (path) => import(`./islands/${path}`),
+1 -1
routes/index.tsx
···
/>
</div>
<p class="font-mono text-lg sm:text-xl mb-4 mt-4 sm:mb-6 mt-0 text-center text-gray-600 dark:text-gray-300">
-
Airport is made with love by <a class="text-blue-500 hover:underline" href="https://bsky.app/profile/knotbin.com">Roscoe</a> in collaboration with <a class="text-blue-500 hover:underline" href="https://sprk.so">Spark</a>.
</p>
<SocialLinks />
</div>
···
/>
</div>
<p class="font-mono text-lg sm:text-xl mb-4 mt-4 sm:mb-6 mt-0 text-center text-gray-600 dark:text-gray-300">
+
Airport is made with love by <a class="text-blue-500 hover:underline" href="https://bsky.app/profile/knotbin.com">Roscoe</a> for <a class="text-blue-500 hover:underline" href="https://sprk.so">Spark</a>, a new short-video platform for AT Protocol.
</p>
<SocialLinks />
</div>
+4 -2
utils.ts
···
import { createDefine } from "fresh";
-
// deno-lint-ignore no-empty-interface
-
export interface State {}
export const define = createDefine<State>();
···
import { createDefine } from "fresh";
+
export interface State {
+
title?: string;
+
auth: boolean;
+
}
export const define = createDefine<State>();