UNPKG

tiny-essentials

Version:

Collection of small, essential scripts designed to be used across various projects. These simple utilities are crafted for speed, ease of use, and versatility.

36 lines (30 loc) 1.1 kB
'use strict'; var objFilter = require('../../../../v1/basics/objFilter.cjs'); // @ts-nocheck /** * Retrieves a session value from the request's session object based on the provided key. * If the session object and the specific session key exist and are of the expected type, * the value is returned. Otherwise, `null` is returned. * * @function * @param {Record<string, any>} req - The request object, which contains the session data. * @param {string} where - The key for which the session value is being retrieved. * * @returns {string|null} The session value associated with the provided key, or `null` if it doesn't exist or isn't valid. * * @example * const userToken = cookieSession(req, 'authToken'); * if (userToken) { * console.log('Session token:', userToken); * } else { * console.log('No valid token in session'); * } */ function cookieSession(req, where) { // Get Token if (objFilter.objType(req.session, 'object') && typeof req.session[where] === 'string') return req.session[where]; // Nope else return null; } module.exports = cookieSession;