create account

Coding Journal #4: Local Storage, JSON Web Tokens & Wallaby.js by jeffbernst

View this thread on: hive.blogpeakd.comecency.com
· @jeffbernst ·
$25.91
Coding Journal #4: Local Storage, JSON Web Tokens & Wallaby.js
![journal-4.855e0ff95cc74fe9891c90d1a910f162.png](https://steemitimages.com/DQmZTGcZuGREwv5fPdrGoBKkpHCsDrHrmjRCz4sFtPCj8yw/journal-4.855e0ff95cc74fe9891c90d1a910f162.png)
<center>_[Emoji Source](https://github.com/twitter/twemoji)_</center>

This week was spent mostly on building out the front end for my Thinkful Node.js project. This is definitely the most complicated project I’ve ever built, so progress has been slow, but it’s starting to really take shape. 😀

![site-update.f4571e3f30a44413854983739043ad89.gif](https://steemitimages.com/DQmZk67YQwUrooxSmUVcAbGypni1US9uyUKq4p92qdbaihM/site-update.f4571e3f30a44413854983739043ad89.gif)

## Local Storage
---- 
If a new user visits my site, they’ll be greeted with a different home page than someone who is logged in. Here’s the welcome message for a new user:

![Screen Shot 2018-02-16 at 1.20.31 PM.2148a94986ac4356b607ca93e02d3a08.png](https://steemitimages.com/DQmNsNeUiwigBJLg1sS1J1gKnaScxdrznVwGVtsvptDajEf/Screen%20Shot%202018-02-16%20at%201.20.31%20PM.2148a94986ac4356b607ca93e02d3a08.png)

And a logged in user sees the courses they’re enrolled in, their Gravatar image, and a create button:

![Screen Shot 2018-02-16 at 1.21.09 PM.50f9a95686a04c6bbcddc13e8f3f8f23.png](https://steemitimages.com/DQmPj9JPmdFjQSkB5ivwvN5qnkqfGBYDV7DVaG3ozYUEf2T/Screen%20Shot%202018-02-16%20at%201.21.09%20PM.50f9a95686a04c6bbcddc13e8f3f8f23.png)

To test out this functionality, I’m using a mock JSON web token (more about that later) stored in local storage, which is a collection of data that’s saved across browser sessions. I’m not sure if this is the exact mechanism that most websites use, but from what I understand, I’ll save a token in the user’s browser when they log in, which makes it so they don’t have to log in every time they visit my site.

It’s actually much easier to manipulate local storage data than I would have expected. Here’s an example from the [MDN web docs](https://developer.mozilla.org/en-US/docs/Web/API/Window/localStorage):

	localStorage.setItem('myCat', 'Tom');
	
	var cat = localStorage.getItem("myCat");
	
	localStorage.removeItem("myCat");
&nbsp;
You can see the items stored in local storage in the Application section of Chrome dev tools:

![Screen Shot 2018-02-16 at 1.24.29 PM.33a3d42dd0894ad18ecb54f23063964c.png](https://steemitimages.com/DQmemxrww2j44Jb5Xhit6sU2UBHmua2bNyjYXxA8UFTjAjR/Screen%20Shot%202018-02-16%20at%201.24.29%20PM.33a3d42dd0894ad18ecb54f23063964c.png)

I also have some mock course and user data to help me simulate what it will be like when my database is up and running.

## JSON Web Tokens
---- 
JSON web tokens (JWT) are strings used to identify a user and grant them access to something behind a password wall. I’m going to walk through my basic understanding of how this all works. Please let me know if I get any of this wrong! ([Here](https://medium.com/vandium-software/5-easy-steps-to-understanding-json-web-tokens-jwt-1164c0adfcec)’s a good medium post on the subject.)

Here’s an example of what a JWT looks like: 

	eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiYWRtaW4iOnRydWV9.TJVA95OrM7E2cBab30RMHrHDcEfxjoYZgeFONFh7HgQ
&nbsp;
It’s three collections of numbers and letters with dots in between them in this format:

	header.payload.signature
&nbsp;
To decode the JWT, you can paste it into [this tool](https://jwt.io/), which shows the following:

![Screen Shot 2018-02-16 at 1.35.32 PM.3cfdb41e18ca486f9920cd9a8a3235b6.png](https://steemitimages.com/DQmRQxShcQVosjPDrmLT8465CpXRrxqyGLjtdoyqcaB1bW5/Screen%20Shot%202018-02-16%20at%201.35.32%20PM.3cfdb41e18ca486f9920cd9a8a3235b6.png)

The JWT is color-coded in the box to show which part of the string corresponds to which data. The string header contains that it’s a JWT encoded with the HS256 algorithm, the data (payload) gets stored in the middle string shown in purple, and the blue section is the signature, which is used to verify that the JWT is valid. The word “secret” in the box at the bottom is the secret key.

So for something like user registration, the user will fill out a form on my site with their username and password. I hash the password (convert the string to an unguessable set of characters) and store it on my database with the rest of the user info.

Once the user is in my system, they can submit their information on the log in form, and in response they’ll get a JWT stashed in the browser. Now when they try to access something that requires authentication, the JWT can be used to show they have access.

This stuff is a bit complicated, but if I understand correctly, the header and payload are encoded using something called base64URL and attached with a dot in between them. Then both of these are hashed with the secret key using the algorithm specified in the header (HS256) and attached to the encoded header and payload with another dot. When this whole JSON web token is sent to the server for authentication, it uses the secret key in its possession to confirm that the signature matches the data. Pretty cool! 

## Wallaby.js
---- 
I’ll probably talk more about this tool in later posts, but I just wanted to introduce it here because I’m setting it up now as I get ready to set up my server. [Wallaby.js](https://wallabyjs.com/) works with your tests to show errors live in your editor:

<center>![jb1.26026728f4d4435b9816a021485d951b.gif](https://steemitimages.com/DQmU1BSdnFAeDe26xwzcRoarhQ6ncwpPds8FtedqqUTx4CM/jb1.26026728f4d4435b9816a021485d951b.gif)</center>
<center>_[Image Source](https://wallabyjs.com/)_</center>

They also have a free tool called [Quokka.js](https://quokkajs.com/) that I want to play around with some more. Here’s what it looks like in action:

<center>![vsc1.44e6af438922479599f274d2010ce002.gif](https://steemitimages.com/DQmf1nqthUhXApRL1bBgMk2fpa4GNm18W7Mcni2ezFbRMpo/vsc1.44e6af438922479599f274d2010ce002.gif)</center>
<center>_[Image Source](https://quokkajs.com/)_</center>

I’ve still got a lot of reviewing to do as I implement my unit/integration tests on my project, but I think Wallaby will make it easier to write my back-end code properly.

## Almost Done!
---- 
I’m getting much closer to being done with my project! Wrestling with the front code is starting to get a little monotonous, so I’m excited to take a crack at setting up my server/database.

Thanks for reading! If you have any suggestions or explanations for any of these things that I’m learning, I’d love to hear about them in the comments! 🙂

---- 
<center>[![follow_jeffbernst.gif](https://steemitimages.com/DQmXGhxrjSU8mJHaRFmq5rKc67YDutbCHLTGHHRRTN4hwwi/follow_jeffbernst.gif)](https://steemit.com/@jeffbernst)</center>
👍  , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , ,
properties (23)
authorjeffbernst
permlinkcoding-journal-4-local-storage-json-web-tokens-and-wallaby-js
categorydev
json_metadata{"tags":["dev","steemdev","life","javascript","sndbox"],"image":["https://steemitimages.com/DQmZTGcZuGREwv5fPdrGoBKkpHCsDrHrmjRCz4sFtPCj8yw/journal-4.855e0ff95cc74fe9891c90d1a910f162.png","https://steemitimages.com/DQmZk67YQwUrooxSmUVcAbGypni1US9uyUKq4p92qdbaihM/site-update.f4571e3f30a44413854983739043ad89.gif","https://steemitimages.com/DQmNsNeUiwigBJLg1sS1J1gKnaScxdrznVwGVtsvptDajEf/Screen%20Shot%202018-02-16%20at%201.20.31%20PM.2148a94986ac4356b607ca93e02d3a08.png","https://steemitimages.com/DQmPj9JPmdFjQSkB5ivwvN5qnkqfGBYDV7DVaG3ozYUEf2T/Screen%20Shot%202018-02-16%20at%201.21.09%20PM.50f9a95686a04c6bbcddc13e8f3f8f23.png","https://steemitimages.com/DQmemxrww2j44Jb5Xhit6sU2UBHmua2bNyjYXxA8UFTjAjR/Screen%20Shot%202018-02-16%20at%201.24.29%20PM.33a3d42dd0894ad18ecb54f23063964c.png","https://steemitimages.com/DQmRQxShcQVosjPDrmLT8465CpXRrxqyGLjtdoyqcaB1bW5/Screen%20Shot%202018-02-16%20at%201.35.32%20PM.3cfdb41e18ca486f9920cd9a8a3235b6.png","https://steemitimages.com/DQmU1BSdnFAeDe26xwzcRoarhQ6ncwpPds8FtedqqUTx4CM/jb1.26026728f4d4435b9816a021485d951b.gif","https://steemitimages.com/DQmf1nqthUhXApRL1bBgMk2fpa4GNm18W7Mcni2ezFbRMpo/vsc1.44e6af438922479599f274d2010ce002.gif","https://steemitimages.com/DQmXGhxrjSU8mJHaRFmq5rKc67YDutbCHLTGHHRRTN4hwwi/follow_jeffbernst.gif"],"links":["https://github.com/twitter/twemoji","https://developer.mozilla.org/en-US/docs/Web/API/Window/localStorage","https://medium.com/vandium-software/5-easy-steps-to-understanding-json-web-tokens-jwt-1164c0adfcec","https://jwt.io/","https://wallabyjs.com/","https://quokkajs.com/","https://steemit.com/@jeffbernst"],"app":"steemit/0.1","format":"markdown"}
created2018-02-16 19:31:48
last_update2018-02-16 19:31:48
depth0
children10
last_payout2018-02-23 19:31:48
cashout_time1969-12-31 23:59:59
total_payout_value19.858 HBD
curator_payout_value6.052 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length6,696
author_reputation13,041,257,634,850
root_title"Coding Journal #4: Local Storage, JSON Web Tokens & Wallaby.js"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id38,077,699
net_rshares4,396,629,581,755
author_curate_reward""
vote details (45)
@chuqui ·
$0.06
Excellent post! It's always easy to read and understand your posts!
👍  
properties (23)
authorchuqui
permlinkre-jeffbernst-coding-journal-4-local-storage-json-web-tokens-and-wallaby-js-20180217t112119278z
categorydev
json_metadata{"tags":["dev"],"app":"steemit/0.1"}
created2018-02-17 11:19:39
last_update2018-02-17 11:19:39
depth1
children1
last_payout2018-02-24 11:19:39
cashout_time1969-12-31 23:59:59
total_payout_value0.044 HBD
curator_payout_value0.013 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length67
author_reputation1,087,526,639
root_title"Coding Journal #4: Local Storage, JSON Web Tokens & Wallaby.js"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id38,234,402
net_rshares10,475,253,476
author_curate_reward""
vote details (1)
@jeffbernst ·
Thanks for reading! 🙂
properties (22)
authorjeffbernst
permlinkre-chuqui-re-jeffbernst-coding-journal-4-local-storage-json-web-tokens-and-wallaby-js-20180219t002630127z
categorydev
json_metadata{"tags":["dev"],"app":"steemit/0.1"}
created2018-02-19 00:26:24
last_update2018-02-19 00:26:24
depth2
children0
last_payout2018-02-26 00:26:24
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length21
author_reputation13,041,257,634,850
root_title"Coding Journal #4: Local Storage, JSON Web Tokens & Wallaby.js"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id38,644,452
net_rshares0
@m0rc ·
$0.04
It looks like you used wallaby I'm webstorm and quokka in vscode. Do they have the same support for both? Which one has better integration?
👍  
properties (23)
authorm0rc
permlinkre-jeffbernst-2018216t21547101z
categorydev
json_metadata{"tags":["dev","steemdev","life","javascript","sndbox"],"app":"esteem/1.5.1","format":"markdown+html","community":"esteem"}
created2018-02-16 20:54:06
last_update2018-02-16 20:54:06
depth1
children1
last_payout2018-02-23 20:54:06
cashout_time1969-12-31 23:59:59
total_payout_value0.031 HBD
curator_payout_value0.010 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length139
author_reputation131,675,406
root_title"Coding Journal #4: Local Storage, JSON Web Tokens & Wallaby.js"
beneficiaries
0.
accountesteemapp
weight1,000
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id38,091,945
net_rshares8,339,198,968
author_curate_reward""
vote details (1)
@jeffbernst ·
Actually those GIFs are from their site! But Webstorm and VS Code both work with Wallaby and Quokka. I'm not sure if one has better integration though -- as far as I can tell the full feature set is available in all of the editors they support.

Thanks for reading!
properties (22)
authorjeffbernst
permlinkre-m0rc-re-jeffbernst-2018216t21547101z-20180217t000336514z
categorydev
json_metadata{"tags":["dev"],"app":"steemit/0.1"}
created2018-02-17 00:03:36
last_update2018-02-17 00:03:36
depth2
children0
last_payout2018-02-24 00:03:36
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length265
author_reputation13,041,257,634,850
root_title"Coding Journal #4: Local Storage, JSON Web Tokens & Wallaby.js"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id38,121,201
net_rshares0
@mslifesteem ·
$0.04
You're doing great Jeff, keep going on your project and move forward to end result.

Looking forward to see more of your updates.
👍  
properties (23)
authormslifesteem
permlinkre-jeffbernst-coding-journal-4-local-storage-json-web-tokens-and-wallaby-js-20180216t104830260z
categorydev
json_metadata{"tags":["dev"],"app":"steemit/0.1"}
created2018-02-16 21:48:42
last_update2018-02-16 21:48:42
depth1
children1
last_payout2018-02-23 21:48:42
cashout_time1969-12-31 23:59:59
total_payout_value0.034 HBD
curator_payout_value0.010 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length129
author_reputation1,658,185,046,990
root_title"Coding Journal #4: Local Storage, JSON Web Tokens & Wallaby.js"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id38,100,581
net_rshares8,205,771,784
author_curate_reward""
vote details (1)
@jeffbernst ·
Thanks @mslifesteem! 🙂
👍  
properties (23)
authorjeffbernst
permlinkre-mslifesteem-re-jeffbernst-coding-journal-4-local-storage-json-web-tokens-and-wallaby-js-20180217t000430248z
categorydev
json_metadata{"tags":["dev"],"users":["mslifesteem"],"app":"steemit/0.1"}
created2018-02-17 00:04:30
last_update2018-02-17 00:04:30
depth2
children0
last_payout2018-02-24 00:04:30
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length22
author_reputation13,041,257,634,850
root_title"Coding Journal #4: Local Storage, JSON Web Tokens & Wallaby.js"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id38,121,331
net_rshares592,785,255
author_curate_reward""
vote details (1)
@sambillingham ·
$0.05
It's great reading your updates Jeff. Can't wait to see this in action. 

p.s I think you could totally submit these updates through Utopian if you're planning to have this as an open source project with an OS licence.
👍  
properties (23)
authorsambillingham
permlinkre-jeffbernst-coding-journal-4-local-storage-json-web-tokens-and-wallaby-js-20180216t203406053z
categorydev
json_metadata{"tags":["dev"],"app":"steemit/0.1"}
created2018-02-16 20:34:06
last_update2018-02-16 20:34:06
depth1
children3
last_payout2018-02-23 20:34:06
cashout_time1969-12-31 23:59:59
total_payout_value0.038 HBD
curator_payout_value0.010 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length218
author_reputation34,876,406,478,004
root_title"Coding Journal #4: Local Storage, JSON Web Tokens & Wallaby.js"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id38,088,548
net_rshares8,872,880,512
author_curate_reward""
vote details (1)
@jeffbernst ·
Oh awesome, I didn't even think about that. Like maybe in the blog post category? This will definitely be an open source project with an OS license.

Thanks for the tip!
properties (22)
authorjeffbernst
permlinkre-sambillingham-re-jeffbernst-coding-journal-4-local-storage-json-web-tokens-and-wallaby-js-20180216t234323953z
categorydev
json_metadata{"tags":["dev"],"app":"steemit/0.1"}
created2018-02-16 23:43:24
last_update2018-02-16 23:43:24
depth2
children2
last_payout2018-02-23 23:43:24
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length169
author_reputation13,041,257,634,850
root_title"Coding Journal #4: Local Storage, JSON Web Tokens & Wallaby.js"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id38,118,245
net_rshares0
@sambillingham ·
$0.05
From my perspective, this is *almost* perfect for the development category. You are developing an OS project and writing about the updates. That's exactly what Utopian is for. The only changes I would suggest are to start creating a branch for each weekly update, then submit a PR highlighting the changes you made (not strictly necessary to make a PR per the rules but would help the moderator). You might have to shift your writing a little more towards release vs journal which is something only you can decide if you want to do, would vary on who the mod was though.

Personally, I feel like the blog post category is more aimed towards writing about larger mainstream OS projects. Reviews (but not tutorials), benchmarks, research or statistics. Just my take, it's a little difficult to know, to be honest.
👍  
properties (23)
authorsambillingham
permlinkre-jeffbernst-re-sambillingham-re-jeffbernst-coding-journal-4-local-storage-json-web-tokens-and-wallaby-js-20180217t005316072z
categorydev
json_metadata{"tags":["dev"],"app":"steemit/0.1"}
created2018-02-17 00:53:15
last_update2018-02-17 00:53:15
depth3
children1
last_payout2018-02-24 00:53:15
cashout_time1969-12-31 23:59:59
total_payout_value0.040 HBD
curator_payout_value0.013 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length811
author_reputation34,876,406,478,004
root_title"Coding Journal #4: Local Storage, JSON Web Tokens & Wallaby.js"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id38,128,859
net_rshares10,074,925,318
author_curate_reward""
vote details (1)