Skip to content

Commit cf8912b

Browse files
committed
serve astro assets directly through nginx and optimize script loading order
1 parent 76bc86a commit cf8912b

7 files changed

Lines changed: 88 additions & 76 deletions

File tree

astro-app/astro.config.mjs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@ import node from "@astrojs/node";
99
export default defineConfig({
1010
base: "/xnode-auth",
1111

12+
build: {
13+
client: "./xnode-auth",
14+
},
15+
1216
vite: {
1317
plugins: [tailwindcss()],
1418
ssr: {
Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,43 @@
11
<appkit-button balance="hide"></appkit-button>
22
<script>
3-
import "../scripts/appkit";
3+
import { createAppKit } from "@reown/appkit";
4+
import { mainnet } from "@reown/appkit/networks";
5+
import { appkitStore } from "../store/appkit";
6+
import { isAddress } from "viem";
7+
import { projectId, wagmiAdapter } from "../scripts/wagmi";
8+
9+
const metadata = {
10+
name: "Xnode Auth",
11+
description: "Web3 authenticator and login dashboard.",
12+
url: "https://auth.xnode.openmesh.network",
13+
icons: ["https://auth.xnode.openmesh.network/favicon.svg"],
14+
};
15+
16+
const modal = createAppKit({
17+
adapters: [wagmiAdapter],
18+
networks: [mainnet],
19+
metadata,
20+
projectId,
21+
defaultAccountTypes: {
22+
eip155: "eoa",
23+
},
24+
// social features only work with projects that have whitelisted domains, which cannot be done for the default project id, as it would prevent it to be used on all websites
25+
features: {
26+
...(projectId === "6afdeb3a0496b33061a69538819a9a7e"
27+
? { email: false, socials: false }
28+
: {}),
29+
analytics: false,
30+
swaps: false,
31+
onramp: false,
32+
},
33+
});
34+
35+
modal.subscribeAccount((state) => {
36+
const address = state.address;
37+
if (address === undefined || isAddress(address)) {
38+
appkitStore.account.set(address);
39+
} else {
40+
console.warn(`Invalid appkit address ${address}`);
41+
}
42+
});
443
</script>

astro-app/src/components/Login.astro

Lines changed: 19 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -51,33 +51,29 @@ import Connect from "../components/Connect.astro";
5151
>
5252
</div>
5353
<script>
54-
import { wagmiAdapter } from "../scripts/appkit";
55-
import { getMessage } from "../lib/message";
56-
import { toXnodeAddress } from "../lib/xnode-address";
57-
import { appkitStore } from "../store/appkit";
58-
import { signMessage } from "@wagmi/core";
54+
const urlParams = new URLSearchParams(window.location.search);
55+
const redirect = urlParams.get("redirect") ?? window.location.origin;
56+
const rejected = urlParams.get("rejected");
5957

60-
function setRedirect(redirect: string) {
58+
export function setRedirect(redirect: string) {
6159
const node = document.getElementById("redirect");
6260
if (node) {
6361
node.textContent = redirect;
6462
}
6563
}
6664

67-
function setError(error: string) {
65+
export function setError(error: string) {
6866
const node = document.getElementById("error");
6967
if (node) {
7068
node.style.display = error ? "flex" : "none";
7169
node.textContent = error;
7270
}
7371
}
7472

75-
let signingState = false;
76-
function setSigning(signing: boolean) {
77-
signingState = signing;
78-
updateDisplay();
79-
}
73+
setRedirect(redirect);
74+
setError(rejected ?? "");
8075

76+
const { appkitStore } = await import("../store/appkit");
8177
function updateDisplay() {
8278
// Signing indicator
8379
{
@@ -98,13 +94,11 @@ import Connect from "../components/Connect.astro";
9894
}
9995
}
10096

101-
const urlParams = new URLSearchParams(window.location.search);
102-
103-
const redirect = urlParams.get("redirect") ?? window.location.origin;
104-
setRedirect(redirect);
105-
106-
const rejected = urlParams.get("rejected");
107-
setError(rejected ?? "");
97+
let signingState = false;
98+
function setSigning(signing: boolean) {
99+
signingState = signing;
100+
updateDisplay();
101+
}
108102

109103
appkitStore.account.subscribe((account) => {
110104
if (!account) {
@@ -113,6 +107,10 @@ import Connect from "../components/Connect.astro";
113107
updateDisplay();
114108
});
115109

110+
const { wagmiAdapter } = await import("../scripts/wagmi");
111+
const { getMessage } = await import("../lib/message");
112+
const { toXnodeAddress } = await import("../lib/xnode-address");
113+
const { signMessage } = await import("@wagmi/core");
116114
document
117115
.getElementById("authenticate")
118116
?.addEventListener("click", async () => {
@@ -164,8 +162,7 @@ import Connect from "../components/Connect.astro";
164162
setSigning(false);
165163
}
166164
});
167-
</script>
168-
<script>
169-
import sdk from "@farcaster/miniapp-sdk";
165+
166+
const { sdk } = await import("@farcaster/miniapp-sdk");
170167
sdk.actions.ready();
171168
</script>

astro-app/src/pages/api/login.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ export const POST: APIRoute = async ({ request, cookies }) => {
2323
httpOnly: true,
2424
secure: true,
2525
sameSite: "none",
26+
path: "/",
2627
} as const;
2728
cookies.set("xnode_auth_user", user, cookieOptions);
2829
if (user?.startsWith("eth:")) {

astro-app/src/scripts/appkit.ts

Lines changed: 0 additions & 52 deletions
This file was deleted.

astro-app/src/scripts/wagmi.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import { mainnet } from "@reown/appkit/networks";
2+
import { WagmiAdapter } from "@reown/appkit-adapter-wagmi";
3+
import { config } from "../lib/config";
4+
import farcasterMiniApp from "@farcaster/miniapp-wagmi-connector";
5+
6+
export const projectId = config.eth.projectid;
7+
8+
export const networks = [mainnet];
9+
10+
export const wagmiAdapter = new WagmiAdapter({
11+
projectId,
12+
networks,
13+
connectors: [farcasterMiniApp()],
14+
});

nix/nixos-module.nix

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -332,7 +332,16 @@ in
332332
))
333333
{
334334
"${cfg.nginxConfig.subpath}" = {
335-
proxyPass = "http://127.0.0.1:${builtins.toString cfg.port}";
335+
root = "${xnode-auth}/share";
336+
};
337+
"${cfg.nginxConfig.subpath}/_astro" = {
338+
root = "${xnode-auth}/share";
339+
extraConfig = ''
340+
add_header Cache-Control "public, max-age=31536000, immutable";
341+
'';
342+
};
343+
"${cfg.nginxConfig.subpath}/api" = {
344+
proxyPass = "http://127.0.0.1:${builtins.toString cfg.port}${cfg.nginxConfig.subpath}/api";
336345
extraConfig = ''
337346
proxy_set_header Host $server_name;
338347
'';

0 commit comments

Comments
 (0)