-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
151 lines (131 loc) · 4.94 KB
/
main.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
const { app, BrowserWindow, ipcMain } = require('electron');
const axios = require('axios');
const http = require('http');
const path = require('path');
const fs = require('fs');
let redirectUriWindow;
let savedRequestToken;
function createWindow() {
const win = new BrowserWindow({
width: 800,
height: 600,
fullscreen: true,
webPreferences: {
nodeIntegration: false,
contextIsolation: true,
preload: path.join(__dirname, 'preload.js'),
},
});
// Read response.json file
const responseFilePath = path.join(__dirname, 'response.json');
let response;
try {
response = JSON.parse(fs.readFileSync(responseFilePath, 'utf-8'));
} catch (error) {
response = null;
}
if (response && response.access_token_response && response.access_token_response.access_token && response.access_token_response.username) {
// Access token and username present in response.json
win.loadFile('home_page.html');
} else {
// Access token and username not present, proceed with authentication flow
win.loadFile('index.html');
}
redirectUriWindow = new BrowserWindow({
show: false,
webPreferences: {
nodeIntegration: false,
contextIsolation: true,
},
});
const server = http.createServer((req, res) => {
const redirectUriFilePath = path.join(__dirname, 'redirect_uri.html');
res.writeHead(200, { 'Content-Type': 'text/html' });
res.end(fs.readFileSync(redirectUriFilePath, 'utf-8'));
});
redirectUriWindow.webContents.on('did-navigate', (event, url) => {
if (url.startsWith('http://localhost:8081/redirect_uri.html')) {
redirectUriWindow.webContents.send('requestToken', savedRequestToken);
redirectUriWindow.close();
}
});
server.listen(8081, 'localhost');
// Handle the authenticate event from the renderer process
ipcMain.handle('authenticate', async (event) => {
try {
const redirectUri = `http://localhost:8081/redirect_uri.html`; // Specify the redirect_uri here
const response = await axios.post('http://127.0.0.1:8080/authenticate', { redirect_uri: redirectUri });
console.log(response.data);
const requestToken = response.data.request_token.code;
savedRequestToken = requestToken;
const redirectUrl = `http://localhost:8081/redirect_uri.html?request_token=${requestToken}`;
console.log('Request Token:', requestToken);
console.log('Redirect URI:', redirectUrl);
// Create a new BrowserWindow for the authorization URL
const authWindow = new BrowserWindow({
show: false,
webPreferences: {
nodeIntegration: false,
contextIsolation: true,
},
});
authWindow.loadURL(`https://getpocket.com/auth/authorize?request_token=${requestToken}&redirect_uri=${redirectUrl}`);
// Handle the navigation in the authWindow
authWindow.webContents.on('did-navigate', (event, url) => {
if (url.startsWith('http://localhost:8081/redirect_uri.html')) {
const requestToken = url.substring(url.indexOf('=') + 1);
win.webContents.send('requestToken', requestToken);
authWindow.close();
}
});
// Show the authWindow when it finishes loading
authWindow.webContents.on('did-finish-load', () => {
authWindow.show();
});
} catch (error) {
console.error(error);
}
});
// Handle the saveAccessToken event from the renderer process
ipcMain.handle('saveAccessToken', async (event, requestToken) => {
try {
// Make the save-access-token API call with the requestToken
const response = await axios.post('http://127.0.0.1:8080/save-access-token', { request_token: requestToken });
console.log('save-access-token response:', response.data);
// Return the response to the renderer process
return response.data;
} catch (error) {
console.error(error);
throw error;
}
});
ipcMain.on('saveResponseToStorage', (event, response) => {
try {
// Implement your code to save the response to permanent storage here
// For example, you can use the Node.js File System module to write the response to a file
const filePath = 'response.json';
fs.writeFileSync(filePath, JSON.stringify(response));
console.log('Response saved to storage:', response);
} catch (error) {
console.error('Error saving response to storage:', error);
}
});
// Handle the deleteResponseFile event from the renderer process
ipcMain.on('deleteResponseFile', (event) => {
const responseFile = path.join(__dirname, 'response.json');
fs.unlink(responseFile, (err) => {
if (err) {
console.error(err);
event.sender.send('responseFileDeleted', { success: false, error: err });
return;
}
event.sender.send('responseFileDeleted', { success: true });
});
});
}
app.whenReady().then(createWindow);
app.on('window-all-closed', () => {
if (process.platform !== 'darwin') {
app.quit();
}
});