Skip to content

Commit 4a06181

Browse files
committed
send check master key to win because it have still stayed in state
1 parent 5b3b0a0 commit 4a06181

File tree

13 files changed

+82
-49
lines changed

13 files changed

+82
-49
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "electron-gp",
33
"private": true,
4-
"version": "0.2.15",
4+
"version": "0.2.16",
55
"type": "module",
66
"main": "dist-main/app.js",
77
"author": "traeop",

src/main/app/window.ts

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import { AuthService } from "../auth/service.js";
99
import { ControlUpdateWindowsPlatformService } from "../updater/services/windows/control-update.js";
1010
import { TrayService } from "../tray/service.js";
1111
import { destroyWindows } from "../@core/control-window/destroy.js";
12-
import { ipcWebContentsSend, isDev } from "../$shared/utils.js";
12+
import { ipcMainOn, ipcWebContentsSend, isDev } from "../$shared/utils.js";
1313
import { menu } from "../config.js";
1414

1515
@WindowManager<TWindows["main"]>({
@@ -48,6 +48,7 @@ export class AppWindow implements TWindowManager {
4848
this.buildMenu(window);
4949
this.buildTray(window);
5050
this.checkAuthenticated(window);
51+
this.ipcCheckSync(window);
5152
this.authService.setCheckAccessInterval(window);
5253

5354
const userId = getElectronStorage("userId");
@@ -58,14 +59,25 @@ export class AppWindow implements TWindowManager {
5859
}
5960
}
6061

61-
private async checkAuthenticated(window: BrowserWindow) {
62-
const result = await this.authService.checkAuthenticated(window);
63-
ipcWebContentsSend("authSocialNetwork", window.webContents, {
64-
isAuthenticated:
65-
result !== undefined && result.isAuthenticated !== undefined,
62+
private ipcCheckSync(window: BrowserWindow): void {
63+
ipcMainOn("sync", (event) => {
64+
const result = this.authService.checkAuthenticated(window);
65+
66+
event.reply("sync", {
67+
isAuthenticated: result !== undefined && result.isAuthenticated,
68+
});
6669
});
6770
}
6871

72+
private async checkAuthenticated(window: BrowserWindow) {
73+
const result = this.authService.checkAuthenticated(window);
74+
if (result !== undefined && result.isAuthenticated !== undefined) {
75+
ipcWebContentsSend("authSocialNetwork", window.webContents, {
76+
isAuthenticated: result.isAuthenticated,
77+
});
78+
}
79+
}
80+
6981
private buildTray(window: BrowserWindow): void {
7082
this.trayService.buildTray(
7183
this.trayService.trayMenu.map((item) => {

src/main/auth/service.ts

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { RestApiService } from "../rest-api/service.js";
66
import {
77
deleteFromElectronStorage,
88
getElectronStorage,
9+
deleteStore,
910
} from "../$shared/store.js";
1011
import { ipcWebContentsSend } from "../$shared/utils.js";
1112
import { getWindow as getWindows } from "../@core/control-window/receive.js";
@@ -47,9 +48,9 @@ export class AuthService {
4748
return response.data;
4849
}
4950

50-
async checkAuthenticated(
51+
checkAuthenticated(
5152
window: BrowserWindow
52-
): Promise<{ isAuthenticated: boolean } | undefined> {
53+
): { isAuthenticated: boolean } | undefined {
5354
const cacheAccess = this.cacheAccess();
5455
if (cacheAccess !== undefined) {
5556
ipcWebContentsSend("sync", window.webContents, {
@@ -69,19 +70,9 @@ export class AuthService {
6970
});
7071

7172
const response = await this.access();
72-
if (response !== undefined) {
73-
ipcWebContentsSend("sync", window.webContents, {
74-
isAuthenticated: response.ok,
75-
});
76-
77-
return {
78-
isAuthenticated: true,
79-
};
80-
} else {
81-
ipcWebContentsSend("sync", window.webContents, {
82-
isAuthenticated: true,
83-
});
84-
}
73+
ipcWebContentsSend("sync", window.webContents, {
74+
isAuthenticated: (response !== undefined && response.ok) || true,
75+
});
8576

8677
const authToken = getElectronStorage("authToken");
8778
if (authToken === undefined) {
@@ -112,6 +103,7 @@ export class AuthService {
112103
deleteFromElectronStorage("authToken");
113104
deleteFromElectronStorage("response");
114105
deleteFromElectronStorage("userId");
106+
deleteStore("masterKey");
115107
deleteFromElectronStorage("twoFactorSecret");
116108
ipcWebContentsSend("authSocialNetwork", window.webContents, {
117109
isAuthenticated: false,

src/main/master-key/ipc.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ export class MasterKeyIpc {
2929
this.masterKeyWindow = await masterKeyWindow.create();
3030
});
3131

32-
this.ipcGetMasterKey();
32+
this.ipcCheckMasterKey();
3333
this.ipcPostMasterKey();
3434
this.ipcDeleteMasterKey();
3535
this.ipcCopyMasterKey();
@@ -98,7 +98,7 @@ export class MasterKeyIpc {
9898
});
9999
}
100100

101-
private ipcGetMasterKey(): void {
101+
private ipcCheckMasterKey(): void {
102102
ipcMainOn("checkMasterKey", (event) => {
103103
const masterKey = getStore("masterKey");
104104

src/main/master-key/window.ts

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import { BrowserWindow } from "electron";
22
import { WindowManager } from "../@core/decorators/window-manager.js";
33
import { TWindowManager } from "../@core/types/window-manager.js";
4+
import { ipcWebContentsSend } from "../$shared/utils.js";
5+
import { getStore } from "../$shared/store.js";
46

57
@WindowManager<TWindows["masterKey"]>({
68
hash: "window:master-key",
@@ -16,9 +18,20 @@ import { TWindowManager } from "../@core/types/window-manager.js";
1618
},
1719
})
1820
export class MasterKeyWindow implements TWindowManager {
21+
isSync = false;
1922
constructor() {}
2023

21-
onDidFinishLoad(window: BrowserWindow): void {
22-
// window.webContents.openDevTools();
24+
onDidFinishLoad(): void {
25+
this.isSync = true;
26+
}
27+
28+
onShow(window: BrowserWindow): void {
29+
if (this.isSync) {
30+
const masterKey = getStore("masterKey");
31+
32+
ipcWebContentsSend("masterKey", window.webContents, {
33+
isMasterKey: Boolean(masterKey),
34+
});
35+
}
2336
}
2437
}

src/main/preload.cts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,9 @@ electron.contextBridge.exposeInMainWorld("electron", {
5656
checkUser: () => {
5757
ipcSend("checkUser");
5858
},
59+
sync: () => {
60+
ipcSend("sync");
61+
},
5962
windowAuthSocialNetwork: (payload) => {
6063
ipcSend("authSocialNetwork", payload);
6164
},

src/main/two-factor/ipc.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -88,9 +88,6 @@ export class TwoFactorIpc {
8888
ipcWebContentsSend("authSocialNetwork", mainWindow.webContents, {
8989
isAuthenticated: true,
9090
});
91-
ipcWebContentsSend("sync", mainWindow.webContents, {
92-
isAuthenticated: true,
93-
});
9491
this.authService.setCheckAccessInterval(mainWindow);
9592
}
9693
}

src/renderer/ui-business/MasterKey/hooks/useIpc.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@ export const useIpc = () => {
55
const { setMasterKey } = useControlContextActions();
66

77
useEffect(() => {
8-
window.electron.receive.subscribeMasterKey(({ isMasterKey }) => {
9-
setMasterKey(isMasterKey);
10-
});
8+
window.electron.send.checkMasterKey();
119
}, []);
1210

1311
useEffect(() => {
14-
window.electron.send.checkMasterKey();
12+
window.electron.receive.subscribeMasterKey(({ isMasterKey }) => {
13+
setMasterKey(isMasterKey);
14+
});
1515
}, []);
1616
};

src/renderer/ui-business/Sync/hooks/useIpc.ts

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,26 @@ export const useIpc = () => {
55
const { setUser, setAuthenticated, setResources } =
66
useControlContextActions();
77

8+
useEffect(() => {
9+
window.electron.send.sync();
10+
}, []);
11+
812
useEffect(() => {
913
const unSub = window.electron.receive.subscribeSync(
1014
({ isAuthenticated, isResources, isUser }) => {
11-
setAuthenticated(isAuthenticated);
15+
setAuthenticated((prevValue) => {
16+
if (prevValue === undefined) {
17+
return isAuthenticated;
18+
}
19+
20+
if (prevValue !== undefined && isAuthenticated === undefined) {
21+
return prevValue;
22+
}
23+
24+
if (isAuthenticated !== undefined && prevValue !== undefined) {
25+
return isAuthenticated;
26+
}
27+
});
1228
setUser((prevValue) => {
1329
if (prevValue === undefined) {
1430
return isUser;

src/renderer/windows/home/components/Home.tsx

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import { lazy, Suspense } from "react";
22
import Box from "@mui/material/Box";
3-
import { Provider as ProviderUser } from "@ui-business/User";
43
import {
54
useIpc as useIpcMasterKey,
65
Provider as ProviderMasterKey,
@@ -18,19 +17,17 @@ const Home = () => {
1817

1918
return (
2019
<ProviderSync>
21-
<ProviderUser>
22-
<Suspense fallback={<LoadingSpinner />}>
23-
<LazyTopPanel isMasterKey={isMasterKey} />
24-
</Suspense>
20+
<Suspense fallback={<LoadingSpinner />}>
21+
<LazyTopPanel isMasterKey={isMasterKey} />
22+
</Suspense>
2523

26-
<ProviderMasterKey>
27-
<Box sx={{ mt: 6, width: "100%" }}>
28-
<Suspense fallback={<LoadingSpinner />}>
29-
<LazyResources isMasterKey={isMasterKey} />
30-
</Suspense>
31-
</Box>
32-
</ProviderMasterKey>
33-
</ProviderUser>
24+
<ProviderMasterKey>
25+
<Box sx={{ mt: 6, width: "100%" }}>
26+
<Suspense fallback={<LoadingSpinner />}>
27+
<LazyResources isMasterKey={isMasterKey} />
28+
</Suspense>
29+
</Box>
30+
</ProviderMasterKey>
3431
</ProviderSync>
3532
);
3633
};

0 commit comments

Comments
 (0)