UNPKG

@wristband/react-client-auth

Version:

A lightweight React SDK that pairs with your backend server auth to initialize and sync frontend sessions via secure session cookies.

32 lines (29 loc) 1.05 kB
'use strict'; /** * Helper function to retrieve a CSRF token from browser cookies. * * Searches through all cookies in the document to find the one matching the provided name and extracts its value. * Handles cookie parsing and decoding. * * @param {string} cookieName - The name of the cookie containing the CSRF token. * @required * @returns {string | null} The CSRF token value if found; null otherwise. * * @example * const token = getCsrfToken('CSRF-TOKEN'); * * @private - This is an internal helper function not intended for external use */ function getCsrfToken(cookieName) { const name = cookieName + '='; const decodedCookie = decodeURIComponent(document.cookie); const cookieArray = decodedCookie.split(';'); for (let i = 0; i < cookieArray.length; i++) { const cookie = cookieArray[i].trim(); if (cookie.indexOf(name) === 0) { return cookie.substring(name.length, cookie.length); } } return null; } exports.getCsrfToken = getCsrfToken;