Skip to content

Commit 67b3682

Browse files
committed
update
1 parent 2bcd3fa commit 67b3682

File tree

22 files changed

+256
-278
lines changed

22 files changed

+256
-278
lines changed

examples/with-solidstart/README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,18 +40,18 @@ To ensure Web3-related logic runs only on the client, use the `clientOnly` utili
4040
1. **Client-Only Component** (e.g. for a component showing user balance)
4141

4242
```jsx
43-
import { clientOnly } from '@solidjs/start/client'
43+
import { clientOnly } from "@solidjs/start/client";
4444

45-
const ClientComponent = clientOnly(() => import('./ClientOnlyComponent'))
45+
const ClientComponent = clientOnly(() => import("./ClientOnlyComponent"));
4646
```
4747

4848
2. **Client-Only Page** (e.g. for a `/swap` page)
4949
Add the following at the top of your route file to render the entire page on the client:
5050

5151
```jsx
52-
import { clientOnly } from '@solidjs/start/client'
52+
import { clientOnly } from "@solidjs/start/client";
5353

54-
export default clientOnly(async () => ({ default: MyPage }))
54+
export default clientOnly(async () => ({ default: MyPage }));
5555
```
5656

5757
For more details, refer to the `clientOnly` [documentation](https://docs.solidjs.com/solid-start/reference/client/client-only#clientonly).
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import { defineConfig } from '@solidjs/start/config'
2-
import tailwindcss from '@tailwindcss/vite'
1+
import { defineConfig } from "@solidjs/start/config";
2+
import tailwindcss from "@tailwindcss/vite";
33

44
export default defineConfig({
5-
server: { preset: '' }, // your deployment
5+
server: { preset: "" }, // your deployment
66
vite: { plugins: [tailwindcss()] }
7-
})
7+
});

examples/with-solidstart/src/app.css

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
@import 'tailwindcss';
1+
@import "tailwindcss";
22

33
#app {
44
background-color: white;
Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,21 @@
1-
import { Suspense } from 'solid-js'
2-
import { Router, type RouteDefinition } from '@solidjs/router'
3-
import { FileRoutes } from '@solidjs/start/router'
4-
import { MetaProvider } from '@solidjs/meta'
5-
import { querySession } from './auth'
6-
import AuthProvider from './auth/Provider'
7-
import Nav from './components/Nav'
8-
import ErrorNotification from './components/Error'
9-
import './app.css'
1+
import { Suspense } from "solid-js";
2+
import { Router, type RouteDefinition } from "@solidjs/router";
3+
import { FileRoutes } from "@solidjs/start/router";
4+
import { MetaProvider } from "@solidjs/meta";
5+
import { querySession } from "./auth";
6+
import AuthProvider from "./auth/Provider";
7+
import Nav from "./components/Nav";
8+
import ErrorNotification from "./components/Error";
9+
import "./app.css";
1010

1111
export const route: RouteDefinition = {
1212
preload: ({ location }) => querySession(location.pathname)
13-
}
13+
};
1414

