> ## Documentation Index
> Fetch the complete documentation index at: https://intunedhq.com/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# Set up two-factor authentication

## Recipe

This recipe shows how to set up a two-factor authentication (2FA) flow in Intuned online IDE.

## Prerequisites

<Steps>
  <Step title="Enable auth session in `intuned.json`">
    Make sure authentication sessions are enabled.
  </Step>

  <Step title="Install the two-factor authentication package">
    Add the required dependency to your project:

    <CodeBlock>
      {`"totp-generator": "^1.0.0"`}
    </CodeBlock>
  </Step>
</Steps>

## Code implementation

Below is a full example of how to automate a create auth session api to login using Intuned + Playwright + TOTP.

<CodeBlock language="ts">
  {`import { Page, BrowserContext } from "playwright";
    import { TOTP } from "totp-generator";

    export interface CreateAuthSessionParams {
    username: string;
    password: string;
    secret: string; // TOTP secret key
    }

    export default async function create(
    params: CreateAuthSessionParams,
    page: Page,
    context: BrowserContext
    ) {

    await page.goto("https://www.npmjs.com/login");

    // Fill username and password
    await page.getByLabel("Username").fill(params.username);
    await page.getByLabel("Password", { exact: true }).fill(params.password);
    await page.getByRole("button", { name: "Sign In" }).click();

    // Generate TOTP code
    const token = TOTP.generate(params.secret, { digits: 6 }).otp;

    // Submit one-time password
    await page.getByLabel("One-time Password").waitFor();
    await page.getByLabel("One-time Password").fill(token);
    await page.getByRole("button", { name: "Login" }).click();

    await page.waitForURL("https://www.npmjs.com/");
    }`}
</CodeBlock>

## Related links

<CardGroup cols={2}>
  <Card title="Auth sessions" href="/main/02-features/auth-sessions">
    Intuned auth sessions
  </Card>

  <Card title="Cookbook (Python)" icon="github" href="https://github.com/Intuned/cookbook/tree/main/python-examples/auth-with-secret-otp">
    Python TOTP authentication example in the Intuned Cookbook
  </Card>

  <Card title="Cookbook (TypeScript)" icon="github" href="https://github.com/Intuned/cookbook/tree/main/typescript-examples/auth-with-secret-otp">
    TypeScript TOTP authentication example in the Intuned Cookbook
  </Card>
</CardGroup>
