diff --git a/package-lock.json b/package-lock.json index 33e13186fc39081cf8a70dee6d7d1ce2c6e1b0c0..2ca87c15f78f00350aa4d992db9e9c056bd1afa6 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,15 +1,16 @@ { "name": "@lidh04/circle-chain-sdk", - "version": "1.0.0", + "version": "1.0.7", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "@lidh04/circle-chain-sdk", - "version": "1.0.0", + "version": "1.0.7", "license": "ISC", "dependencies": { "axios": "^1.7.4", + "js-cookie": "^3.0.5", "tsd-jsdoc": "^2.5.0" }, "devDependencies": {} @@ -197,6 +198,14 @@ "integrity": "sha512-RbJ5/jmFcNNCcDV5o9eTnBLJ/HszWV0P73bc+Ff4nS/rJj+YaS6IGyiOL0VoBYX+l1Wrl3k63h/KrH+nhJ0XvQ==", "peer": true }, + "node_modules/js-cookie": { + "version": "3.0.5", + "resolved": "https://registry.npmjs.org/js-cookie/-/js-cookie-3.0.5.tgz", + "integrity": "sha512-cEiJEAEoIbWfCZYKWhVwFuvPX1gETRYPw6LlaTKoxD3s2AkXzkCjnp6h0V77ozyqj0jakteJ4YqDJT830+lVGw==", + "engines": { + "node": ">=14" + } + }, "node_modules/js2xmlparser": { "version": "4.0.2", "resolved": "https://registry.npmjs.org/js2xmlparser/-/js2xmlparser-4.0.2.tgz", diff --git a/package.json b/package.json index dae2f35dc61f40a19523bda4b92934680253d5ef..6342d2ea745b998549f7bfc794c152741e7a259c 100644 --- a/package.json +++ b/package.json @@ -15,9 +15,9 @@ "license": "ISC", "dependencies": { "axios": "^1.7.4", + "js-cookie": "^3.0.5", "tsd-jsdoc": "^2.5.0" }, - "devDependencies": {}, "repository": { "type": "git", "url": "git@gitee.com:lidh04/js-circle-chain-sdk.git" diff --git a/src/circle-common.js b/src/circle-common.js index 0627450df1eecb13192a08e9c1786619bba5424e..18826bd0e0d8ed53bac0d24f81c8bc214a24b922 100644 --- a/src/circle-common.js +++ b/src/circle-common.js @@ -1,7 +1,12 @@ -import gateway from "./circle-gateway.js"; import axios from 'axios'; - import { readFile } from 'node:fs/promises'; +import Cookies from 'js-cookie'; +import gateway from "./circle-gateway.js"; + +/** + * @type string + */ +export const COOKIE_KEY = "userLoginInfo"; /** * build url tool @@ -68,8 +73,13 @@ export async function postData(url, input, timeout=30000) { * get session key from the path. */ export async function getSessionKey() { - if (process.browser) { - // TODO return the data in cookie + if (isBrowser()) { + const value = Cookies.get(COOKIE_KEY); + if (value) { + const userLoginInfo = JSON.parse(value); + const { sessionKey } = userLoginInfo; + return sessionKey; + } return null; } @@ -126,3 +136,24 @@ export async function getData(url, timeout=30000) { }; } } + +export function isBrowser() { + // Check if the environment is a + // Service worker + if (typeof importScripts === "function") { + return false; + } + + // Check if the environment is a Browser + if (typeof window === "object") { + return true; + } + + // Check if the environment is Node.js + if (typeof process === "object" + && typeof require === "function") { + return false; + } + + return false; +} diff --git a/src/circle-user.js b/src/circle-user.js index 9971f9c74a9f94367c281fffbf7e9b6620d736e3..5bfd469e52757ae8088e0dc4815c630d9b35a9ce 100644 --- a/src/circle-user.js +++ b/src/circle-user.js @@ -5,11 +5,11 @@ * @license copyright to shc */ -import gateway from "./circle-gateway.js"; +import Cookies from 'js-cookie'; import { writeFile, mkdir } from 'node:fs/promises'; import path from 'path'; -import { buildUrl, getData, postData } from "./circle-common.js"; - +import { buildUrl, getData, postData, COOKIE_KEY, isBrowser } from "./circle-common.js"; +import gateway from "./circle-gateway.js"; /// @@ -136,8 +136,10 @@ export async function login(input) { * @returns {Promise} - no returns. */ export async function saveUserLoginInfo(userLoginInfo) { - if (process.browser) { - // TODO store the login info in cookie + if (isBrowser()) { + Cookies.set(COOKIE_KEY, JSON.stringify(userLoginInfo), { + expires: 7, path: '/' + }); return; } @@ -158,8 +160,7 @@ ${accountInfo} await mkdir(dir, { recursive: true }); await writeFile(sessionFullPath, content); } catch (err) { - console.error("cannot save user login info:", JSON.stringify(userLoginInfo), - "to session path:", sessionFullPath); + console.error("cannot save user login info:", JSON.stringify(userLoginInfo), "to session path:", sessionFullPath); } } diff --git a/tools/logged-user.js b/tools/logged-user.js index dd5d9e9c67c09b897542065365e65028809c66be..63da7f4684b7f6b3eb1da95bcd899ee59d6d1f53 100644 --- a/tools/logged-user.js +++ b/tools/logged-user.js @@ -9,6 +9,7 @@ async function main() { console.log("userInfo:", userInfo); const balanceOfWallet = await circle.wallet.getBalanceOfWallet(); console.log("balanceOfWallet:", balanceOfWallet); + console.log("require:", typeof(importScripts)); } async function loginUser() {