Fix #303 Capacitor cookies implementation
This commit is contained in:
parent
6ebebccab2
commit
fde798bcf2
|
|
@ -27,13 +27,13 @@ const axiosRetailOcr = axios.create({
|
||||||
|
|
||||||
const getToken = () => {
|
const getToken = () => {
|
||||||
return new Promise((resolve) => {
|
return new Promise((resolve) => {
|
||||||
let token = sessionStorage.getItem('auth');
|
let token = getSession('auth');
|
||||||
|
|
||||||
if (token) {
|
if (token) {
|
||||||
resolve(token);
|
resolve(token);
|
||||||
} else {
|
} else {
|
||||||
const tokenInterval = setInterval(() => {
|
const tokenInterval = setInterval(() => {
|
||||||
token = sessionStorage.getItem('auth');
|
token = getSession('auth');
|
||||||
if (token) {
|
if (token) {
|
||||||
clearInterval(tokenInterval);
|
clearInterval(tokenInterval);
|
||||||
resolve(token);
|
resolve(token);
|
||||||
|
|
@ -51,11 +51,11 @@ const addAuthHeader = async (config) => {
|
||||||
} else {
|
} else {
|
||||||
console.warn('Token is still not available.');
|
console.warn('Token is still not available.');
|
||||||
}
|
}
|
||||||
let encryptedMobileno = sessionStorage.getItem('MobileNo');
|
let encryptedMobileno = getSession('MobileNo');
|
||||||
let encryptedUserId = sessionStorage.getItem('UserId');
|
let encryptedUserId = getSession('UserId');
|
||||||
let encryptedLoginType = TokendecryptedValuesFun(
|
let encryptedLoginType =
|
||||||
sessionStorage.getItem('LoginType')
|
getSession('LoginType')
|
||||||
);
|
|
||||||
console.log(
|
console.log(
|
||||||
'encryptedLoginType',
|
'encryptedLoginType',
|
||||||
encryptedLoginType,
|
encryptedLoginType,
|
||||||
|
|
@ -64,7 +64,7 @@ const addAuthHeader = async (config) => {
|
||||||
|
|
||||||
// let Mobileno = encryptedUserId && encryptedLoginType != "Kiosk" ? TokendecryptedValuesFun(encryptedMobileno) : '1000000001';
|
// let Mobileno = encryptedUserId && encryptedLoginType != "Kiosk" ? TokendecryptedValuesFun(encryptedMobileno) : '1000000001';
|
||||||
let Mobileno = encryptedUserId
|
let Mobileno = encryptedUserId
|
||||||
? TokendecryptedValuesFun(encryptedMobileno)
|
? encryptedMobileno
|
||||||
: '1000000001';
|
: '1000000001';
|
||||||
|
|
||||||
console.warn('Mobileno is still not available.');
|
console.warn('Mobileno is still not available.');
|
||||||
|
|
@ -141,8 +141,8 @@ const responseErrorHandler = async (error) => {
|
||||||
method: 'PUT',
|
method: 'PUT',
|
||||||
headers: {
|
headers: {
|
||||||
'Content-Type': 'application/json',
|
'Content-Type': 'application/json',
|
||||||
Authorization: sessionStorage.getItem('auth')
|
Authorization: getSession('auth')
|
||||||
? sessionStorage.getItem('auth')
|
? getSession('auth')
|
||||||
: 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9',
|
: 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9',
|
||||||
Mobileno: getSession('MobileNo')
|
Mobileno: getSession('MobileNo')
|
||||||
? getSession('MobileNo')
|
? getSession('MobileNo')
|
||||||
|
|
|
||||||
|
|
@ -76,7 +76,7 @@ const BookingPage = () => {
|
||||||
const UserId = getSession('UserId');
|
const UserId = getSession('UserId');
|
||||||
const AppName = getSession('AppName');
|
const AppName = getSession('AppName');
|
||||||
const UserType = getSession('UserType');
|
const UserType = getSession('UserType');
|
||||||
const AuthToken = sessionStorage.getItem('auth');
|
const AuthToken = getSession('auth');
|
||||||
const SessionMobileNo = getSession('MobileNo');
|
const SessionMobileNo = getSession('MobileNo');
|
||||||
const FeatureAddonData = useSelector(GlobalFeatAddOnData, shallowEqual);
|
const FeatureAddonData = useSelector(GlobalFeatAddOnData, shallowEqual);
|
||||||
const Scantemplatedata = useSelector(GlobalScanTemplate, shallowEqual);
|
const Scantemplatedata = useSelector(GlobalScanTemplate, shallowEqual);
|
||||||
|
|
|
||||||
|
|
@ -170,7 +170,7 @@ function BSReprint({ setOpenModel = () => { } }) {
|
||||||
const AppId = getSession('AppId');
|
const AppId = getSession('AppId');
|
||||||
const UserId = getSession('UserId');
|
const UserId = getSession('UserId');
|
||||||
const UserType = getSession('UserType');
|
const UserType = getSession('UserType');
|
||||||
const AuthToken = sessionStorage.getItem('auth');
|
const AuthToken = getSession('auth');
|
||||||
const SessionMobileNo = getSession('MobileNo');
|
const SessionMobileNo = getSession('MobileNo');
|
||||||
const [dateStrings, setDateStrings] = useState([]);
|
const [dateStrings, setDateStrings] = useState([]);
|
||||||
const [type, setType] = useState('Sales');
|
const [type, setType] = useState('Sales');
|
||||||
|
|
|
||||||
|
|
@ -39,7 +39,7 @@ const BSPrinterSetting = ({
|
||||||
const SettingData = useSelector(PreferenceData);
|
const SettingData = useSelector(PreferenceData);
|
||||||
const BSLayout1Data = useSelector(getTemplateData);
|
const BSLayout1Data = useSelector(getTemplateData);
|
||||||
const navbarOptions = BSLayout1Data?.BookingNavbar?.[1];
|
const navbarOptions = BSLayout1Data?.BookingNavbar?.[1];
|
||||||
const AuthToken = sessionStorage.getItem('auth');
|
const AuthToken = getSession('auth');
|
||||||
const mobileA4Preference = SettingData?.[0]?.SettingDtlDetails?.find(
|
const mobileA4Preference = SettingData?.[0]?.SettingDtlDetails?.find(
|
||||||
(setting) =>
|
(setting) =>
|
||||||
setting?.SettingIdName?.toLowerCase() === 'mobilea4' &&
|
setting?.SettingIdName?.toLowerCase() === 'mobilea4' &&
|
||||||
|
|
|
||||||
|
|
@ -19,7 +19,7 @@ const ExtendSubscriptionModal = ({
|
||||||
const AppId =getSession("AppId");
|
const AppId =getSession("AppId");
|
||||||
const UserId =getSession("UserId");
|
const UserId =getSession("UserId");
|
||||||
const SessionId =getSession("SessionId");
|
const SessionId =getSession("SessionId");
|
||||||
const AuthToken =sessionStorage.getItem('auth');
|
const AuthToken =getSession('auth');
|
||||||
const CompId =getSession("CompId");
|
const CompId =getSession("CompId");
|
||||||
const SessionMobileNo =getSession("MobileNo");
|
const SessionMobileNo =getSession("MobileNo");
|
||||||
const UserName =getSession("userName");
|
const UserName =getSession("userName");
|
||||||
|
|
|
||||||
|
|
@ -50,7 +50,7 @@ const KioskBookingPage = () => {
|
||||||
const AppName = getSession('AppName');
|
const AppName = getSession('AppName');
|
||||||
const userName = getSession('userName');
|
const userName = getSession('userName');
|
||||||
const MobileNo = getSession('MobileNo');
|
const MobileNo = getSession('MobileNo');
|
||||||
const AuthToken = sessionStorage.getItem('auth')
|
const AuthToken = getSession('auth')
|
||||||
const SessionDtl = {
|
const SessionDtl = {
|
||||||
CompId: CompId,
|
CompId: CompId,
|
||||||
BranchId: BranchId,
|
BranchId: BranchId,
|
||||||
|
|
@ -82,7 +82,7 @@ const KioskBookingPage = () => {
|
||||||
console.log('queryParams', queryParams);
|
console.log('queryParams', queryParams);
|
||||||
if (Object.keys(queryParams)?.length !== 0) {
|
if (Object.keys(queryParams)?.length !== 0) {
|
||||||
console.log('queryParams', queryParams);
|
console.log('queryParams', queryParams);
|
||||||
if (!sessionStorage.getItem('auth')) {
|
if (!getSession('auth')) {
|
||||||
let data = {
|
let data = {
|
||||||
username: '1000000001',
|
username: '1000000001',
|
||||||
password: '1234',
|
password: '1234',
|
||||||
|
|
@ -99,7 +99,7 @@ const KioskBookingPage = () => {
|
||||||
);
|
);
|
||||||
|
|
||||||
const { token } = response?.data;
|
const { token } = response?.data;
|
||||||
sessionStorage.setItem('auth', token);
|
sessionStore('auth', token);
|
||||||
sessionStore('LoginType', 'Kiosk');
|
sessionStore('LoginType', 'Kiosk');
|
||||||
}
|
}
|
||||||
const MACAddress = queryParams?.MD;
|
const MACAddress = queryParams?.MD;
|
||||||
|
|
@ -119,7 +119,7 @@ const KioskBookingPage = () => {
|
||||||
let responseData = deviceMACResponse?.data?.data?.[0];
|
let responseData = deviceMACResponse?.data?.data?.[0];
|
||||||
responseData?.accessToken != null &&
|
responseData?.accessToken != null &&
|
||||||
responseData?.accessToken != undefined &&
|
responseData?.accessToken != undefined &&
|
||||||
sessionStorage.setItem('auth', responseData?.accessToken);
|
sessionStore('auth', responseData?.accessToken);
|
||||||
responseData?.MobileNo != null &&
|
responseData?.MobileNo != null &&
|
||||||
responseData?.MobileNo != undefined &&
|
responseData?.MobileNo != undefined &&
|
||||||
sessionStore('MobileNo', responseData?.MobileNo);
|
sessionStore('MobileNo', responseData?.MobileNo);
|
||||||
|
|
|
||||||
|
|
@ -12,7 +12,7 @@ const url_string = window.location.href;
|
||||||
const url = new URL(url_string);
|
const url = new URL(url_string);
|
||||||
const codesParam = url.searchParams.get('code');
|
const codesParam = url.searchParams.get('code');
|
||||||
|
|
||||||
import { sessionStore } from '../../../Services/Others';
|
import { getSession, sessionStore } from '../../../Services/Others';
|
||||||
import Loader from '../../../Components/Loader/Loader';
|
import Loader from '../../../Components/Loader/Loader';
|
||||||
import { useDispatch } from 'react-redux';
|
import { useDispatch } from 'react-redux';
|
||||||
import {
|
import {
|
||||||
|
|
@ -85,7 +85,7 @@ function CustomerPurchaseConfirm() {
|
||||||
};
|
};
|
||||||
|
|
||||||
const addSession = async () => {
|
const addSession = async () => {
|
||||||
if (!sessionStorage.getItem('auth')) {
|
if (!getSession('auth')) {
|
||||||
try {
|
try {
|
||||||
const data = { username: '1000000001', password: '1234' };
|
const data = { username: '1000000001', password: '1234' };
|
||||||
const response = await axios.post(
|
const response = await axios.post(
|
||||||
|
|
@ -102,7 +102,7 @@ function CustomerPurchaseConfirm() {
|
||||||
const { token } = response?.data;
|
const { token } = response?.data;
|
||||||
|
|
||||||
if (token) {
|
if (token) {
|
||||||
sessionStorage.setItem('auth', token);
|
sessionStore('auth', token);
|
||||||
sessionStore('LoginType', 'Kiosk');
|
sessionStore('LoginType', 'Kiosk');
|
||||||
}
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
|
|
|
||||||
|
|
@ -11,7 +11,7 @@ import { jsPDF } from "jspdf";
|
||||||
import html2canvas from "html2canvas";
|
import html2canvas from "html2canvas";
|
||||||
|
|
||||||
import { PublicQrCodeget } from "../../../Features/ConfigMasterPage/ConfigMasterPage";
|
import { PublicQrCodeget } from "../../../Features/ConfigMasterPage/ConfigMasterPage";
|
||||||
import { decryptToObject } from "../../../Services/Others";
|
import { decryptToObject, getSession } from "../../../Services/Others";
|
||||||
import { getBranchDetail } from "../../../Features/BrachLogin/BranchLogin";
|
import { getBranchDetail } from "../../../Features/BrachLogin/BranchLogin";
|
||||||
import "./PurchaseLink.scss";
|
import "./PurchaseLink.scss";
|
||||||
|
|
||||||
|
|
@ -33,13 +33,13 @@ const PurchaseLink = () => {
|
||||||
|
|
||||||
const initializeData = async () => {
|
const initializeData = async () => {
|
||||||
try {
|
try {
|
||||||
if (!sessionStorage.getItem("auth")) {
|
if (!getSession("auth")) {
|
||||||
const { data } = await axios.post(`${apiUrlToken}/jwtTokenGenerator`, {
|
const { data } = await axios.post(`${apiUrlToken}/jwtTokenGenerator`, {
|
||||||
username: "1000000001",
|
username: "1000000001",
|
||||||
password: "1234",
|
password: "1234",
|
||||||
});
|
});
|
||||||
sessionStorage.setItem("auth", data.token);
|
sessionStore("auth", data.token);
|
||||||
sessionStorage.setItem("LoginType", "Public");
|
sessionStore("LoginType", "Public");
|
||||||
}
|
}
|
||||||
|
|
||||||
const qrData = await dispatch(PublicQrCodeget(codesParam)).unwrap();
|
const qrData = await dispatch(PublicQrCodeget(codesParam)).unwrap();
|
||||||
|
|
|
||||||
|
|
@ -2,7 +2,7 @@ import { useEffect, useRef, useState } from "react";
|
||||||
import axios from "axios";
|
import axios from "axios";
|
||||||
import { Phone, Mail, Globe, MapPin, Facebook, Instagram } from "lucide-react";
|
import { Phone, Mail, Globe, MapPin, Facebook, Instagram } from "lucide-react";
|
||||||
import "./MenuQRCode.scss";
|
import "./MenuQRCode.scss";
|
||||||
import { decryptToObject, sessionStore } from "../../../Services/Others";
|
import { decryptToObject, getSession, sessionStore } from "../../../Services/Others";
|
||||||
import { useDispatch } from "react-redux";
|
import { useDispatch } from "react-redux";
|
||||||
import {
|
import {
|
||||||
PublicQrCodeget,
|
PublicQrCodeget,
|
||||||
|
|
@ -94,13 +94,13 @@ const CustomerMenuPage = () => {
|
||||||
};
|
};
|
||||||
|
|
||||||
const addSession = async () => {
|
const addSession = async () => {
|
||||||
if (!sessionStorage.getItem("auth")) {
|
if (!getSession("auth")) {
|
||||||
try {
|
try {
|
||||||
const { data } = await axios.post(`${apiUrlToken}/jwtTokenGenerator`, {
|
const { data } = await axios.post(`${apiUrlToken}/jwtTokenGenerator`, {
|
||||||
username: "1000000001",
|
username: "1000000001",
|
||||||
password: "1234",
|
password: "1234",
|
||||||
});
|
});
|
||||||
sessionStorage.setItem("auth", data.token);
|
sessionStore("auth", data.token);
|
||||||
sessionStore("LoginType");
|
sessionStore("LoginType");
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error("Session Error:", error);
|
console.error("Session Error:", error);
|
||||||
|
|
|
||||||
|
|
@ -166,7 +166,7 @@ const SelfBooking = () => {
|
||||||
dispatch(changeSelectTable([]));
|
dispatch(changeSelectTable([]));
|
||||||
dispatch(changeSelectChair([]));
|
dispatch(changeSelectChair([]));
|
||||||
const token = await generateToken();
|
const token = await generateToken();
|
||||||
sessionStorage.setItem('auth', token);
|
sessionStore('auth', token);
|
||||||
|
|
||||||
const qrResponse = await dispatch(
|
const qrResponse = await dispatch(
|
||||||
PublicQrCodepost(SelfBookingId)
|
PublicQrCodepost(SelfBookingId)
|
||||||
|
|
|
||||||
|
|
@ -7,11 +7,12 @@ import store from '../app/store.js';
|
||||||
// import html2pdf from 'html2pdf.js';
|
// import html2pdf from 'html2pdf.js';
|
||||||
|
|
||||||
import html2canvas from 'html2canvas';
|
import html2canvas from 'html2canvas';
|
||||||
|
import { Capacitor } from '@capacitor/core';
|
||||||
|
|
||||||
const cookies = new Cookies();
|
const cookies = new Cookies();
|
||||||
const SECRET_KEY = import.meta.env.ENV_SECRET_KEY;
|
const SECRET_KEY = import.meta.env.ENV_SECRET_KEY;
|
||||||
const URL_SECRET_KEY = import.meta.env.ENV_URL_SECRET_KEY;
|
const URL_SECRET_KEY = import.meta.env.ENV_URL_SECRET_KEY;
|
||||||
|
const isCapacitor = () => Capacitor.isNativePlatform();
|
||||||
export const localstore = (GSTIN) => {
|
export const localstore = (GSTIN) => {
|
||||||
localStorage.setItem('GSTIN', GSTIN);
|
localStorage.setItem('GSTIN', GSTIN);
|
||||||
};
|
};
|
||||||
|
|
@ -34,14 +35,14 @@ export const getLocalStorageValues = (key) => {
|
||||||
return localStorage.getItem(key);
|
return localStorage.getItem(key);
|
||||||
};
|
};
|
||||||
|
|
||||||
export const sessionStore = (key, value) => {
|
// export const sessionStore = (key, value) => {
|
||||||
const encryptedValue = CryptoJS.AES.encrypt(
|
// const encryptedValue = CryptoJS.AES.encrypt(
|
||||||
JSON.stringify(value),
|
// JSON.stringify(value),
|
||||||
SECRET_KEY
|
// SECRET_KEY
|
||||||
).toString();
|
// ).toString();
|
||||||
|
|
||||||
if (encryptedValue) sessionStorage.setItem(key, encryptedValue);
|
// if (encryptedValue) sessionStorage.setItem(key, encryptedValue);
|
||||||
};
|
// };
|
||||||
|
|
||||||
export const encryptedValuesFun = (value) => {
|
export const encryptedValuesFun = (value) => {
|
||||||
const encryptedValue = CryptoJS.AES.encrypt(
|
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 getCapCookieData = (key) => {
|
||||||
const val = sessionStorage.getItem(key);
|
const match = document.cookie
|
||||||
if (val) {
|
.split("; ")
|
||||||
const decrypted = CryptoJS.AES.decrypt(val, SECRET_KEY).toString(
|
.find((row) => row.startsWith(`${key}=`));
|
||||||
CryptoJS.enc.Utf8
|
return match ? decodeURIComponent(match.split("=")[1]) : null;
|
||||||
);
|
};
|
||||||
|
|
||||||
if (decrypted) {
|
export const sessionStore = (key, value) => {
|
||||||
return JSON.parse(decrypted);
|
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) => {
|
export const clearSession = (key) => {
|
||||||
const val = sessionStorage.getItem(key);
|
const val = sessionStorage.getItem(key);
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue