npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

whodis-react-storage-browser

v0.1.4

Published

React hooks and components for secure, best practices authentication in seconds

Downloads

48

Readme

whodis-react-storage-browser

A secure auth token storage mechanism for whodis-react in browser.

Supports both serverside and clientside rendering.

ci_on_commit deploy_on_tag

install

npm install --save whodis-react-storage-browser

use

Please see the whodis-react package for instructions on how to use this browser storage mechanism.

nuances

cookie based authentication -vs- local development

Cookies are used in the web environment because they are the only way to securely store an auth token in a browser.

  • HTTPOnly flag prevents javascript from accessing the cookie, protecting the token against XSS attacks.
  • Secure flag prevents the token from being sent without HTTPS encryption, protecting the token against MITM attacks.
  • Same-Site flag (+ additional server side validation) prevents the token from being sent from any website, protecting the token against CSRF attacks.

So, using a cookie is a fundamental requirement of securing an auth token in the browser. However, browsers have a lot of rules setup around cookies in order to protect users. There are two that affect us in local development specifically.

1. website must be same-site in order for a browser to set and send a cookie

Browsers protect the user by making sure that the cookie is intended for the website it is sent to - and that the cookie is only sent to the intended website. These are critical to the security of cookies - but do add some extra complexity to working with cookies in local development. Specifically:

  • if the domain of the cookie does not match the domain of the website that the cookie was sent to, the browser will not set it.
  • if the domain of the api that the website is contacting does not match the domain of the cookie, the browser will not send it.
  • if the domain of the api that the website is contacting does not match the domain of the website, the browser will not send the cookie.

In order to solve for this for local development, while still making sure that development and production environments are as similar as possible, you can ask your local computer's DNS to forward requests from localhost.yourdomain.com to localhost.

For example, on UNIX machines, if your website is yourdomain.com, add the following line to /etc/hosts to access your site from localhost.yourdomain.com:

127.0.0.1       localhost.yourdomain.com
2. website must be served over HTTPS in order for the browser to send the cookie

Browsers support a secure flag on cookies which eliminates the potential of man-in-the-middle (MITM) attacks. When this flag is set on a cookie, the cookie will not be sent to a server over HTTP - nor will the cookie be sent from a website served over HTTP.

In order to solve this for local development, while still making sure that development and production environments are as similar as possible, you can serve your website on localhost through HTTPS.

For example, on UNIX machines, if your website is yourdomain.com you can easily generate a self signed certificate for localhost.yourdomain.com with the following:

openssl req -x509 -out localhost.crt -keyout localhost.key \
  -newkey rsa:2048 -nodes -sha256 \
  -subj '/CN=localhost.yourdomain.com' -extensions EXT -config <( \
   printf "[dn]\nCN=localhost.yourdomain.com\n[req]\ndistinguished_name = dn\n[EXT]\nsubjectAltName=DNS:localhost.yourdomain.com\nkeyUsage=digitalSignature\nextendedKeyUsage=serverAuth")

server side rendering

Special consideration is to support server-side rendering frameworks, like Next.JS.

The auth-token is not accessible from the browser's storage mechanisms (e.g., localStorage, cookieStorage) when in server-side-rendering. Therefore, we have to load the token securely into your app's context from the headers of the request payload your server gets.

To do that, this library exposes the loadAuthenticationFromSSRReq method.

For example, generically:

import { loadAuthenticationFromSSRReq } from 'whodis-react-storage-browser';

// ... somewhere with access to `req` ...
loadAuthenticationFromSSRReq({ req });

For example, for Next.JS specifically:

import { loadAuthenticationFromSSRReq } from 'whodis-react-storage-browser';

export const getServerSideProps = async ({ req }) => {
  loadAuthenticationFromSSRReq({ req });
};