1515
export default function App() {
1616
return (
1717
<Router
18-
root={props => (
18+
root={(props) => (
1919
<MetaProvider>
2020
<AuthProvider>
2121
<Suspense>
@@ -29,5 +29,5 @@ export default function App() {
2929
>
3030
<FileRoutes />
3131
</Router>
32-
)
32+
);
3333
}
Lines changed: 45 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,97 +1,90 @@
1-
import { createEffect, createResource, on, type ParentProps } from 'solid-js'
1+
import { createEffect, createResource, on, type ParentProps } from "solid-js";
22
import {
33
useLocation,
44
createAsync,
55
useAction,
66
useSearchParams
7-
} from '@solidjs/router'
8-
import injectedWallets from '@web3-onboard/injected-wallets'
9-
import { signOutAction, querySession } from '~/auth'
10-
import { sign, authWalletAction, addWalletAction } from '~/auth/web3'
11-
import useWeb3Onboard, { BASE_ID, load } from '~/web3'
12-
import Context from './context'
7+
} from "@solidjs/router";
8+
import injectedWallets from "@web3-onboard/injected-wallets";
9+
import { signOutAction, querySession } from "~/auth";
10+
import { sign, authWalletAction, addWalletAction } from "~/auth/web3";
11+
import useWeb3Onboard, { load } from "~/web3";
12+
import Context from "./context";
1313

1414
export default function AuthProvider(props: ParentProps) {
15-
const location = useLocation()
16-
const [searchParams, setSearchParams] = useSearchParams()
15+
const location = useLocation();
16+
const [searchParams, setSearchParams] = useSearchParams();
1717
const session = createAsync(() => querySession(location.pathname), {
1818
deferStream: true
19-
})
19+
});
2020

21-
const authWallet = useAction(authWalletAction)
22-
const addWallet = useAction(addWalletAction)
23-
const signOut = useAction(signOutAction)
24-
const signedIn = () => Boolean(session()?.id)
21+
const authWallet = useAction(authWalletAction);
22+
const addWallet = useAction(addWalletAction);
23+
const signOut = useAction(signOutAction);
24+
const signedIn = () => Boolean(session()?.id);
2525

2626
const onboard = useWeb3Onboard({
2727
wallets: [injectedWallets()],
2828
connect: { autoConnectLastWallet: true },
29-
chains: [{ id: BASE_ID }],
29+
chains: [{ id: "0x1" }, { id: "0x2105" }],
3030
appMetadata: {
31-
name: 'SolidStart',
32-
description: 'SolidStart web3-onboard template',
33-
icon: 'favicon.svg'
31+
name: "SolidStart",
32+
description: "Web3-onboard template",
33+
icon: "favicon.svg"
3434
}
35-
})
35+
});
3636

3737
createResource(
38-
() => searchParams.login === 'true' && !signedIn() && onboard,
39-
async instance => {
38+
() => searchParams.login === "true" && !signedIn() && onboard,
39+
async (instance) => {
4040
try {
41-
const [wallet] = await instance.connectWallet()
42-
if (!wallet?.provider) throw new Error('Wallet connection failed')
43-
const address = await sign(wallet.provider)
44-
const r = searchParams.redirect
45-
await authWallet(address, Array.isArray(r) ? r[0] : r)
41+
const [wallet] = await instance.connectWallet();
42+
if (!wallet?.provider) throw new Error("Wallet connection failed");
43+
const address = await sign(wallet.provider);
44+
const r = searchParams.redirect;
45+
await authWallet(address, Array.isArray(r) ? r[0] : r);
4646
} catch (err) {
4747
setSearchParams({
48-
error: err instanceof Error ? err.message : '',
49-
login: ''
50-
})
48+
error: err instanceof Error ? err.message : "",
49+
login: ""
50+
});
5151
}
5252
}
53-
)
53+
);
5454

55-
const [web3] = createResource(
56-
onboard?.connectedWallet,
57-
async ({ provider }) => {
58-
if (onboard!.connectedChain().id !== BASE_ID)
59-
await onboard!.setChain({ chainId: BASE_ID })
60-
return load(provider)
61-
}
62-
)
55+
const [web3] = createResource(onboard?.connectedWallet, load);
6356

6457
createEffect(
6558
on(
6659
() => onboard?.walletAddress(),
6760
async (current, previous) => {
68-
const saved = session()?.wallets
69-
if (!saved?.length || !previous) return
70-
if (!current) await signOut()
61+
const saved = session()?.wallets;
62+
if (!saved?.length || !previous) return;
63+
if (!current) await signOut();
7164
if (current && current !== previous) {
7265
try {
73-
const { provider } = onboard!.connectedWallet()
74-
const addr = current.toLowerCase()
75-
const verified = saved.includes(addr) ? addr : await sign(provider)
76-
await addWallet(verified)
66+
const { provider } = onboard!.connectedWallet();
67+
const addr = current.toLowerCase();
68+
const verified = saved.includes(addr) ? addr : await sign(provider);
69+
await addWallet(verified);
7770
} catch (err) {
78-
setSearchParams({ error: err instanceof Error ? err.message : '' })
71+
setSearchParams({ error: err instanceof Error ? err.message : "" });
7972
}
8073
}
8174
},
8275
{ defer: true }
8376
)
84-
)
77+
);
8578

8679
const logout = async () => {
87-
const wallet = onboard?.connectedWallet()
88-
if (wallet) await onboard!.disconnectWallet({ label: wallet.label })
89-
return signOut()
90-
}
80+
const wallet = onboard?.connectedWallet();
81+
if (wallet) await onboard!.disconnectWallet({ label: wallet.label });
82+
return signOut();
83+
};
9184

