async function startServerTS() {
const msalInstance =
await PublicClientApplication.createPublicClientApplication(msalConfig);
if (resultEl) {
await stopCurrentServer();
try {
// Start the OAuth server
const port = await start();
currentPort = port;
isRustServer = false;
resultEl.textContent = OAuth slerver started on port ${port};
const unlistenUrl = await onUrl(async (url: string) => {
console.log("Received OAuth URL:", url);
try {
const response = await msalInstance.handleRedirectPromise();
if (response) {
console.log("Authentication successful:", response);
resultEl!.textContent += `\nAuthentication successful: ${response}`;
} else {
console.warn("No response");
}
} catch (error) {
console.error("Error", error);
} finally {
await stopCurrentServer();
}
});
const unlistenInvalidUrl = await onInvalidUrl((error: any) => {
resultEl!.textContent += `\nReceived invalid OAuth URL: ${error}`;
});
const redirectRequest: RedirectRequest = {
scopes: ["openid", "profile", "User.Read"],
redirectUri: `http://localhost:${port}`,
};
try {
console.log("Calling loginRedirect...");
await msalInstance.loginRedirect(redirectRequest);
} catch (error) {
console.error("Login redirect failed:", error);
}
// Store unlisten functions to call them when stopping the server
(window as any).unlistenFunctions = [unlistenUrl, unlistenInvalidUrl];
} catch (error) {
resultEl.textContent = `Error starting OAuth server (TypeScript): ${error}`;
}
}
}