Skip to content
Merged
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
45 changes: 32 additions & 13 deletions apps/dashboard-api/src/controllers/auth.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ const {
deleteAccountSchema,
onlyEmailSchema,
resetPasswordSchema,
verifyOtpSchema
verifyOtpSchema,
AppError
} = require("@urbackend/common");
const { emitEvent } = require('../utils/emitEvent');

Expand Down Expand Up @@ -87,20 +88,38 @@ const clearGithubStateCookie = (res) => {
});
};

const OAUTH_FETCH_TIMEOUT_MS = 10000;

const fetchJson = async (url, options, defaultMessage) => {
const response = await fetch(url, options);
const payload = await response.json().catch(() => null);

if (!response.ok) {
const message =
payload?.error_description ||
payload?.error ||
payload?.message ||
defaultMessage;
throw new Error(message);
}
// Prevent OAuth requests from hanging forever
const controller = new AbortController();
const timeout = setTimeout(() => controller.abort(), OAUTH_FETCH_TIMEOUT_MS);
Comment thread
yash-pouranik marked this conversation as resolved.

return payload;
try {
const response = await fetch(url, {
...(options || {}),
signal: controller.signal,
});
const payload = await response.json().catch(() => null);

if (!response.ok) {
const message =
payload?.error_description ||
payload?.error ||
payload?.message ||
defaultMessage;
throw new AppError(message, response.status || 502);
}

return payload;
} catch (err) {
if (err.name === 'AbortError') {
throw new AppError('OAuth request timed out.', 504);
}
throw err;
} finally {
clearTimeout(timeout);
}
};

const exchangeGithubCodeForToken = async ({ code, req }) => {
Expand Down
Loading