diff --git a/src/bot/authProcedure.ts b/src/bot/authProcedure.ts index a895450..6992f41 100644 --- a/src/bot/authProcedure.ts +++ b/src/bot/authProcedure.ts @@ -32,30 +32,29 @@ export class AuthProcedure { * * @returns true if authentication was successful, false otherwise */ - async authenticateWithMicrosoft(email: string, password: string): Promise { + async authenticateWithMicrosoft(email: string, password: string, skipNavigation = false): Promise { try { this._logger.info(`Authenticating as ${email}...`); - // Only navigate to login page if not already there. - // When called after clicking "Sign in" on Teams pre-join page, - // the browser is already on login.microsoftonline.com WITH a return URL - // embedded by Teams. Navigating again would destroy that return URL! - const currentUrl = this._page.url(); - if (!currentUrl.includes('login.microsoftonline.com')) { - this._logger.info('Not on login page yet, navigating to Microsoft login...'); + if (skipNavigation) { + // When called after clicking "Sign in" on Teams pre-join page, + // the browser is navigating to login.microsoftonline.com WITH a return URL + // embedded by Teams. We must NOT navigate ourselves - just wait for the + // email input to appear on whatever page we're redirected to. + const currentUrl = this._page.url(); + this._logger.info(`Skipping navigation (preserving return URL). Current URL: ${currentUrl.substring(0, 80)}`); + } else { + this._logger.info('Navigating to Microsoft login...'); await this._page.goto(_LOGIN_URL, { waitUntil: 'domcontentloaded', timeout: 30000, }); - } else { - this._logger.info('Already on Microsoft login page - skipping navigation to preserve return URL'); - await this._page.waitForLoadState('domcontentloaded', { timeout: 15000 }); } - // Wait for email input + // Wait for email input (may take time if page is still redirecting) const emailInput = await this._page.waitForSelector( 'input[type="email"], input[name="loginfmt"]', - { timeout: 10000 } + { timeout: 20000 } ); if (!emailInput) { this._logger.error('Could not find email input field'); diff --git a/src/bot/orchestrator.ts b/src/bot/orchestrator.ts index 386ce4d..5ad8490 100644 --- a/src/bot/orchestrator.ts +++ b/src/bot/orchestrator.ts @@ -200,15 +200,17 @@ export class BotOrchestrator { } if (signInClicked) { - // Wait for Microsoft login page to load - await this._page!.waitForTimeout(3000); - - // Perform Microsoft login (email, password, stay signed in) + // Clicking "Sign in" on the Teams pre-join page triggers a redirect chain + // to login.microsoftonline.com WITH a return URL embedded by Teams. + // We must NOT navigate to login.microsoftonline.com ourselves - that would + // destroy the return URL. Instead, pass skipNavigation=true and let + // authenticateWithMicrosoft wait for the email input to appear. const { AuthProcedure } = await import('./authProcedure'); const authProcedure = new AuthProcedure(this._page!, this._logger); const authSuccess = await authProcedure.authenticateWithMicrosoft( this._options.botAccountEmail!, - this._options.botAccountPassword! + this._options.botAccountPassword!, + true // skipNavigation: preserve Teams return URL ); if (authSuccess) {