|
1 | | -import { Component, createSignal, For, Show } from "solid-js"; |
| 1 | +import { Component, createSignal, onMount, For, Show } from "solid-js"; |
2 | 2 | import { createDbStore, supabaseAdapter, DbRow, DbStoreError } from "../src/index.js"; |
3 | | -import { createClient, SupabaseClient } from "@supabase/supabase-js"; |
| 3 | +import { AuthResponse, createClient, Session, SupabaseClient } from "@supabase/supabase-js"; |
4 | 4 | import { reconcile } from "solid-js/store"; |
5 | 5 |
|
6 | | -const TodoList = (props: { client: SupabaseClient }) => { |
| 6 | +const TodoList = (props: { client: SupabaseClient, logout: () => void }) => { |
7 | 7 | const [error, setError] = createSignal<DbStoreError<DbRow>>(); |
| 8 | + (globalThis as any).supabaseClient = props.client; |
8 | 9 | const [todos, setTodos] = createDbStore({ |
9 | 10 | adapter: supabaseAdapter({ client: props.client, table: "todos" }), |
| 11 | + defaultFields: ['id', 'user_id'], |
10 | 12 | onError: setError, |
11 | 13 | }); |
12 | 14 | const [edit, setEdit] = createSignal<DbRow>(); |
13 | 15 | const done = (task: DbRow) => setTodos(reconcile(todos.filter(todo => todo !== task))); |
14 | 16 | const add = (task: string) => setTodos(todos.length, { task }); |
15 | 17 | return ( |
16 | | - <> |
| 18 | + <div> |
| 19 | + <button onclick={props.logout}>logout</button> |
17 | 20 | <Show when={error()} keyed> |
18 | 21 | {err => ( |
19 | 22 | <p class="text-red-600"> |
@@ -70,85 +73,74 @@ const TodoList = (props: { client: SupabaseClient }) => { |
70 | 73 | </button> |
71 | 74 | </li> |
72 | 75 | </ul> |
73 | | - </> |
| 76 | + </div> |
74 | 77 | ); |
75 | 78 | }; |
76 | 79 |
|
77 | 80 | const App: Component = () => { |
78 | | - const [client, setClient] = createSignal<SupabaseClient<any, "public", any>>(); |
79 | | - const connect = () => { |
80 | | - const url = (document.querySelector('[type="url"]') as HTMLInputElement | null)?.value; |
81 | | - const key = (document.querySelector('[type="password"]') as HTMLInputElement | null)?.value; |
82 | | - url && key && setClient(createClient(url, key)); |
| 81 | + // these are public keys that will end up in the client in any case: |
| 82 | + const client = |
| 83 | + createClient( |
| 84 | + import.meta.env.VITE_SUPABASE_URL, |
| 85 | + import.meta.env.VITE_SUPABASE_KEY |
| 86 | + ); |
| 87 | + const [session, setSession] = createSignal<Session>(); |
| 88 | + const [error, setError] = createSignal(''); |
| 89 | + const handleAuthPromise = ({ error, data }: AuthResponse) => { |
| 90 | + if (error) { |
| 91 | + setError(error.toString()) |
| 92 | + } else { |
| 93 | + setSession(data.session ?? undefined); |
| 94 | + } |
| 95 | + }; |
| 96 | + onMount(() => client.auth.refreshSession().then(handleAuthPromise)); |
| 97 | + const login = () => { |
| 98 | + const email = (document.querySelector('[type="email"]') as HTMLInputElement | null)?.value; |
| 99 | + const password = (document.querySelector('[type="password"]') as HTMLInputElement | null)?.value; |
| 100 | + if (!email || !password) { |
| 101 | + setError('please provide an email and password'); |
| 102 | + return; |
| 103 | + } |
| 104 | + client.auth.signInWithPassword({ email, password }).then(handleAuthPromise); |
83 | 105 | }; |
| 106 | + const register = () => { |
| 107 | + const email = (document.querySelector('[type="email"]') as HTMLInputElement | null)?.value; |
| 108 | + const password = (document.querySelector('[type="password"]') as HTMLInputElement | null)?.value; |
| 109 | + if (!email || !password) { |
| 110 | + setError('please provide an email and password'); |
| 111 | + return; |
| 112 | + } |
| 113 | + client.auth.signUp({ email, password }).then(handleAuthPromise); |
| 114 | + } |
84 | 115 |
|
85 | 116 | return ( |
86 | 117 | <div class="box-border flex min-h-screen w-full flex-col items-center justify-center space-y-4 bg-gray-800 p-24 text-white"> |
87 | 118 | <div class="wrapper-v"> |
88 | 119 | <h4>db-store-backed to-do list</h4> |
89 | 120 | <Show |
90 | | - when={client()} |
91 | | - keyed |
| 121 | + when={session()} |
92 | 122 | fallback={ |
93 | 123 | <> |
94 | | - <details> |
95 | | - <summary>To configure your own database,</summary> |
96 | | - <ul class="list-disc"> |
97 | | - <li> |
98 | | - Register with <a href="https://supabase.com">Supabase</a>. |
99 | | - </li> |
100 | | - <li> |
101 | | - Create a new database and note down the url and the key (that usually go into an |
102 | | - environment) |
103 | | - </li> |
104 | | - <li> |
105 | | - Within the database, create a table and configure it to be public, promote |
106 | | - changes in realtime and has no row protection: |
107 | | - <pre> |
108 | | - <code>{`-- Create table |
109 | | -create table todos ( |
110 | | - id serial primary key, |
111 | | - task text |
112 | | -); |
113 | | --- Turn off row-level security |
114 | | -alter table "todos" |
115 | | -disable row level security; |
116 | | --- Allow anonymous access |
117 | | -create policy "Allow anonymous access" |
118 | | -on todos |
119 | | -for select |
120 | | -to anon |
121 | | -using (true);`}</code> |
122 | | - </pre> |
123 | | - </li> |
124 | | - <li>Fill in the url and key in the fields below and press "connect".</li> |
125 | | - </ul> |
126 | | - </details> |
127 | | - <p> |
128 | | - <label> |
129 | | - DB |
130 | | - <select> |
131 | | - <option value="supabase">SupaBase</option> |
132 | | - </select> |
133 | | - </label> |
134 | | - </p> |
| 124 | + <Show when={error()}><p>{error()}</p></Show> |
135 | 125 | <p> |
136 | 126 | <label> |
137 | | - Client URL <input type="url" /> |
| 127 | + Email <input type="email" onInput={() => setError('')} /> |
138 | 128 | </label> |
139 | 129 | </p> |
140 | 130 | <p> |
141 | 131 | <label> |
142 | | - Client Key <input type="password" /> |
| 132 | + Password <input type="password" /> |
143 | 133 | </label> |
144 | 134 | </p> |
145 | | - <button class="btn" onClick={connect}> |
146 | | - Connect |
147 | | - </button> |
| 135 | + <button class="btn" onClick={login}>sign in</button> |
| 136 | + <button class="btn" onClick={register}>sign up</button> |
148 | 137 | </> |
149 | 138 | } |
150 | 139 | > |
151 | | - {(client: SupabaseClient) => <TodoList client={client} />} |
| 140 | + <TodoList |
| 141 | + client={client} |
| 142 | + logout={() => { setSession(undefined); client.auth.signOut(); }} |
| 143 | + /> |
152 | 144 | </Show> |
153 | 145 | </div> |
154 | 146 | </div> |
|
0 commit comments