forked from prepguides/prepguides.dev
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcallback.html
More file actions
184 lines (159 loc) · 6.29 KB
/
callback.html
File metadata and controls
184 lines (159 loc) · 6.29 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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>GitHub Authentication - PrepGuides.dev</title>
<style>
body {
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
min-height: 100vh;
display: flex;
align-items: center;
justify-content: center;
margin: 0;
padding: 20px;
}
.auth-container {
background: white;
border-radius: 16px;
padding: 40px;
text-align: center;
box-shadow: 0 25px 50px -12px rgba(0, 0, 0, 0.25);
max-width: 500px;
width: 100%;
}
.loading {
display: flex;
flex-direction: column;
align-items: center;
gap: 20px;
}
.spinner {
width: 40px;
height: 40px;
border: 4px solid #f3f3f3;
border-top: 4px solid #6366f1;
border-radius: 50%;
animation: spin 1s linear infinite;
}
@keyframes spin {
0% { transform: rotate(0deg); }
100% { transform: rotate(360deg); }
}
.success {
color: #059669;
}
.error {
color: #dc2626;
}
</style>
</head>
<body>
<div class="auth-container">
<div id="loading-state" class="loading">
<div class="spinner"></div>
<h2>Authenticating with GitHub...</h2>
<p>Please wait while we complete your authentication.</p>
</div>
<div id="success-state" style="display: none;">
<h2 class="success">✅ Authentication Successful!</h2>
<p>You can now close this window and return to the main site.</p>
</div>
<div id="error-state" style="display: none;">
<h2 class="error">❌ Authentication Failed</h2>
<p id="error-message">There was an error during authentication.</p>
</div>
</div>
<script>
// Get OAuth parameters from URL
const urlParams = new URLSearchParams(window.location.search);
const code = urlParams.get('code');
const state = urlParams.get('state');
const error = urlParams.get('error');
const errorDescription = urlParams.get('error_description');
// Handle OAuth callback
async function handleAuthCallback() {
const loadingState = document.getElementById('loading-state');
const successState = document.getElementById('success-state');
const errorState = document.getElementById('error-state');
const errorMessage = document.getElementById('error-message');
try {
// Check for OAuth errors
if (error) {
showError(errorDescription || error);
return;
}
// Check for missing authorization code
if (!code) {
showError('No authorization code received');
return;
}
// Process the OAuth callback
const response = await fetch('/api/auth/github', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
code: code,
state: state
})
});
if (!response.ok) {
const errorData = await response.json();
throw new Error(errorData.message || 'Authentication failed');
}
const tokenData = await response.json();
// Get user info
const userResponse = await fetch('https://api.github.com/user', {
headers: {
'Authorization': `token ${tokenData.access_token}`,
'Accept': 'application/vnd.github.v3+json'
}
});
if (userResponse.ok) {
const userData = await userResponse.json();
// Send success message to parent window
if (window.opener) {
window.opener.postMessage({
type: 'GITHUB_AUTH_SUCCESS',
access_token: tokenData.access_token,
user: userData
}, window.location.origin);
}
loadingState.style.display = 'none';
successState.style.display = 'block';
// Close popup after a short delay
setTimeout(() => {
window.close();
}, 2000);
} else {
throw new Error('Failed to get user information');
}
} catch (error) {
console.error('Auth callback error:', error);
// Send error message to parent window
if (window.opener) {
window.opener.postMessage({
type: 'GITHUB_AUTH_ERROR',
error: error.message
}, window.location.origin);
}
showError(error.message || 'An unexpected error occurred during authentication.');
}
}
function showError(message) {
const loadingState = document.getElementById('loading-state');
const errorState = document.getElementById('error-state');
const errorMessage = document.getElementById('error-message');
loadingState.style.display = 'none';
errorMessage.textContent = message;
errorState.style.display = 'block';
}
// Start the authentication process when the page loads
document.addEventListener('DOMContentLoaded', handleAuthCallback);
</script>
</body>
</html>