LogOut Fixed
This commit is contained in:
parent
ec383c109f
commit
e2b0561251
File diff suppressed because it is too large
Load Diff
|
|
@ -21,6 +21,7 @@
|
|||
"@tailwindcss/vite": "^4.1.16",
|
||||
"axios": "^1.13.2",
|
||||
"electron-store": "^11.0.2",
|
||||
"get-windows": "^9.2.3",
|
||||
"lucide-react": "^0.552.0",
|
||||
"os-utils": "^0.0.14",
|
||||
"react": "^18.3.1",
|
||||
|
|
|
|||
|
|
@ -2,19 +2,21 @@
|
|||
import axios, { AxiosInstance, AxiosRequestConfig, AxiosResponse } from 'axios';
|
||||
import { getToken } from '../utils/tokenHelper.js';
|
||||
|
||||
const TOKEN_KEY = 'auth_token';
|
||||
const TOKEN_KEY = 'ACCESS_TOKEN';
|
||||
|
||||
const api: AxiosInstance = axios.create({
|
||||
baseURL: 'https://wired-terminally-muskrat.ngrok-free.app/api/v1', // 🔁 your backend
|
||||
timeout: 10000,
|
||||
baseURL: 'https://wired-terminally-muskrat.ngrok-free.app/api/v1/va', // 🔁 your backend
|
||||
// timeout: 10000,
|
||||
});
|
||||
|
||||
// --- Request Interceptor ---
|
||||
api.interceptors.request.use(
|
||||
async (config: AxiosRequestConfig): Promise<any> => {
|
||||
const token = await getToken(TOKEN_KEY);
|
||||
if (token && config.headers) {
|
||||
config.headers.token = token;
|
||||
config.headers = config.headers || {}
|
||||
|
||||
if (token) {
|
||||
config.headers['token'] = token;
|
||||
config.headers['ngrok-skip-browser-warning'] = 98098;
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -4,11 +4,11 @@ import { getToken, saveToken, clearToken } from '../utils/tokenHelper.js';
|
|||
import { handleLogin, takeScreenshot } from '../utils/utilityFunctions.js';
|
||||
import api from '../api/axiosClient.js';
|
||||
|
||||
ipcMain.handle('get-token', async (_, TOKEN_KEY: string) => {
|
||||
ipcMain.handle('get-token', async (_, TOKEN_KEY: "ACCESS_TOKEN" | "REFRESH_TOKEN") => {
|
||||
return await getToken(TOKEN_KEY);
|
||||
});
|
||||
|
||||
ipcMain.handle('save-token', async (_, TOKEN_KEY: string, token: string) => {
|
||||
ipcMain.handle('save-token', async (_, TOKEN_KEY: "ACCESS_TOKEN" | "REFRESH_TOKEN", token: string) => {
|
||||
await saveToken(TOKEN_KEY, token);
|
||||
return true;
|
||||
});
|
||||
|
|
@ -29,8 +29,15 @@ ipcMain.handle('login-request', async (event, credentials: { email: string, pass
|
|||
});
|
||||
|
||||
ipcMain.handle('logout-request', async () => {
|
||||
(async () => {
|
||||
try {
|
||||
console.log("Handle Logout listened")
|
||||
await api.get("/logout");
|
||||
})()
|
||||
console.log("Token", await getToken("ACCESS_TOKEN"))
|
||||
const res = await api.get("/logout");
|
||||
if(res && ("success" in res) && res.success) {
|
||||
return {success: true, message: "Logout Successfully"}
|
||||
}
|
||||
return {success: false, message: ""}
|
||||
} catch(error) {
|
||||
return {success: false, message: ""}
|
||||
}
|
||||
});
|
||||
|
|
@ -11,6 +11,12 @@ app.on('ready', async () => {
|
|||
webPreferences: {
|
||||
preload: getPreloadPath(),
|
||||
},
|
||||
|
||||
resizable: false, // disables resizing (no maximize)
|
||||
minimizable: false, // disables minimize button
|
||||
maximizable: false, // disables maximize button
|
||||
closable: true,
|
||||
alwaysOnTop: true,
|
||||
// disables default system frame (dont do this if you want a proper working menu bar)
|
||||
frame: process.platform === 'darwin' ? false : true,
|
||||
autoHideMenuBar: true
|
||||
|
|
@ -30,19 +36,19 @@ app.on('ready', async () => {
|
|||
return getStaticData();
|
||||
});
|
||||
|
||||
ipcMainOn('sendFrameAction', (payload) => {
|
||||
switch (payload) {
|
||||
case 'CLOSE':
|
||||
mainWindow.close();
|
||||
break;
|
||||
case 'MAXIMIZE':
|
||||
mainWindow.maximize();
|
||||
break;
|
||||
case 'MINIMIZE':
|
||||
mainWindow.minimize();
|
||||
break;
|
||||
}
|
||||
});
|
||||
// ipcMainOn('sendFrameAction', (payload) => {
|
||||
// switch (payload) {
|
||||
// case 'CLOSE':
|
||||
// mainWindow.close();
|
||||
// break;
|
||||
// case 'MAXIMIZE':
|
||||
// mainWindow.maximize();
|
||||
// break;
|
||||
// case 'MINIMIZE':
|
||||
// mainWindow.minimize();
|
||||
// break;
|
||||
// }
|
||||
// });
|
||||
|
||||
// createTray(mainWindow);
|
||||
handleCloseEvents(mainWindow);
|
||||
|
|
|
|||
|
|
@ -0,0 +1,32 @@
|
|||
import { contextBridge, ipcRenderer } from "electron";
|
||||
console.log("setting PreLoad")
|
||||
|
||||
|
||||
contextBridge.exposeInMainWorld('electron', {
|
||||
login: (credentials: { email: string, password: string }) => ipcRenderer.invoke('login-request', credentials),
|
||||
logout: () => ipcRenderer.invoke('logout-request'),
|
||||
getToken: (TOKEN_KEY: "ACCESS_TOKEN" | "REFRESH_TOKEN") => ipcRenderer.invoke('get-token', TOKEN_KEY),
|
||||
uploadScreenshot: () => ipcRenderer.invoke('upload-screenshot'),
|
||||
} satisfies Window['electron']);
|
||||
|
||||
// function ipcInvoke<Key extends keyof EventPayloadMapping>(
|
||||
// key: Key
|
||||
// ): Promise<EventPayloadMapping[Key]> {
|
||||
// return ipcRenderer.invoke(key);
|
||||
// }
|
||||
|
||||
// function ipcOn<Key extends keyof EventPayloadMapping>(
|
||||
// key: Key,
|
||||
// callback: (payload: EventPayloadMapping[Key]) => void
|
||||
// ) {
|
||||
// const cb = (_: Electron.IpcRendererEvent, payload: any) => callback(payload);
|
||||
// ipcRenderer.on(key, cb);
|
||||
// return () => ipcRenderer.off(key, cb);
|
||||
// }
|
||||
|
||||
// function ipcSend<Key extends keyof EventPayloadMapping>(
|
||||
// key: Key,
|
||||
// payload: EventPayloadMapping[Key]
|
||||
// ) {
|
||||
// ipcRenderer.send(key, payload);
|
||||
// }
|
||||
|
|
@ -1,55 +0,0 @@
|
|||
import { contextBridge, ipcRenderer } from "electron";
|
||||
|
||||
const electron = require('electron');
|
||||
|
||||
console.log("setting PreLoad")
|
||||
|
||||
|
||||
contextBridge.exposeInMainWorld('electron', {
|
||||
subscribeStatistics: (callback) =>
|
||||
ipcOn('statistics', (stats) => {
|
||||
callback(stats);
|
||||
}),
|
||||
subscribeChangeView: (callback) =>
|
||||
ipcOn('changeView', (view) => {
|
||||
callback(view);
|
||||
}),
|
||||
getStaticData: () => ipcInvoke('getStaticData'),
|
||||
sendFrameAction: (payload) => ipcSend('sendFrameAction', payload),
|
||||
|
||||
login: (credentials: { email: string, password: string }) => ipcRenderer.invoke('login-request', credentials),
|
||||
|
||||
logout: () => ipcRenderer.invoke('logout-request'),
|
||||
|
||||
getToken: (TOKEN_KEY: string) => ipcRenderer.invoke('get-token', TOKEN_KEY),
|
||||
uploadScreenshot: () => ipcRenderer.invoke('upload-screenshot'),
|
||||
} satisfies Window['electron']);
|
||||
|
||||
function ipcInvoke<Key extends keyof EventPayloadMapping>(
|
||||
key: Key
|
||||
): Promise<EventPayloadMapping[Key]> {
|
||||
return electron.ipcRenderer.invoke(key);
|
||||
}
|
||||
|
||||
function ipcOn<Key extends keyof EventPayloadMapping>(
|
||||
key: Key,
|
||||
callback: (payload: EventPayloadMapping[Key]) => void
|
||||
) {
|
||||
const cb = (_: Electron.IpcRendererEvent, payload: any) => callback(payload);
|
||||
electron.ipcRenderer.on(key, cb);
|
||||
return () => electron.ipcRenderer.off(key, cb);
|
||||
}
|
||||
|
||||
function ipcSend<Key extends keyof EventPayloadMapping>(
|
||||
key: Key,
|
||||
payload: EventPayloadMapping[Key]
|
||||
) {
|
||||
electron.ipcRenderer.send(key, payload);
|
||||
}
|
||||
|
||||
|
||||
contextBridge.exposeInMainWorld('electronAPI', {
|
||||
getToken: (key: string) => ipcRenderer.invoke('get-token', key),
|
||||
saveToken: (key: string, token: string) => ipcRenderer.invoke('save-token', key, token),
|
||||
clearToken: (key: string) => ipcRenderer.invoke('clear-token', key),
|
||||
});
|
||||
|
|
@ -4,14 +4,14 @@ import Store from 'electron-store';
|
|||
|
||||
const store = new Store();
|
||||
|
||||
export async function getToken(TOKEN_KEY: string): Promise<string | null> {
|
||||
export async function getToken(TOKEN_KEY: "ACCESS_TOKEN" | "REFRESH_TOKEN"): Promise<string | null> {
|
||||
const encryptedBase64 = store.get(TOKEN_KEY) as string | undefined;
|
||||
if (!encryptedBase64) return null;
|
||||
const encryptedBuffer = Buffer.from(encryptedBase64, 'base64');
|
||||
return safeStorage.decryptString(encryptedBuffer);
|
||||
}
|
||||
|
||||
export async function saveToken(TOKEN_KEY: string, token: string): Promise<void> {
|
||||
export async function saveToken(TOKEN_KEY: "ACCESS_TOKEN" | "REFRESH_TOKEN", token: string): Promise<void> {
|
||||
const encryptedBuffer = safeStorage.encryptString(token);
|
||||
store.set(TOKEN_KEY, encryptedBuffer.toString('base64'));
|
||||
}
|
||||
|
|
|
|||
|
|
@ -62,7 +62,10 @@ export const handleLogin = async (credentials: { email: string, password: string
|
|||
|
||||
console.log("API CAlled", response)
|
||||
const { accessToken, refreshToken } = response.data;
|
||||
await saveToken(accessToken, refreshToken);
|
||||
|
||||
await saveToken("ACCESS_TOKEN", accessToken);
|
||||
await saveToken("REFRESH_TOKEN", refreshToken);
|
||||
|
||||
return { success: true, user: response.data.user };
|
||||
|
||||
} catch (error) {
|
||||
|
|
|
|||
|
|
@ -101,20 +101,16 @@ const DashboardLayout: React.FC<DashboardLayoutProps> = ({ setUser }) => {
|
|||
}, []);
|
||||
|
||||
const handleLogout = async () => {
|
||||
localStorage.removeItem("user");
|
||||
setUser(null);
|
||||
|
||||
console.log("calling Logout UI")
|
||||
|
||||
|
||||
try {
|
||||
const res = await window.electron.logout()
|
||||
console.log(res)
|
||||
if (res && res.success) {
|
||||
localStorage.removeItem("user");
|
||||
setUser(null);
|
||||
navigate("/login");
|
||||
}
|
||||
} catch (error) {
|
||||
console.log(error)
|
||||
}
|
||||
|
||||
navigate("/login");
|
||||
};
|
||||
|
||||
const toggleSidebar = () => setSidebarOpen(!isSidebarOpen);
|
||||
|
|
|
|||
|
|
@ -25,17 +25,9 @@ type UnsubscribeFunction = () => void;
|
|||
|
||||
interface Window {
|
||||
electron: {
|
||||
subscribeStatistics: (
|
||||
callback: (statistics: Statistics) => void
|
||||
) => UnsubscribeFunction;
|
||||
getStaticData: () => Promise<StaticData>;
|
||||
subscribeChangeView: (
|
||||
callback: (view: View) => void
|
||||
) => UnsubscribeFunction;
|
||||
sendFrameAction: (payload: FrameWindowAction) => void;
|
||||
login: (credentials: { email: string; password: string }) => Promise<any>;
|
||||
logout: () => {};
|
||||
getToken: (tokenKey: string) => Promise<string | null>;
|
||||
logout: () => Promise<any>;
|
||||
getToken: (tokenKey: "ACCESS_TOKEN" | "REFRESH_TOKEN") => Promise<string | null>;
|
||||
uploadScreenshot: () => {};
|
||||
};
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue