-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhackatime-auth.js
More file actions
142 lines (125 loc) · 5.75 KB
/
Copy pathhackatime-auth.js
File metadata and controls
142 lines (125 loc) · 5.75 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
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
// Replace 'YOUR_CLIENT_ID' with the Client ID provided by Hack Club OAuth
const CLIENT_ID = '2ciUev1XVQ1kwX5LMTWGnk0V1kabE8fH9tqAvHcWVTY';
async function updateReviewerLink() {
const email = localStorage.getItem('email');
const link = document.getElementById('reviewer-link');
if (!link) return;
if (!email) { link.style.display = 'none'; return; }
try {
const res = await fetch('/api/is-reviewer', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ email })
});
const { isReviewer } = await res.json();
link.style.display = isReviewer ? 'inline' : 'none';
} catch {
link.style.display = 'none';
}
}
// Standardize the redirect URI to the origin root
const HTREDIRECT_URI = window.location.origin + window.location.pathname; // Keep this one as it matches your dashboard
// When using serverless functions, this is often a relative path on your same domain
const HTBACKEND_TOKEN_EXCHANGE_URL = '/api/hackatime-exchange'; // Replace with your actual deployed function URL
document.getElementById('htlogin-link')?.addEventListener('click', (e) => {
e.preventDefault();
const authUrl = new URL('https://hackatime.hackclub.com/oauth/authorize');
authUrl.searchParams.set('client_id', CLIENT_ID);
authUrl.searchParams.set('redirect_uri', HTREDIRECT_URI);
authUrl.searchParams.set('response_type', 'code');
authUrl.searchParams.set('scope', 'profile read');
window.location.href = authUrl.toString();
});
document.getElementById('htlogout-link')?.addEventListener('click', (e) => {
e.preventDefault();
localStorage.removeItem('htloggedIn');
localStorage.removeItem('htaccessToken');
updateAuthUI();
// Optional: redirect to home or refresh
window.location.reload();
});
function updateAuthUI() {
const htisLoggedIn = localStorage.getItem('htloggedIn') === 'true';
const htloginLink = document.getElementById('htlogin-link');
const htuserStatus = document.getElementById('htuser-status');
const htlogoutLink = document.getElementById('htlogout-link');
const projects = document.getElementById('projects');
const newProject = document.getElementById('new-project');
const prizes = document.getElementById('prizes');
//const projectScript = document.getElementById('project-script');
if (htisLoggedIn) {
if (htloginLink) htloginLink.style.display = 'none';
if (htuserStatus) htuserStatus.style.display = 'inline';
if (htuserStatus) htuserStatus.textContent = 'Hackatime Logged In \n';
//if (userStatus) userStatus.style.color = 'red';
if (htlogoutLink) htlogoutLink.style.display = 'inline';
if (htlogoutLink) htlogoutLink.textContent = 'Logout';
if (projects) projects.href = 'projects2.html';
if (projects) projects.style.display = 'inline';
//if (loadProjects) loadProjects.style.display = 'inline';
if (newProject) {
newProject.textContent = 'New Project';
const userEmail = localStorage.getItem('email') || '';
const formBase = 'https://airtable.com/app9IYnpxO1DtNd97/pagMZ83gw96vY9fVc/form';
newProject.href = userEmail
? `${formBase}?prefill_Email=${encodeURIComponent(userEmail)}`
: formBase;
newProject.target = '_blank';
}
if (prizes) {
prizes.href = 'prizes.html';
prizes.style.display = 'inline';
}
//if (projectScript) projectScript.src = 'projects2.js';
} else {
if (htloginLink) htloginLink.style.display = 'inline';
if (htloginLink) htloginLink.textContent = 'Login: Hackatime';
if (htuserStatus) htuserStatus.style.display = 'none';
if (htlogoutLink) htlogoutLink.style.display = 'none';
if (projects) projects.style.display = 'none';
if (projects) projects.href = '#';
if (prizes) {
prizes.href = '#';
prizes.style.display = 'none';
}
}
updateReviewerLink();
}
window.addEventListener('DOMContentLoaded', () => {
updateAuthUI();
if (localStorage.getItem('htloggedIn') === 'false' && !localStorage.getItem('htaccessToken')){
updateAuthUI();
}
const urlParams = new URLSearchParams(window.location.search);
const code = urlParams.get('code');
const state = urlParams.get('state');
if (code && !localStorage.getItem('htloggedIn')) { // Check for state and prevent re-exchanging
console.log('HT Authorization code received:', code);
HTpostData(code, HTREDIRECT_URI).then(data => {
if (data.success) {
localStorage.setItem('htloggedIn', 'true');
localStorage.setItem('htaccessToken', data.accessToken);
if (typeof handler === 'function') {
handler().then(() => updateReviewerLink());
}
// Clean up URL and redirect/refresh
const cleanUrl = window.location.origin + window.location.pathname;
window.history.replaceState({}, document.title, cleanUrl);
updateAuthUI();
} else {
console.error('Authentication failed:', data.error);
alert('Login failed: ' + (data.error || 'Unknown error'));
}
});
}
});
async function HTpostData(code, redirectUri) {
console.log({code: code});
console.log({redirectUri: redirectUri});
const response = await fetch(HTBACKEND_TOKEN_EXCHANGE_URL, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ code: code, redirect_uri: redirectUri }),
});
return response.json(); // Parse the JSON response from the server
}