-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnotion-auth.js
More file actions
63 lines (54 loc) · 2.05 KB
/
notion-auth.js
File metadata and controls
63 lines (54 loc) · 2.05 KB
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
const axios = require('axios');
const { ipcMain } = require('electron');
let authorizationCode = null;
async function handleAuthCallback(url) {
console.log('handleAuthCallback called with URL:', url);
const urlObj = new URL(url);
const code = urlObj.searchParams.get('code');
const error = urlObj.searchParams.get('error');
if (error) {
console.error('OAuth error:', error);
ipcMain.emit('auth-error', null, error);
return;
}
if (code) {
console.log('Authorization code received:', code);
authorizationCode = code;
ipcMain.emit('auth-success', null, code);
} else {
console.log('No code found in URL, waiting for redirect');
// Don't emit an error here, as this might be an intermediate step
}
}
async function exchangeCodeForToken() {
console.log('Exchanging code for token');
if (!process.env.NOTION_CLIENT_ID || !process.env.NOTION_CLIENT_SECRET) {
throw new Error('Notion credentials are not properly configured');
}
if (!authorizationCode) {
throw new Error('No authorization code available');
}
try {
const response = await axios.post('https://api.notion.com/v1/oauth/token', {
grant_type: 'authorization_code',
code: authorizationCode,
redirect_uri: 'http://localhost:3000/callback'
}, {
auth: {
username: process.env.NOTION_CLIENT_ID,
password: process.env.NOTION_CLIENT_SECRET
},
headers: {
'Content-Type': 'application/json',
'Access-Control-Allow-Origin': '*'
}
});
console.log('Token exchange successful');
authorizationCode = null; // Clear the code after successful exchange
return response.data.access_token;
} catch (error) {
console.error('Error exchanging code for token:', error.response ? error.response.data : error.message);
throw error;
}
}
module.exports = { handleAuthCallback, exchangeCodeForToken };