9285
return (
9386
<Context.Provider value={{ session, signedIn, logout, web3 }}>
9487
{props.children}
9588
</Context.Provider>
96-
)
89+
);
9790
}
Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1-
import { createContext, type Resource } from 'solid-js'
2-
import type { AccessorWithLatest } from '@solidjs/router'
3-
import type { Session } from '~/auth/server'
4-
import type { Web3 } from '~/web3'
1+
import { createContext, type Resource } from "solid-js";
2+
import type { AccessorWithLatest } from "@solidjs/router";
3+
import type { Session } from "~/auth/server";
4+
import type { Web3 } from "~/web3";
55

66
interface Context {
7-
session: AccessorWithLatest<Session | undefined>
8-
signedIn: () => boolean
9-
logout: () => Promise<never>
10-
web3: Resource<Web3>
7+
session: AccessorWithLatest<Session | undefined>;
8+
signedIn: () => boolean;
9+
logout: () => Promise<never>;
10+
web3: Resource<Web3>;
1111
}
1212

13-
export default createContext<Context>()
13+
export default createContext<Context>();
Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,34 @@
1-
import { useContext } from 'solid-js'
2-
import { query, redirect, action } from '@solidjs/router'
3-
import { getSession } from './server'
4-
import context from './context'
1+
import { useContext } from "solid-js";
2+
import { query, redirect, action } from "@solidjs/router";
3+
import { getSession } from "./server";
4+
import context from "./context";
55

6-
const PROTECTED_ROUTES = ['/']
6+
const PROTECTED_ROUTES = ["/"];
77

88
export const protectedRoute = (path: string) =>
9-
PROTECTED_ROUTES.some(route =>
10-
route.endsWith('/*')
9+
PROTECTED_ROUTES.some((route) =>
10+
route.endsWith("/*")
1111
? path.startsWith(route.slice(0, -2))
12-
: path === route || path.startsWith(route + '/')
13-
)
12+
: path === route || path.startsWith(route + "/")
13+
);
1414

1515
export const querySession = query(async (path: string) => {
16-
'use server'
17-
const { data: session } = await getSession()
18-
if (session.wallets?.length) return session
19-
if (protectedRoute(path)) throw redirect('/about?login=true&redirect=' + path)
20-
}, 'querySession')
16+
"use server";
17+
const { data: session } = await getSession();
18+
if (session.wallets?.length) return session;
19+
if (protectedRoute(path))
20+
throw redirect("/about?login=true&redirect=" + path);
21+
}, "querySession");
2122

2223
export const signOutAction = action(async () => {
23-
'use server'
24-
const session = await getSession()
25-
await session.update({ wallets: undefined })
26-
throw redirect('/about', { revalidate: querySession.key })
27-
})
24+
"use server";
25+
const session = await getSession();
26+
await session.update({ wallets: undefined });
27+
throw redirect("/about", { revalidate: querySession.key });
28+
});
2829

2930
export default function useAuth() {
30-
const ctx = useContext(context)
31-
if (!ctx) throw new Error('useAuth must be used within AuthProvider')
32-
return ctx
31+
const ctx = useContext(context);
32+
if (!ctx) throw new Error("useAuth must be used within AuthProvider");
33+
return ctx;
3334
}
Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,22 @@
1-
import { redirect } from '@solidjs/router'
2-
import { useSession } from 'vinxi/http'
1+
import { redirect } from "@solidjs/router";
2+
import { useSession } from "vinxi/http";
33

44
export interface Session {
5-
id: number | undefined
6-
wallets: string[] | undefined
5+
id: number | undefined;
6+
wallets: string[] | undefined;
77
}
88

99
export const getSession = () =>
10-
useSession<Session>({ password: process.env.SESSION_SECRET! })
10+
useSession<Session>({ password: process.env.SESSION_SECRET! });
1111

1212
export const updateSession = async (wallets: string[], id?: number) => {
13-
const session = await getSession()
13+
const session = await getSession();
1414
const { data } = await session.update(() => ({
1515
id: id || session.data.id,
1616
wallets
17-
}))
18-
return data as Session
19-
}
17+
}));
18+
return data as Session;
19+
};
2020

2121
export const safeRedirect = (url?: string) =>
22-
redirect(url?.startsWith('/') && !url.startsWith('//') ? url : '/')
22+
redirect(url?.startsWith("/") && !url.startsWith("//") ? url : "/");

0 commit comments

Comments
 (0)