Essential CSS
JavaScript Roadmap
Learn JavaScript
Mastering Web Typography
Practical Astro
Understanding Async JS
Unorthodox Tailwind
API Masterclass
Build and Deploy
The Beginner Freelancer
Real World Node
Profile Billing Logout
Understanding Async JS
Lesson Plan
  1. Introduction
    1. What is Asynchronous JS?
    2. Callbacks are not automatically asynchronous
    3. What is an API?
    4. Evolution of HTTP APIs
    5. Ajax vs Asynchronous JavaScript
  2. Fundamental Concepts
    1. How to fetch resources
    2. Using XHR
    3. Using the Fetch API
    4. Using libraries
    5. Core JavaScript concepts
    6. Understanding Promises
    7. Understanding Async Functions
    8. Understanding the Event Loop
    9. Understanding JSON
    10. Async JS Best Practices
    11. Using Optimistic UI
  3. Understanding Rest API
    1. Diving into Rest APIs
    2. The Rest Architecture
    3. The Rest Structure
    4. Anatomy of a Request
    5. What's an endpoint
    6. Request methods
    7. Understanding request headers
    8. Understanding the request body
    9. Authentication
    10. Basic Authentication
    11. Bearer Authentication
    12. Authenticating with API keys and API Secrets
    13. Cross Origin Resource Sharing
    14. Anatomy of a response
    15. HTTP Statuses
    16. Response headers
    17. The response body
    18. Handling Errors
    19. Reading API documentations
    20. Understanding Curl
  4. Node Basics
    1. Why we are using Node
    2. Command Line Basics
    3. Installing Node
    4. Managing dependencies
    5. Using ES Modules in Node
    6. Using Node
    7. Restarting the server automatically
    8. Sending requests via Node
    9. Protecting API keys and secrets
  5. Building a server
    1. What's a server
    2. Building a simple server with Express
    3. Two kinds of servers
    4. API Server architecture
    5. Handling routes
    6. Understanding Middlewares
    7. Enabling CORS
    8. Receiving data
    9. Sending responses
    10. Handling errors in Express
    11. Using the server as a proxy
    12. Minimum viable server
  6. Open Authorization
    1. Open Authorisation (OAuth)
    2. Authenticating with Access Tokens
    3. Obtaining Access Tokens
    4. Public and Confidential Clients
    5. Authorisation Code Grant
    6. Understanding scope
    7. Proof Key for Code Exchange (PKCE)
    8. PKCE with Twitter
    9. Dynamic state and `code_verifier`
    10. Saving the access token in cookies
    11. Passing access tokens to the browser
    12. Saving the access token in local storage
    13. Refresh Token Grant
    14. Device Code Grant
    15. Client libraries vs REST
  7. Building Social Media Logins
    1. Introduction
    2. Starter Files
    3. Creating SSL certificates
    4. Login in with Github
    5. Github: Saving the token to local storage
    6. Github: Persisting the login status
    7. Login with Twitter
    8. Twitter: Saving the token to local storage
    9. Twitter: Persisting the login status
    10. Twitter: Refreshing the access token
    11. Login with Facebook
    12. Facebook: Saving the token to localStorage
    13. Facebook: Persisting the login status
    14. Facebook: Refreshing the access token
    15. Login with google
    16. Google: Saving the token to localStorage
    17. Google: Refreshing the access token

Saving the access token in local storage

2m:23s

We’ll cover how to use local storage in three ways:

  1. Basic local storage usage
  2. How to store multiple values in local storage
  3. How to use local storage with a library

Basic local storage usage

You can save things into local storage with setItem.

Since local storage only supports string values, the values you saved inside local storage will be converted into a string.

localStorage.setItem('key', 'value')

You can get the item back from local storage with getItem.

localStorage.getItem('key')

To delete items from local storage, you use deleteItem.

local storage.deleteItem('key')

How to store multiple values in local storage

You can save multiple values in local storage using two methods:

  1. Using multiple entries
  2. Save the values as a JSON Object.

Using multiple entries

Using multiple entries is straightforward — you save each value as a separate entry.

You can do this by calling setItem for each value you want to save.

// Using multiple entries
local storage.setItem('access_token', access_token)
local storage.setItem('expiresAt', Date.now() + expires_in * 1000)

Using a JSON Object

It’s also quite straightforward to save values as a JSON Object. To do this, you convert the JavaScript object (or array) into JSON with JSON.stringify.

// Saving the values as a JSON object
const token = {
value,
expiresAt: Date.now() + expires_in * 1000,
}
local storage.setItem('token', JSON.stringify(token))

If you want to get the object value back from local storage, you have to use getItem to retrieve the JSON value, then JSON.parse to convert it back to an object.

const token = JSON.parse(local storage.getItem('token'))

Making things easier with a library

I created a library called localStore that helps you make local storage easier to use. You can read the documentation here.

It helps you save strings and objects without worrying about the JSON conversion — it does the JSON conversion for you automatically (and when necessary).

You can install localStore by installing with this command:

Terminal window
npm install @zellwk/javascript

You can include localStore in your project like this:

import localStore from '@zellwk/javascript/browser/localStore'

If you don’t use npm, you can include localStore in your project like this:

import localStore from 'https://cdn.jsdelivr.net/npm/@zellwk/[email protected]/src/browser/localstore.js'
Note

Remember to change the version number from @3.0.0 to the version you’re requesting for! See releases for the latest release.

Using localStore

When you save a value, localStore converts objects into JSON automatically so you don’t have to write JSON.stringify.

// Saving a value into local storage
const value = {
access_token,
expiry: Date.now() + expires_in * 1000,
}
localStore.set('token', value)

When you retrieve a value, localStore converts the value from JSON back to JavaScript automatically, so there’s no need to write JSON.parse.

// Getting an item from local storage
import localStore from '@zellwk/javascript/localstore'
const token = localStore.get('token')