Fix #303 Capacitor cookies implementation
This commit is contained in:
parent
6ebebccab2
commit
fde798bcf2
|
|
@ -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')
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
|
|
|
|||
|
|
@ -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');
|
||||
|
|
|
|||
|
|
@ -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' &&
|
||||
|
|
|
|||
|
|
@ -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");
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
|
|
|
|||
|
|
@ -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) {
|
||||
|
|
|
|||
|
|
@ -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();
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
|
|
|
|||
Loading…
Reference in New Issue