-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
146 lines (124 loc) · 4.2 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
const {app, BrowserWindow, ipcMain} = require('electron');
const twitterAPI = require("node-twitter-api");
const path = require('path')
const config = require("./config");
let window
function createWindow() {
// Create the main window.
window = new BrowserWindow({
width: 800,
height: 600,
webPreferences: {
preload: path.join(__dirname, "preload.js"),
contextIsolation: false,
nodeIntegration: true,
webSecurity: false
}
})
window.loadFile('index.html')
// Emitted when the window is closed.
window.on('closed', function () {
// Dereference the window object, usually you would store windows
// in an array if your app supports multi windows, this is the time
// when you should delete the corresponding element.
window = null
})
}
app.on('ready', createWindow)
// Quit when all windows are closed.
app.on('window-all-closed', function () {
// On OS X it is common for applications and their menu bar
// to stay active until the user quits explicitly with Cmd + Q
if (process.platform !== 'darwin') {
app.quit()
}
})
app.on('activate', function () {
// On OS X it's common to re-create a window in the app when the
// dock icon is clicked and there are no other windows open.
if (window === null) {
createWindow()
}
})
//twitter login event
ipcMain.on("do-twitter-login", (event, arg) => {
//initialize twitter api using credentials
const twitter = new twitterAPI({
consumerKey: config.twitter.consumerKey,
consumerSecret: config.twitter.consumerSecret,
callback: config.twitter.callback,
});
let authWindow = new BrowserWindow({
width: 650,
height: 800,
show: false,
parent: window
});
authWindow.center();
authWindow.webContents.on("did-finish-load", function () {
authWindow.show();
});
let closedByUser = true,
isVerified = false;
let auth_verifier,
requestTokenSecret,
requestToken,
accessToken,
accessTokenSecret
/* obtaining requestToken and secret.
using requestToken, we can redirect to authorized url */
twitter.getRequestToken((auth_error, token, tokenSecret) => {
if (auth_error) {
console.log("Error getting OAuth request token : ", JSON.stringify(auth_error));
} else {
requestToken = token;
requestTokenSecret = tokenSecret;
authWindow.loadURL(config.twitter.authUrl + token);
}
});
const handleUrl = function (url) {
//getting verifier from authorized url
let raw_auth_verifier = /oauth_verifier=([^&]*)/.exec(url) || null;
auth_verifier =
raw_auth_verifier && raw_auth_verifier.length > 1
? raw_auth_verifier[1]
: null;
let auth_denied = /denied=([^&]*)/.exec(url) || null;
if (auth_verifier) {
closedByUser = false;
/*From verifier and requestToken, getting oAuthToken and oAuthSecret,
using this token and secret, we can sign in to twitter */
twitter.getAccessToken(
requestToken,
requestTokenSecret,
auth_verifier,
(auth_error, token, secret) => {
if (auth_error) {
console.log(auth_error)
} else {
accessToken = token;
accessTokenSecret = secret;
event.sender.send("twitter-login", {
accessToken,
accessTokenSecret, auth_error
});
}
}
);
isVerified = true;
}
if (auth_denied || isVerified) {
authWindow.close();
}
};
authWindow.webContents.on("will-navigate", (event, url) => {
handleUrl(url);
});
authWindow.on("close", () => {
if (closedByUser) {
event.sender.send("twitter-login", {
error: "Twitter Window Closed By User",
});
}
});
})