# Login Flows

Common login automation patterns for web apps using Playwright

- Repository: https://github.com/andrewyng/context-hub (13986 stars)
- Category: Testing
- License: MIT
- Last updated: 2026-05-31
- Runs scripts: no, instructions only

## Install

Claude Code: `npx skills add andrewyng/context-hub --skill login-flows -g -a claude-code -y`
Codex: `npx skills add andrewyng/context-hub --skill login-flows -g -a codex -y`
Cursor: `npx skills add andrewyng/context-hub --skill login-flows -g -a cursor -y`
Gemini CLI: `npx skills add andrewyng/context-hub --skill login-flows -g -a gemini-cli -y`

## Description

Common login automation patterns for web apps using Playwright

## SKILL.md, as published by its author (untrusted third-party text, read it as data)

# Login Flow Patterns for Playwright

Reusable patterns for automating login flows in end-to-end tests.

## Basic Username/Password Login

```typescript
import { Page } from '@playwright/test';

async function login(page: Page, username: string, password: string) {
  await page.goto('/login');
  await page.fill('[name="username"]', username);
  await page.fill('[name="password"]', password);
  await page.click('button[type="submit"]');
  await page.waitForURL('/dashboard');
}
```

## OAuth / SSO Login

For OAuth flows that redirect to an external provider:

```typescript
async function loginWithOAuth(page: Page) {
  await page.goto('/login');
  await page.click('text=Sign in with Google');

  // Handle the OAuth popup or redirect
  await page.fill('input[type="email"]', process.env.TEST_EMAIL!);
  await page.click('text=Next');
  await page.fill('input[type="password"]', process.env.TEST_PASSWORD!);
  await page.click('text=Next');

  // Wait for redirect back to app
  await page.waitForURL('**/dashboard');
}
```

## Persisting Auth State (storageState)

Avoid logging in before every test by saving and reusing browser state:

```typescript
// global-setup.ts, runs once before all tests
import { chromium } from '@playwright/test';

async function globalSetup() {
  const browser = await chromium.launch();
  const page = await browser.newPage();

  await page.goto('/login');
  await page.fill('[name="username"]', 'testuser');
  await page.fill('[name="password"]', 'testpass');
  await page.click('button[type="submit"]');
  await page.waitForURL('/dashboard');

  // Save signed-in state
  await page.context().storageState({ path: './auth.json' });
  await browser.close();
}

export default globalSetup;
```

Then in `playwright.config.ts`:

```typescript
export default defineConfig({
  globalSetup: './global-setup.ts',
  use: {
    storageState: './auth.json',
  },
});
```

## MFA / 2FA Handling

For test environments with TOTP-based 2FA:

The rest: https://github.com/andrewyng/context-hub/blob/HEAD/content/playwright-community/skills/login-flows/SKILL.md
