|
1 | | -import { createEffect, createResource, on, type ParentProps } from "solid-js"; |
| 1 | +import { createEffect, createResource, on, type ParentProps } from 'solid-js' |
2 | 2 | import { |
3 | 3 | useLocation, |
4 | 4 | createAsync, |
5 | 5 | useAction, |
6 | 6 | 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, { BASE_ID, load } from '~/web3' |
| 12 | +import Context from './context' |
13 | 13 |
|
14 | 14 | export default function AuthProvider(props: ParentProps) { |
15 | | - const location = useLocation(); |
16 | | - const [searchParams, setSearchParams] = useSearchParams(); |
| 15 | + const location = useLocation() |
| 16 | + const [searchParams, setSearchParams] = useSearchParams() |
17 | 17 | const session = createAsync(() => querySession(location.pathname), { |
18 | 18 | deferStream: true |
19 | | - }); |
| 19 | + }) |
20 | 20 |
|
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) |
25 | 25 |
|
26 | 26 | const onboard = useWeb3Onboard({ |
27 | 27 | wallets: [injectedWallets()], |
28 | 28 | connect: { autoConnectLastWallet: true }, |
29 | 29 | chains: [{ id: BASE_ID }], |
30 | 30 | appMetadata: { |
31 | | - name: "SolidStart", |
32 | | - description: "SolidStart web3-onboard template", |
33 | | - icon: "favicon.svg" |
| 31 | + name: 'SolidStart', |
| 32 | + description: 'SolidStart web3-onboard template', |
| 33 | + icon: 'favicon.svg' |
34 | 34 | } |
35 | | - }); |
| 35 | + }) |
36 | 36 |
|
37 | 37 | createResource( |
38 | | - () => searchParams.login === "true" && !signedIn() && onboard, |
39 | | - async (instance) => { |
| 38 | + () => searchParams.login === 'true' && !signedIn() && onboard, |
| 39 | + async instance => { |
40 | 40 | 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) |
46 | 46 | } catch (err) { |
47 | 47 | setSearchParams({ |
48 | | - error: err instanceof Error ? err.message : "", |
49 | | - login: "" |
50 | | - }); |
| 48 | + error: err instanceof Error ? err.message : '', |
| 49 | + login: '' |
| 50 | + }) |
51 | 51 | } |
52 | 52 | } |
53 | | - ); |
| 53 | + ) |
54 | 54 |
|
55 | 55 | const [web3] = createResource( |
56 | 56 | onboard?.connectedWallet, |
57 | 57 | async ({ provider }) => { |
58 | 58 | if (onboard!.connectedChain().id !== BASE_ID) |
59 | | - await onboard!.setChain({ chainId: BASE_ID }); |
60 | | - return load(provider); |
| 59 | + await onboard!.setChain({ chainId: BASE_ID }) |
| 60 | + return load(provider) |
61 | 61 | } |
62 | | - ); |
| 62 | + ) |
63 | 63 |
|
64 | 64 | createEffect( |
65 | 65 | on( |
66 | 66 | () => onboard?.walletAddress(), |
67 | 67 | async (current, previous) => { |
68 | | - const saved = session()?.wallets; |
69 | | - if (!saved?.length || !previous) return; |
70 | | - if (!current) await signOut(); |
| 68 | + const saved = session()?.wallets |
| 69 | + if (!saved?.length || !previous) return |
| 70 | + if (!current) await signOut() |
71 | 71 | if (current && current !== previous) { |
72 | 72 | 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); |
| 73 | + const { provider } = onboard!.connectedWallet() |
| 74 | + const addr = current.toLowerCase() |
| 75 | + const verified = saved.includes(addr) ? addr : await sign(provider) |
| 76 | + await addWallet(verified) |
77 | 77 | } catch (err) { |
78 | | - setSearchParams({ error: err instanceof Error ? err.message : "" }); |
| 78 | + setSearchParams({ error: err instanceof Error ? err.message : '' }) |
79 | 79 | } |
80 | 80 | } |
81 | 81 | }, |
82 | 82 | { defer: true } |
83 | 83 | ) |
84 | | - ); |
| 84 | + ) |
85 | 85 |
|
86 | 86 | const logout = async () => { |
87 | | - try { |
88 | | - const wallet = onboard?.connectedWallet(); |
89 | | - if (wallet) { |
90 | | - await onboard!.disconnectWallet({ label: wallet.label }); |
91 | | - } |
92 | | - } finally { |
93 | | - await signOut(); |
94 | | - } |
95 | | - }; |
96 | | - }; |
97 | | - |
98 | | - return ( |
| 87 | + const wallet = onboard?.connectedWallet() |
| 88 | + if (wallet) await onboard!.disconnectWallet({ label: wallet.label }) |
| 89 | + return signOut() |
| 90 | + } |
99 | 91 |
|
100 | 92 | return ( |
101 | 93 | <Context.Provider value={{ session, signedIn, logout, web3 }}> |
102 | 94 | {props.children} |
103 | 95 | </Context.Provider> |
104 | | - ); |
| 96 | + ) |
105 | 97 | } |
0 commit comments