Repository navigation
Expand file tree
/
Copy pathdocs_quickstart.html
More file actions
768 lines (584 loc) · 27.7 KB
/
Copy pathdocs_quickstart.html
File metadata and controls
768 lines (584 loc) · 27.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
<!-- Docs - Quickstart -->
<div class="section">
<h2><a href="/docs/">Docs</a> : Quickstart</h2>
<p>
In this tutorial we will build a very basic to-do web app. You can think of this tutorial as a demonstration of the core functionality of Userbase in the simplest way possible. We are going to focus solely on building a functional web app. Making things pretty is left as an exercise to the reader.
</p>
<p>The entire web app we'll be building will fit in a <a href="https://github.com/encrypted-dev/userbase-samples/blob/master/ugliest-todo/index.html" target="_blank">single static HTML file of 182 lines</a>. You can also see a <a href="https://ugliest-todo.netlify.com/" target="_blank">live demo</a> of the final result.</p>
<h3>Setting up</h3>
<p>Let's get going. Open a new file in your favorite code editor.</p>
<pre>
<code class="language-bash">
code ugly-todo.html
</code>
</pre>
<p>And add some boilerplate HTML.</p>
<div>
<pre>
<code class="language-markup">
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Ugliest To-Do</title>
</head>
<body>
<!-- application code -->
<script type="text/javascript">
</script>
</body>
</html>
</code>
</pre>
</div>
<p>
Now, open this file in a web browser of your choosing. At this point all you'll see is a blank page. As we add functionality throughout the tutorial, you can refresh this page to see the changes.
</p>
<a class="anchor" id="creating-admin-account"></a>
<h3>Creating an admin account</h3>
<p>To complete this tutorial, you'll need to <a href="https://v1.userbase.com/#create-admin">create a Userbase admin account</a>. Upon creation, a default application named "Starter App" will be created. Take note of the App ID because we'll need it very soon.</p>
<h3>Installing the SDK</h3>
<p>We're going to load the Userbase SDK from a CDN with a <code class="language-markup"><script></code> tag in the head of our page.</p>
<pre data-line="4">
<code class="language-markup"><!--
<head>
<meta charset="UTF-8">
<title>Ugliest To-Do App</title>
<script type="text/javascript" src="https://sdk.userbase.com/2/userbase.js"></script>
</head>
--></code>
</pre>
<p>The Userbase SDK will now be accessible via the <code class="language-javascript">userbase</code> variable. This will be our only dependency.</p>
<h3>Initializing the SDK</h3>
<p>
Before doing anything with the Userbase SDK, we need to let it know our App ID. Simply replace <code class="language-javascript">'YOUR_APP_ID'</code> with the App ID you received when you created your admin account.
</p>
<pre data-line="4">
<code class="language-markup">
<body>
<!-- application code -->
<script type="text/javascript">
userbase.init({ appId: 'YOUR_APP_ID' })
</script>
</body>
</html>
</code>
</pre>
<h3>Letting new users create an account</h3>
<p>Before our users can start creating to-dos, we need to give them a way to create an account with our app.</p>
<p>First, let's add a sign up form.</p>
<div>
<pre data-line="2-11">
<code class="language-markup">
<body>
<!-- Auth View -->
<div id="auth-view">
<h1>Create an account</h1>
<form id="signup-form">
<input id="signup-username" type="text" required placeholder="Username">
<input id="signup-password" type="password" required placeholder="Password">
<input type="submit" value="Create an account">
</form>
<div id="signup-error"></div>
</div>
<!-- application code -->
<script type="text/javascript"></script>
</body>
</code>
</pre>
</div>
<p>Then, let's add the code to handle the form submission.</p>
<div>
<pre data-line="5-16">
<code class="language-markup">
<!-- application code -->
<script type="text/javascript">
userbase.init({ appId: 'YOUR_APP_ID' })
function handleSignUp(e) {
e.preventDefault()
const username = document.getElementById('signup-username').value
const password = document.getElementById('signup-password').value
userbase.signUp({ username, password, rememberMe: 'none' })
.then((user) => alert('You signed up!'))
.catch((e) => document.getElementById('signup-error').innerHTML = e)
}
document.getElementById('signup-form').addEventListener('submit', handleSignUp)
</script>
</code>
</pre>
</div>
<p>
Now, whenever someone submits the form, the <code class="language-javascript">handleSignUp</code> function will be called. This gets the values of the username and password inputs and calls <code class="language-javascript">userbase.signUp({ username, password, rememberMe: 'none' })</code> to create a new user account with Userbase. We are specifying <code class="language-javascript">rememberMe: 'none'</code> because we won't implement automatic login until the end of the guide.
</p>
<p>
Go ahead and reload the page in your browser. Enter a username and password in the form and submit. You should get an alert saying that you signed up. And if you go to your Userbase admin account, you should also see the new user under your app.
</p>
<p>
Now try signing up for another account using the same username and you'll see an error message displayed under the form.
</p>
<p>
We'll come back to this function in a bit to make it do something more interesting.
</p>
<h3>Letting users log in</h3>
<p>Now that our users can create accounts, let's give them the ability to login.</p>
<p>First, let's add a "Login" form to the page above our "Create Account" form.</p>
<pre data-line="4-10">
<code class="language-markup">
<body>
<!-- Auth View -->
<div id="auth-view">
<h1>Login</h1>
<form id="login-form">
<input id="login-username" type="text" required placeholder="Username">
<input id="login-password" type="password" required placeholder="Password">
<input type="submit" value="Sign in">
</form>
<div id="login-error"></div>
<h1>Create an account</h1>
<form id="signup-form">
</code>
</pre>
<p>Then, let's add the code to handle the form submission.</p>
<pre data-line="5-14">
<code class="language-markup">
<!-- application code -->
<script type="text/javascript">
userbase.init({ appId: 'YOUR_APP_ID' })
function handleLogin(e) {
e.preventDefault()
const username = document.getElementById('login-username').value
const password = document.getElementById('login-password').value
userbase.signIn({ username, password, rememberMe: 'none' })
.then((user) => alert('You signed in!'))
.catch((e) => document.getElementById('login-error').innerHTML = e)
}
function handleSignUp(e) {
e.preventDefault()
…
</script>
</code>
</pre>
<p>And finally, let's bind our login form with our login handler.</p>
<pre data-line="9">
<code class="language-markup">
<!-- application code -->
<script type="text/javascript">
…
.catch((e) => document.getElementById('signup-error').innerHTML = e)
}
document.getElementById('login-form').addEventListener('submit', handleLogin)
document.getElementById('signup-form').addEventListener('submit', handleSignUp)
</script>
</body>
</code>
</pre>
<p>
You'll notice that this looks very similar to the sign up code from before. The <code class="language-javascript">handleLogin</code> function gets the values of the username and password inputs, and calls <code class="language-javascript">userbase.signIn({ username, password, rememberMe: 'none' })</code>. This will attempt to sign in the user, handling a success with an alert and a failure by displaying the error.
</p>
<p>
Reload the page and you should see our new login form. Enter the username and
password you used to create an account in the previous step, and submit the form.
You should get an alert saying that you signed in.
</p>
<p>
Try submitting the form again with incorrect credentials and you'll see an error
message displayed under the form.
</p>
<h3>Showing the to-do view</h3>
<p>
After a user signs in, we'll want to hide the authentication forms and display their to-do list. First, let's add a container for the to-do list under the authentication forms.</p>
<pre data-line="8-14">
<code class="language-markup">
<!-- Auth View -->
<div id="auth-view">
…
</div>
<!-- To-dos View -->
<div id="todo-view">
<div id="username"></div>
<h1>To-Do List</h1>
<div id="todos"></div>
</div>
<!-- application code -->
<script type="text/javascript">
userbase.init({ appId: 'YOUR_APP_ID' })
…
</script>
</code>
</pre>
<p>Then, let's make this view hidden by default, and add a function to display it.</p>
<div>
<pre data-line="9-13,18">
<code class="language-markup">
<!-- application code -->
<script type="text/javascript">
…
.catch((e) => document.getElementById('signup-error').innerHTML = e)
}
function showTodos(username) {
document.getElementById('auth-view').style.display = 'none'
document.getElementById('todo-view').style.display = 'block'
document.getElementById('username').innerHTML = username
}
document.getElementById('login-form').addEventListener('submit', handleLogin)
document.getElementById('signup-form').addEventListener('submit', handleSignUp)
document.getElementById('todo-view').style.display = 'none'
</script>
</code>
</pre>
</div>
<p>
Now that we have a function to show a view for signed in users, let's change
<code class="language-javascript">handleLogin</code> to call this function instead of showing an alert.
</p>
<pre data-line="8">
<code class="language-javascript">
function handleLogin(e) {
e.preventDefault()
const username = document.getElementById('login-username').value
const password = document.getElementById('login-password').value
userbase.signIn({ username, password, rememberMe: 'none' })
.then((user) => showTodos(user.username))
.catch((e) => document.getElementById('login-error').innerHTML = e)
}
</code>
</pre>
<p>And we do the same thing for <code class="language-javascript">handleSignUp</code>.</p>
<pre data-line="7">
<code class="language-javascript">
function handleSignUp(e) {
e.preventDefault()
const password = document.getElementById('signup-password').value
userbase.signUp({ username, password, rememberMe: 'none' })
.then((user) => showTodos(user.username))
.catch((e) => document.getElementById('signup-error').innerHTML = e)
}
</code>
</pre>
<p>
Reload the page and login again using your username and password. You should see the
authentication view disappear and your username show up along with the to-do list heading.
</p>
<h3>Using the database</h3>
<p>
Every time a user signs in, we need to establish a connection with the database that will hold the user's to-dos.
</p>
<p>
First, let's add a couple of elements for showing a loading indicator and error messages.
</p>
<pre data-line="6-7">
<code class="language-markup"><!--
<div id="todo-view">
<span id="username"></span>
<h1>To-Do List</h1>
<div id="todos"></div>
<div id="db-loading">Loading to-dos...</div>
<div id="db-error"></div>
</div>
--></code>
</pre>
<p>Then, let's change <code class="language-javascript">showTodos</code> to open a new database connection.</p>
<pre data-line="5,7-12,15-25">
<code class="language-javascript">
function showTodos(username) {
document.getElementById('auth-view').style.display = 'none'
document.getElementById('todo-view').style.display = 'block'
// reset the todos view
document.getElementById('username').innerHTML = username
document.getElementById('todos').innerText = ''
document.getElementById('db-loading').style.display = 'block'
document.getElementById('db-error').innerText = ''
userbase.openDatabase({ databaseName: 'todos', changeHandler })
.catch((e) => document.getElementById('db-error').innerText = e)
}
function changeHandler(items) {
document.getElementById('db-loading').style.display = 'none'
const todosList = document.getElementById('todos')
if (items.length === 0) {
todosList.innerText = 'Empty'
} else {
// render to-dos, not yet implemented
}
}
document.getElementById('login-form').addEventListener('submit', handleLogin)
document.getElementById('signup-form').addEventListener('submit', handleSignUp)
</code>
</pre>
<p>
We changed <code class="language-javascript">showTodos</code> to make a call to <code class="language-javascript">userbase.openDatabase({ databaseName: 'todos', changeHandler })</code>. After the <code class="language-javascript">'todos'</code>
database is opened, our callback
function <code class="language-javascript">changeHandler</code> will be called whenever data changes in the database. When the Promise is resolved, the database is ready for use and we hide the loading indicator.
</p>
<p>
Reload the page and sign in again. You'll see "Loading to-dos..." while the connection
to the database is getting established, followed by "Empty", indicating there are
currently no to-dos in the database.
</p>
<h3>Showing the to-dos</h3>
<p>
If the database has items in it, we'll want to render those under our to-do list.
Let's implement that in <code class="language-javascript">changeHandler</code>.
</p>
<pre data-line="9-24">
<code class="language-javascript"><!--
function changeHandler(items) {
document.getElementById('db-loading').style.display = 'none'
const todosList = document.getElementById('todos')
if (items.length === 0) {
todosList.innerText = 'Empty'
} else {
// clear the list
todosList.innerHTML = ''
// render all the to-do items
for (let i = 0; i < items.length; i++) {
// build the todo label
const todoLabel = document.createElement('label')
todoLabel.innerHTML = items[i].item.todo
todoLabel.style.textDecoration = items[i].item.complete ? 'line-through' : 'none'
// append the todo item to the list
const todoItem = document.createElement('div')
todoItem.appendChild(todoLabel)
todosList.appendChild(todoItem)
}
}
}
--></code>
</pre>
<h3>Adding to-dos</h3>
<p>Now, let's add a form for creating new to-dos.</p>
<script type="text/plain" data-line="10-14" class="language-markup">
<!-- To-dos View -->
<div id="todo-view">
<span id="username"></span>
<h1>To-Do List</h1>
<div id="todos"></div>
<div id="db-loading">Loading to-dos...</div>
<div id="db-error"></div>
<form id="add-todo-form">
<input id="add-todo" type="text" required placeholder="To-Do">
<input type="submit" value="Add">
</form>
<div id="add-todo-error"></div>
</div>
</script>
<p>Then, let's add the code to handle the form submission.</p>
<pre data-line="5-13,17,20">
<code class="language-markup">
<!-- application code -->
<script type="text/javascript">
…
function addTodoHandler(e) {
e.preventDefault()
const todo = document.getElementById('add-todo').value
userbase.insertItem({ databaseName: 'todos', item: { 'todo': todo }})
.then(() => document.getElementById('add-todo').value = '')
.catch((e) => document.getElementById('add-todo-error').innerHTML = e)
}
document.getElementById('login-form').addEventListener('submit', handleLogin)
document.getElementById('signup-form').addEventListener('submit', handleSignUp)
document.getElementById('add-todo-form').addEventListener('submit', addTodoHandler)
document.getElementById('todo-view').style.display = 'none'
</script>
</code>
</pre>
<p>
In <code class="language-javascript">addTodoHandler</code> we get the to-do text from the input, and then call <code class="language-javascript">userbase.insertItem</code>
with the database name and the object we want the persist. This
will return a Promise that resolves when the data is successfully persisted to
the database.
</p>
<p>
Reload the page and add some to-dos. Then, reload the page again and the to-dos should automatically appear after you login. These to-dos have been successfully persisted in the end-to-end encrypted Userbase database.
</p>
<h3>Updating to-dos</h3>
<p>Now, let's add a checkbox to allow to-dos to be marked as completed.</p>
<pre data-line="4-16,26">
<code class="language-javascript">
// render all the to-do items
for (let i = 0; i < items.length; i++) {
// build the todo checkbox
const todoBox = document.createElement('input')
todoBox.type = 'checkbox'
todoBox.id = items[i].itemId
todoBox.checked = items[i].item.complete ? true : false
todoBox.onclick = (e) => {
e.preventDefault()
userbase.updateItem({ databaseName: 'todos', itemId: items[i].itemId, item: {
'todo': items[i].item.todo,
'complete': !items[i].item.complete
}})
.catch((e) => document.getElementById('add-todo-error').innerHTML = e)
}
// build the todo label
const todoLabel = document.createElement('label')
todoLabel.innerHTML = items[i].item.todo
…
// append the todo item to the list
const todoItem = document.createElement('div')
todoItem.appendChild(todoBox)
todoItem.appendChild(todoLabel)
todosList.appendChild(todoItem)
}
</code>
</pre>
<p>
Reload the page and complete some to-dos. Their state should persist even after you reload the page and login again.
</p>
<h3>Deleting to-dos</h3>
<p>And finally, let's create a button for deleting a to-do.</p>
<pre data-line="4-11">
<code class="language-javascript">
// render all the to-do items
for (let i = 0; i < items.length; i++) {
// build the todo delete button
const todoDelete = document.createElement('button')
todoDelete.innerHTML = 'X'
todoDelete.style.display = 'inline-block'
todoDelete.onclick = () => {
userbase.deleteItem({ databaseName: 'todos', itemId: items[i].itemId })
.catch((e) => document.getElementById('add-todo-error').innerHTML = e)
}
// build the todo checkbox
const todoBox = document.createElement('input')
todoBox.type = 'checkbox'
</code>
</pre>
<p>And now let's append the delete button to the to-do element.</p>
<pre data-line="3">
<code class="language-javascript">
// append the todo item to the list
const todoItem = document.createElement('div')
todoItem.appendChild(todoDelete)
todoItem.appendChild(todoBox)
todoItem.appendChild(todoLabel)
todosList.appendChild(todoItem)
</code>
</pre>
<p>
Reload the page and delete some to-dos. They should no longer show up even after you reload the page and login again.
</p>
<h3>Polishing up</h3>
<p>Before we wrap up, let's add two final pieces of account functionality: user logout and automatic login for returning users.</p>
<h3>Signing out users</h3>
<p>First, let's add a logout button along with a container for error messages.</p>
<script type="text/plain" data-line="4-5" class="language-markup">
<!-- To-dos View -->
<div id="todo-view">
<span id="username"></span>
<input type="button" value="Logout" id="logout-button">
<div id="logout-error"></div>
<h1>To-Do List</h1>
<div id="todos"></div>
</script>
<p>Then, let's add the code to handle the logout.</p>
<pre data-line="9-13,21-30,40">
<code class="language-markup">
<!-- application code -->
<script type="text/javascript">
…
.catch((e) => document.getElementById('signup-error').innerHTML = e)
}
function handleLogout() {
userbase.signOut()
.then(() => showAuth())
.catch((e) => document.getElementById('logout-error').innerText = e)
}
function showTodos(username) {
document.getElementById('auth-view').style.display = 'none'
document.getElementById('todo-view').style.display = 'block'
…
function showAuth() {
document.getElementById('todo-view').style.display = 'none'
document.getElementById('auth-view').style.display = 'block'
document.getElementById('login-username').value = ''
document.getElementById('login-password').value = ''
document.getElementById('login-error').innerText = ''
document.getElementById('signup-username').value = ''
document.getElementById('signup-password').value = ''
document.getElementById('signup-error').innerText = ''
}
function changeHandler(items) {
const todosList = document.getElementById('todos')
…
document.getElementById('login-form').addEventListener('submit', handleLogin)
document.getElementById('signup-form').addEventListener('submit', handleSignUp)
document.getElementById('add-todo-form').addEventListener('submit', addTodoHandler)
document.getElementById('logout-button').addEventListener('click', handleLogout)
document.getElementById('todo-view').style.display = 'none'
</script>
</code>
</pre>
<p>The <code class="language-javascript">handleLogout</code> function calls <code class="language-javascript">userbase.signOut</code> which sends a request to end the user's session. A Promise is returned that resolves when the user is signed out, in which case we hide the to-do view and show the authentication view.</p>
<h3>Automatically resuming a session</h3>
<p>Let's modify our app to automatically sign in a returning user when the page loads. First, we'll add a loading indicator that will show while the app is trying to automatically sign in the user.</p>
<pre data-line="4-5">
<code class="language-markup">
</head>
<body>
<!-- Loading View -->
<div id="loading-view">Loading...</div>
<!-- Auth View -->
<div id="auth-view">
<h1>Login</h1>
</code>
</pre>
<p>Then, let's add the following to our <code class="language-javascript">userbase.init</code> call.</p>
<pre data-line="5-7, 17">
<code class="language-markup">
<!-- application code -->
<script type="text/javascript">
userbase.init({ appId: 'YOUR_APP_ID' })
.then((session) => session.user ? showTodos(session.user.username) : showAuth())
.catch(() => showAuth())
.finally(() => document.getElementById('loading-view').style.display = 'none')
…
document.getElementById('login-form').addEventListener('submit', handleLogin)
document.getElementById('signup-form').addEventListener('submit', handleSignUp)
document.getElementById('add-todo-form').addEventListener('submit', addTodoHandler)
document.getElementById('logout-button').addEventListener('click', handleLogout)
document.getElementById('todo-view').style.display = 'none'
document.getElementById('auth-view').style.display = 'none'
</script>
</code>
</pre>
<p>
We are now hiding the authentication view by default so that it will only show if an existing session can't be resumed.
</p>
<p>
The <code class="language-javascript">userbase.init</code> function returns a Promise that resolves when the SDK has determined if it can reuse the previous session. If so, the user gets automatically logged in, and the <code class="language-javascript">session.user</code> object gets set. If there was no previous session, or the session cannot be resumed, the <code class="language-javascript">session.user</code> object will not be set. If <code class="language-javascript">userbase.init</code> fails, we'll just send the user to the sign in page regardless of the reason.
</p>
<p>
Next, we want to tell the SDK to persist the session even after we refresh the page or close the browser's window. To do that, we need to set the <code class="language-javascript">rememberMe</code> parameter to either <code class="language-javascript">'local'</code> (persists after the window gets closed) or <code class="language-javascript">'session'</code> (persists only when the page gets refreshed) when calling <code class="language-javascript">userbase.signIn</code> and <code class="language-javascript">userbase.signUp</code>. Let's set ours to <code class="language-javascript">'local'</code>.
</p>
<pre data-line="11,22">
<code class="language-markup">
<!-- application code -->
<script type="text/javascript">
userbase.init({ appId: 'YOUR_APP_ID' })
function handleLogin(e) {
e.preventDefault()
const username = document.getElementById('login-username').value
const password = document.getElementById('login-password').value
userbase.signIn({ username, password, rememberMe: 'local' })
.then((user) => showTodos(user.username))
.catch((e) => document.getElementById('login-error').innerHTML = e)
}
function handleSignUp(e) {
e.preventDefault()
const username = document.getElementById('signup-username').value
const password = document.getElementById('signup-password').value
userbase.signUp({ username, password, rememberMe: 'local' })
.then((user) => showTodos(user.username))
.catch((e) => document.getElementById('signup-error').innerHTML = e)
}
…
</script>
</code>
</pre>
<p>
Now, if we sign in and close the page, we will get signed in automatically next time we open it, without having to re-enter our username and password.
</p>
<h3>What's next?</h3>
<p>And that was it! A fully working (but ugly) web app in just 182 lines of code, including markup and comments. If you have any questions, or there's anything we can do to help you with your web app, please <a href="/contact/">get in touch</a>. Thank you!</p>
</div>
<hr>