@cs124/client-id
Advanced tools
Comparing version 2024.8.0 to 2024.8.1
import crypto from "crypto"; | ||
import { publicIpv4, publicIpv6 } from "public-ip"; | ||
import base64url from "base64url"; | ||
import React, { createContext, useContext, useEffect, useRef, useState } from "react"; | ||
const generateRandomID = () => crypto.randomBytes(20).toString("base64"); | ||
const generateRandomID = () => base64url.fromBase64(crypto.randomBytes(20).toString("base64")); | ||
export const ClientIDContext = createContext({ | ||
@@ -9,23 +10,25 @@ browserID: "", | ||
}); | ||
const BROWSER_ID = "browserID"; | ||
const TAB_ID = "tabID"; | ||
export const ClientIDProvider = ({ IPinterval = 5 * 6 * 1000, disableIP, verbose, cookies, domain, children, }) => { | ||
const browser = useRef((typeof window !== "undefined" && localStorage.getItem("client-id")) || generateRandomID()); | ||
const tab = useRef((typeof window !== "undefined" && sessionStorage.getItem("client-id")) || generateRandomID()); | ||
const browserID = useRef((typeof window !== "undefined" && localStorage.getItem(BROWSER_ID)) || generateRandomID()); | ||
const tabID = useRef((typeof window !== "undefined" && sessionStorage.getItem(TAB_ID)) || generateRandomID()); | ||
useEffect(() => { | ||
verbose && console.log(`Browser: ${browser.current}, Tab: ${tab.current}`); | ||
localStorage.setItem("client-id", browser.current); | ||
sessionStorage.setItem("client-id", tab.current); | ||
verbose && console.log(`Browser: ${browserID.current}, Tab: ${tabID.current}`); | ||
localStorage.setItem(BROWSER_ID, browserID.current); | ||
sessionStorage.setItem(TAB_ID, tabID.current); | ||
}, [verbose]); | ||
useEffect(() => { | ||
if (!cookies) { | ||
document.cookie = `browserID=${browser.current}; max-age=0;`; | ||
document.cookie = `tabID=${tab.current}; max-age=0;`; | ||
document.cookie = `${BROWSER_ID}=${browserID.current}; max-age=0;`; | ||
document.cookie = `${TAB_ID}=${tabID.current}; max-age=0;`; | ||
return; | ||
} | ||
if (domain) { | ||
document.cookie = `browserID=${browser.current}; max-age=31536000; path=/; samesite=lax; domain=${domain};`; | ||
document.cookie = `tabID=${tab.current}; max-age=31536000; path=/; samesite=lax; domain=${domain};`; | ||
document.cookie = `${BROWSER_ID}=${browserID.current}; max-age=31536000; path=/; samesite=lax; domain=${domain};`; | ||
document.cookie = `${TAB_ID}=${tabID.current}; max-age=31536000; path=/; samesite=lax; domain=${domain};`; | ||
} | ||
else { | ||
document.cookie = `browserID=${browser.current}; max-age=31536000; path=/; samesite=lax;`; | ||
document.cookie = `tabID=${tab.current}; max-age=31536000; path=/; samesite=lax;`; | ||
document.cookie = `${BROWSER_ID}=${browserID.current}; max-age=31536000; path=/; samesite=lax;`; | ||
document.cookie = `${TAB_ID}=${tabID.current}; max-age=31536000; path=/; samesite=lax;`; | ||
} | ||
@@ -63,5 +66,5 @@ }, [cookies, domain]); | ||
}, [IPinterval, disableIP, verbose]); | ||
return (React.createElement(ClientIDContext.Provider, { value: { browserID: browser.current, tabID: tab.current, IPv4: ip.v4, IPv6: ip.v6 } }, children)); | ||
return (React.createElement(ClientIDContext.Provider, { value: { browserID: browserID.current, tabID: tabID.current, IPv4: ip.v4, IPv6: ip.v6 } }, children)); | ||
}; | ||
export const useClientID = () => useContext(ClientIDContext); | ||
//# sourceMappingURL=index.js.map |
{ | ||
"name": "@cs124/client-id", | ||
"version": "2024.8.0", | ||
"version": "2024.8.1", | ||
"license": "MIT", | ||
@@ -22,2 +22,3 @@ "type": "module", | ||
"dependencies": { | ||
"base64url": "3.0.1", | ||
"public-ip": "7.0.1" | ||
@@ -29,11 +30,9 @@ }, | ||
"devDependencies": { | ||
"@types/react": "18.3.3", | ||
"@typescript-eslint/eslint-plugin": "8.2.0", | ||
"@typescript-eslint/parser": "8.2.0", | ||
"@eslint/js": "9.9.0", | ||
"@types/react": "18.3.4", | ||
"depcheck": "1.4.7", | ||
"eslint": "8.47.0", | ||
"eslint": "9.9.0", | ||
"eslint-config-prettier": "9.1.0", | ||
"eslint-plugin-prettier": "^5.2.1", | ||
"eslint-plugin-react-hooks": "4.6.2", | ||
"npm-check-updates": "17.0.6", | ||
"npm-check-updates": "17.1.0", | ||
"prettier": "3.3.3", | ||
@@ -44,4 +43,5 @@ "prettier-package-json": "2.8.0", | ||
"rimraf": "6.0.1", | ||
"typescript": "5.5.4" | ||
"typescript": "5.5.4", | ||
"typescript-eslint": "8.2.0" | ||
} | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
12710
14
3
9
219
+ Addedbase64url@3.0.1
+ Addedbase64url@3.0.1(transitive)