Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 45 additions & 22 deletions apps/x/apps/main/src/auth-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export function createAuthServer(
onCallback: (callbackUrl: URL) => void | Promise<void>
): Promise<AuthServerResult> {
return new Promise((resolve, reject) => {
const server = createServer((req, res) => {
const server = createServer(async (req, res) => {
if (!req.url) {
res.writeHead(400);
res.end('Bad Request');
Expand Down Expand Up @@ -64,27 +64,51 @@ export function createAuthServer(
return;
}

// Handle callback - pass full URL so params like iss (OpenID Connect) are preserved for token exchange
onCallback(url);
try {
// Handle callback - pass full URL so params like iss (OpenID Connect)
// are preserved for token exchange.
await onCallback(url);

res.writeHead(200, { 'Content-Type': 'text/html' });
res.end(`
<!DOCTYPE html>
<html>
<head>
<title>Authorization Successful</title>
<style>
body { font-family: Arial, sans-serif; text-align: center; padding: 50px; }
.success { color: #2e7d32; }
</style>
</head>
<body>
<h1 class="success">Authorization Successful</h1>
<p>You can close this window.</p>
<script>setTimeout(() => window.close(), 2000);</script>
</body>
</html>
`);
res.writeHead(200, { 'Content-Type': 'text/html' });
res.end(`
<!DOCTYPE html>
<html>
<head>
<title>Authorization Successful</title>
<style>
body { font-family: Arial, sans-serif; text-align: center; padding: 50px; }
.success { color: #2e7d32; }
</style>
</head>
<body>
<h1 class="success">Authorization Successful</h1>
<p>You can close this window.</p>
<script>setTimeout(() => window.close(), 2000);</script>
</body>
</html>
`);
} catch (callbackError) {
const message = callbackError instanceof Error ? callbackError.message : String(callbackError);
res.writeHead(200, { 'Content-Type': 'text/html' });
res.end(`
<!DOCTYPE html>
<html>
<head>
<title>OAuth Error</title>
<style>
body { font-family: Arial, sans-serif; text-align: center; padding: 50px; }
.error { color: #d32f2f; }
</style>
</head>
<body>
<h1 class="error">Authorization Failed</h1>
<p>Error: ${escapeHtml(message)}</p>
<p>You can close this window.</p>
<script>setTimeout(() => window.close(), 3000);</script>
</body>
</html>
`);
}
} else {
res.writeHead(404);
res.end('Not Found');
Expand All @@ -104,4 +128,3 @@ export function createAuthServer(
});
});
}