diff --git a/src/Features/AuthenicationToken/AuthenticationToken.js b/src/Features/AuthenicationToken/AuthenticationToken.js index b06f214..77a8d99 100644 --- a/src/Features/AuthenicationToken/AuthenticationToken.js +++ b/src/Features/AuthenicationToken/AuthenticationToken.js @@ -27,13 +27,13 @@ const axiosRetailOcr = axios.create({ const getToken = () => { return new Promise((resolve) => { - let token = sessionStorage.getItem('auth'); + let token = getSession('auth'); if (token) { resolve(token); } else { const tokenInterval = setInterval(() => { - token = sessionStorage.getItem('auth'); + token = getSession('auth'); if (token) { clearInterval(tokenInterval); resolve(token); @@ -51,11 +51,11 @@ const addAuthHeader = async (config) => { } else { console.warn('Token is still not available.'); } - let encryptedMobileno = sessionStorage.getItem('MobileNo'); - let encryptedUserId = sessionStorage.getItem('UserId'); - let encryptedLoginType = TokendecryptedValuesFun( - sessionStorage.getItem('LoginType') - ); + let encryptedMobileno = getSession('MobileNo'); + let encryptedUserId = getSession('UserId'); + let encryptedLoginType = + getSession('LoginType') + console.log( 'encryptedLoginType', encryptedLoginType, @@ -64,7 +64,7 @@ const addAuthHeader = async (config) => { // let Mobileno = encryptedUserId && encryptedLoginType != "Kiosk" ? TokendecryptedValuesFun(encryptedMobileno) : '1000000001'; let Mobileno = encryptedUserId - ? TokendecryptedValuesFun(encryptedMobileno) + ? encryptedMobileno : '1000000001'; console.warn('Mobileno is still not available.'); @@ -141,8 +141,8 @@ const responseErrorHandler = async (error) => { method: 'PUT', headers: { 'Content-Type': 'application/json', - Authorization: sessionStorage.getItem('auth') - ? sessionStorage.getItem('auth') + Authorization: getSession('auth') + ? getSession('auth') : 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9', Mobileno: getSession('MobileNo') ? getSession('MobileNo') diff --git a/src/Pages/BookingScreen/BookingPage.jsx b/src/Pages/BookingScreen/BookingPage.jsx index 6eefdf3..ae089d4 100644 --- a/src/Pages/BookingScreen/BookingPage.jsx +++ b/src/Pages/BookingScreen/BookingPage.jsx @@ -76,7 +76,7 @@ const BookingPage = () => { const UserId = getSession('UserId'); const AppName = getSession('AppName'); const UserType = getSession('UserType'); - const AuthToken = sessionStorage.getItem('auth'); + const AuthToken = getSession('auth'); const SessionMobileNo = getSession('MobileNo'); const FeatureAddonData = useSelector(GlobalFeatAddOnData, shallowEqual); const Scantemplatedata = useSelector(GlobalScanTemplate, shallowEqual); diff --git a/src/Pages/BookingScreen/Components/OtherConponents/Reprint/BSReprint.jsx b/src/Pages/BookingScreen/Components/OtherConponents/Reprint/BSReprint.jsx index 344c815..5749d4f 100644 --- a/src/Pages/BookingScreen/Components/OtherConponents/Reprint/BSReprint.jsx +++ b/src/Pages/BookingScreen/Components/OtherConponents/Reprint/BSReprint.jsx @@ -170,7 +170,7 @@ function BSReprint({ setOpenModel = () => { } }) { const AppId = getSession('AppId'); const UserId = getSession('UserId'); const UserType = getSession('UserType'); - const AuthToken = sessionStorage.getItem('auth'); + const AuthToken = getSession('auth'); const SessionMobileNo = getSession('MobileNo'); const [dateStrings, setDateStrings] = useState([]); const [type, setType] = useState('Sales'); diff --git a/src/Pages/BookingScreen/Components/UtillComponents/BSPrinterSetting.jsx b/src/Pages/BookingScreen/Components/UtillComponents/BSPrinterSetting.jsx index 990ea68..79756c6 100644 --- a/src/Pages/BookingScreen/Components/UtillComponents/BSPrinterSetting.jsx +++ b/src/Pages/BookingScreen/Components/UtillComponents/BSPrinterSetting.jsx @@ -39,7 +39,7 @@ const BSPrinterSetting = ({ const SettingData = useSelector(PreferenceData); const BSLayout1Data = useSelector(getTemplateData); const navbarOptions = BSLayout1Data?.BookingNavbar?.[1]; - const AuthToken = sessionStorage.getItem('auth'); + const AuthToken = getSession('auth'); const mobileA4Preference = SettingData?.[0]?.SettingDtlDetails?.find( (setting) => setting?.SettingIdName?.toLowerCase() === 'mobilea4' && diff --git a/src/Pages/ExtentedModal/ExtendSubscriptionModal.jsx b/src/Pages/ExtentedModal/ExtendSubscriptionModal.jsx index e27beb5..70225ad 100644 --- a/src/Pages/ExtentedModal/ExtendSubscriptionModal.jsx +++ b/src/Pages/ExtentedModal/ExtendSubscriptionModal.jsx @@ -19,7 +19,7 @@ const ExtendSubscriptionModal = ({ const AppId =getSession("AppId"); const UserId =getSession("UserId"); const SessionId =getSession("SessionId"); - const AuthToken =sessionStorage.getItem('auth'); + const AuthToken =getSession('auth'); const CompId =getSession("CompId"); const SessionMobileNo =getSession("MobileNo"); const UserName =getSession("userName"); diff --git a/src/Pages/Kiosk/KioskBookingPage.jsx b/src/Pages/Kiosk/KioskBookingPage.jsx index abbf814..e99518b 100644 --- a/src/Pages/Kiosk/KioskBookingPage.jsx +++ b/src/Pages/Kiosk/KioskBookingPage.jsx @@ -50,7 +50,7 @@ const KioskBookingPage = () => { const AppName = getSession('AppName'); const userName = getSession('userName'); const MobileNo = getSession('MobileNo'); - const AuthToken = sessionStorage.getItem('auth') + const AuthToken = getSession('auth') const SessionDtl = { CompId: CompId, BranchId: BranchId, @@ -82,7 +82,7 @@ const KioskBookingPage = () => { console.log('queryParams', queryParams); if (Object.keys(queryParams)?.length !== 0) { console.log('queryParams', queryParams); - if (!sessionStorage.getItem('auth')) { + if (!getSession('auth')) { let data = { username: '1000000001', password: '1234', @@ -99,7 +99,7 @@ const KioskBookingPage = () => { ); const { token } = response?.data; - sessionStorage.setItem('auth', token); + sessionStore('auth', token); sessionStore('LoginType', 'Kiosk'); } const MACAddress = queryParams?.MD; @@ -119,7 +119,7 @@ const KioskBookingPage = () => { let responseData = deviceMACResponse?.data?.data?.[0]; responseData?.accessToken != null && responseData?.accessToken != undefined && - sessionStorage.setItem('auth', responseData?.accessToken); + sessionStore('auth', responseData?.accessToken); responseData?.MobileNo != null && responseData?.MobileNo != undefined && sessionStore('MobileNo', responseData?.MobileNo); diff --git a/src/Pages/PurchaseOrder/CustomerPurchaseConfirm/CustomerPurchaseConfirm.jsx b/src/Pages/PurchaseOrder/CustomerPurchaseConfirm/CustomerPurchaseConfirm.jsx index b4603ee..535c4d9 100644 --- a/src/Pages/PurchaseOrder/CustomerPurchaseConfirm/CustomerPurchaseConfirm.jsx +++ b/src/Pages/PurchaseOrder/CustomerPurchaseConfirm/CustomerPurchaseConfirm.jsx @@ -12,7 +12,7 @@ const url_string = window.location.href; const url = new URL(url_string); const codesParam = url.searchParams.get('code'); -import { sessionStore } from '../../../Services/Others'; +import { getSession, sessionStore } from '../../../Services/Others'; import Loader from '../../../Components/Loader/Loader'; import { useDispatch } from 'react-redux'; import { @@ -85,7 +85,7 @@ function CustomerPurchaseConfirm() { }; const addSession = async () => { - if (!sessionStorage.getItem('auth')) { + if (!getSession('auth')) { try { const data = { username: '1000000001', password: '1234' }; const response = await axios.post( @@ -102,7 +102,7 @@ function CustomerPurchaseConfirm() { const { token } = response?.data; if (token) { - sessionStorage.setItem('auth', token); + sessionStore('auth', token); sessionStore('LoginType', 'Kiosk'); } } catch (error) { diff --git a/src/Pages/PurchaseOrder/CustomerPurchaseConfirm/PurchaseLink.jsx b/src/Pages/PurchaseOrder/CustomerPurchaseConfirm/PurchaseLink.jsx index f395321..f7d252d 100644 --- a/src/Pages/PurchaseOrder/CustomerPurchaseConfirm/PurchaseLink.jsx +++ b/src/Pages/PurchaseOrder/CustomerPurchaseConfirm/PurchaseLink.jsx @@ -11,7 +11,7 @@ import { jsPDF } from "jspdf"; import html2canvas from "html2canvas"; import { PublicQrCodeget } from "../../../Features/ConfigMasterPage/ConfigMasterPage"; -import { decryptToObject } from "../../../Services/Others"; +import { decryptToObject, getSession } from "../../../Services/Others"; import { getBranchDetail } from "../../../Features/BrachLogin/BranchLogin"; import "./PurchaseLink.scss"; @@ -33,13 +33,13 @@ const PurchaseLink = () => { const initializeData = async () => { try { - if (!sessionStorage.getItem("auth")) { + if (!getSession("auth")) { const { data } = await axios.post(`${apiUrlToken}/jwtTokenGenerator`, { username: "1000000001", password: "1234", }); - sessionStorage.setItem("auth", data.token); - sessionStorage.setItem("LoginType", "Public"); + sessionStore("auth", data.token); + sessionStore("LoginType", "Public"); } const qrData = await dispatch(PublicQrCodeget(codesParam)).unwrap(); diff --git a/src/Pages/Reports/MenuQRCode/CustomerMenuPage.jsx b/src/Pages/Reports/MenuQRCode/CustomerMenuPage.jsx index 46d00ec..a715295 100644 --- a/src/Pages/Reports/MenuQRCode/CustomerMenuPage.jsx +++ b/src/Pages/Reports/MenuQRCode/CustomerMenuPage.jsx @@ -2,7 +2,7 @@ import { useEffect, useRef, useState } from "react"; import axios from "axios"; import { Phone, Mail, Globe, MapPin, Facebook, Instagram } from "lucide-react"; import "./MenuQRCode.scss"; -import { decryptToObject, sessionStore } from "../../../Services/Others"; +import { decryptToObject, getSession, sessionStore } from "../../../Services/Others"; import { useDispatch } from "react-redux"; import { PublicQrCodeget, @@ -94,13 +94,13 @@ const CustomerMenuPage = () => { }; const addSession = async () => { - if (!sessionStorage.getItem("auth")) { + if (!getSession("auth")) { try { const { data } = await axios.post(`${apiUrlToken}/jwtTokenGenerator`, { username: "1000000001", password: "1234", }); - sessionStorage.setItem("auth", data.token); + sessionStore("auth", data.token); sessionStore("LoginType"); } catch (error) { console.error("Session Error:", error); diff --git a/src/Pages/SelfBooking/SelfBooking.jsx b/src/Pages/SelfBooking/SelfBooking.jsx index e15c2f3..d7f10ac 100644 --- a/src/Pages/SelfBooking/SelfBooking.jsx +++ b/src/Pages/SelfBooking/SelfBooking.jsx @@ -166,7 +166,7 @@ const SelfBooking = () => { dispatch(changeSelectTable([])); dispatch(changeSelectChair([])); const token = await generateToken(); - sessionStorage.setItem('auth', token); + sessionStore('auth', token); const qrResponse = await dispatch( PublicQrCodepost(SelfBookingId) diff --git a/src/Services/Others.js b/src/Services/Others.js index 08396a4..fd6b5b2 100644 --- a/src/Services/Others.js +++ b/src/Services/Others.js @@ -7,11 +7,12 @@ import store from '../app/store.js'; // import html2pdf from 'html2pdf.js'; import html2canvas from 'html2canvas'; +import { Capacitor } from '@capacitor/core'; const cookies = new Cookies(); const SECRET_KEY = import.meta.env.ENV_SECRET_KEY; const URL_SECRET_KEY = import.meta.env.ENV_URL_SECRET_KEY; - +const isCapacitor = () => Capacitor.isNativePlatform(); export const localstore = (GSTIN) => { localStorage.setItem('GSTIN', GSTIN); }; @@ -34,14 +35,14 @@ export const getLocalStorageValues = (key) => { return localStorage.getItem(key); }; -export const sessionStore = (key, value) => { - const encryptedValue = CryptoJS.AES.encrypt( - JSON.stringify(value), - SECRET_KEY - ).toString(); +// export const sessionStore = (key, value) => { +// const encryptedValue = CryptoJS.AES.encrypt( +// JSON.stringify(value), +// SECRET_KEY +// ).toString(); - if (encryptedValue) sessionStorage.setItem(key, encryptedValue); -}; +// if (encryptedValue) sessionStorage.setItem(key, encryptedValue); +// }; export const encryptedValuesFun = (value) => { const encryptedValue = CryptoJS.AES.encrypt( @@ -131,19 +132,88 @@ export const TokendecryptedValuesFun = (value) => { } } }; +// ✅ Cookie helpers (used only in Capacitor) +const setCapCookieData = (key, value, days = 1) => { + console.log("Setting cookie:", key, value); + const expires = new Date(); + expires.setDate(expires.getDate() + days); + document.cookie = `${key}=${encodeURIComponent(value)}; expires=${expires.toUTCString()}; path=/`; +}; -export const getSession = (key) => { - const val = sessionStorage.getItem(key); - if (val) { - const decrypted = CryptoJS.AES.decrypt(val, SECRET_KEY).toString( - CryptoJS.enc.Utf8 - ); +const getCapCookieData = (key) => { + const match = document.cookie + .split("; ") + .find((row) => row.startsWith(`${key}=`)); + return match ? decodeURIComponent(match.split("=")[1]) : null; +}; - if (decrypted) { - return JSON.parse(decrypted); +export const sessionStore = (key, value) => { + console.log("Storing session:", key, value); + const encryptedValue = key !== "auth" + ? CryptoJS.AES.encrypt(JSON.stringify(value), SECRET_KEY).toString() + : value; + // const encryptedValue = CryptoJS.AES.encrypt( + // JSON.stringify(value), + // SECRET_KEY + // ).toString(); + + if (encryptedValue) { + if (isCapacitor()) { + // 📱 Capacitor App → cookie (persists after close) + setCapCookieData(key, encryptedValue, 7); + } else { + // 🌐 Browser → sessionStorage (clears on tab close) + sessionStorage.setItem(key, encryptedValue); + } } +} + + + +// ✅ GET session +export const getSession = (key) => { + try { + let val = null; + + if (isCapacitor()) { + // 📱 Capacitor App → cookie + val = getCapCookieData(key); + } else { + // 🌐 Browser → sessionStorage + val = sessionStorage.getItem(key); + } + + if (val) { + if (key !== "auth") { + // 🔐 Decrypt for all keys except "auth" + const decrypted = CryptoJS.AES.decrypt(val, SECRET_KEY).toString( + CryptoJS.enc.Utf8 + ); + return JSON.parse(decrypted); + } else { + // 🔓 "auth" was stored without encryption, just parse + return val + } +} + + return null; + } catch (error) { + console.error("getSession error:", error); + return null; } }; +// export const getSession = (key) => { +// const val = sessionStorage.getItem(key); +// if (val) { +// const decrypted = CryptoJS.AES.decrypt(val, SECRET_KEY).toString( +// CryptoJS.enc.Utf8 +// ); + +// if (decrypted) { +// return JSON.parse(decrypted); +// } +// } +// }; export const clearSession = (key) => { const val = sessionStorage.getItem(key);