1.0.5 • Published 2 years ago

@teleology/cookies v1.0.5

Weekly downloads
-
License
MIT
Repository
github
Last release
2 years ago

@teleology/cookies

A slim implementation for client+server to handle coookies

Installation

yarn add @teleology/cookies

Attribute Options

AttributeDescriptionTypeEnvironment
keyThe name of the cookiestringClient & Server
valueThe value of the cookie (will be coerced)anyClient & Server
maxAgeThe number of seconds until the cookie expiresnumberClient & Sever
expiresA HTTP-date timestamp when the cookie expiresstring or ('Session')Client & Server
pathThe path necessary for the server to send a cookiestringClient & Server
secureCookies are only sent during HTTPS requestsbooleanClient & Sever
httpOnlyPrevents Javascript from accessing the cookiebooleanServer Only
sameSiteWhether or not a cookie is sent with CORS'Strict' or 'Lax' or 'None'Server Only
urlEncodeWhether to encode the cookie valuebooleanClient & Server

Client Usage

import * as cookies from '@teleology/cookies';

cookies.set({
  key: 'access_token',
  value: 'aGVsbG8gd29ybGQK...',
  maxAge: 3600,
  secure: true
});

cookies.get('access_token')

cookies.clear('access_token');

Server Usage

const cookies = require('@teleology/cookies');


const handleLogin = async (args) => {
  let access_token;
  // ... get access token

  const setCookie = cookies.header({
    key: 'access_token',
    value: access_token,
    maxAge: 3600,
    domain: 'example.com',
    path: '/',
    secure: true,
    httpOnly: true,
    sameSite: cookies.SameSite.STRICT,
  });

  // access_token=aGVsbG8gd29ybGQK...; Max-Age=3600; Domain=example.com; Path=/; SameSite=Strict; Secure; HttpOnly

  return {
    statusCode: 200,
    headers: {
      'Set-Cookie': setCookie,
    },
    body: {
      access_token,
      expires_in: 3600,

      // ...
    }
  }
}

Changelog

1.0.0

  • Initial publications