# Two-factor authentication (/two-factor-authentication)

With two-factor authentication on, logging in to Plunk takes your password and a 6-digit code from an authenticator app on your phone. Someone who learns your password still can't get into your account, your projects or your API keys without that code.

Two-factor authentication is for accounts that log in with an email address and password. If you log in with GitHub or Google, your account is protected by that provider's login: turn on two-factor authentication there.

## Turn it on [#turn-it-on]

You need an authenticator app, such as 1Password, Google Authenticator or Authy.

<Steps>
<Step>
### Open your account settings [#open-your-account-settings]

Open the account menu in the dashboard and choose **Account settings**. Under **Security**, find **Two-factor authentication** and click **Set up**.
</Step>

<Step>
### Confirm your password [#confirm-your-password]

Plunk asks for your password before it changes how you log in.
</Step>

<Step>
### Connect your authenticator app [#connect-your-authenticator-app]

Scan the QR code with your app. If you can't scan it, enter the key shown below the code instead. Then type the 6-digit code the app shows, to confirm it is connected.
</Step>

<Step>
### Save your recovery codes [#save-your-recovery-codes]

Plunk gives you 10 recovery codes. Each one logs you in once if you don't have your phone. They are only shown this once, so copy them or download them as a text file, and keep them somewhere safe, away from your phone, such as your password manager.
</Step>
</Steps>

Two-factor authentication is on from this point. The **Security** section shows it as **On**, with the date you added it.

## Logging in [#logging-in]

After your email and password, Plunk asks for the 6-digit code from your authenticator app. If you don't have your phone, choose **Use a recovery code** and enter one of your saved codes instead.

You have 5 minutes and 5 tries to enter the code. After that, start again from your password. Too many wrong codes pause further attempts for up to 15 minutes, which stops anyone from guessing their way in.

## Recovery codes [#recovery-codes]

Each recovery code works once. The **Security** section shows how many you have left, and reminds you when you're down to the last two.

To replace them, click **Create new codes**, then confirm with your password and a code from your app. You get 10 new codes, and the old ones stop working.

### If you lose your phone [#if-you-lose-your-phone]

1. Log in with your password and a recovery code.
2. In **Account settings**, turn two-factor authentication off.
3. Set it up again with your new phone. This also gives you a fresh set of recovery codes.

Resetting your password doesn't turn two-factor authentication off: you still need a code to log in afterwards.

<Callout type="warn" title="Keep your recovery codes">
If you lose both your phone and your recovery codes, you can't log in on your own. Contact support from the email address on your account.
</Callout>

## Turn it off [#turn-it-off]

In **Account settings**, click **Turn off** next to two-factor authentication and confirm with your password and a code from your app or a recovery code. Logging in then only needs your password, and your authenticator app and recovery codes stop working.

## Security emails [#security-emails]

Plunk emails you whenever something about your second factor changes, so you notice a change you didn't make.

| What happened                        | Email                                                                      |
| ------------------------------------ | -------------------------------------------------------------------------- |
| Two-factor authentication turned on  | Logging in now also asks for a code                                        |
| Two-factor authentication turned off | Logging in now only needs your password                                    |
| New recovery codes created           | Your previous codes no longer work                                         |
| A recovery code was used to log in   | Someone logged in with your password and a recovery code, and how many codes are left |

If you get one of these emails and didn't make the change, someone has your password: reset it from the login page right away, then check your two-factor settings.

## Self-hosting [#self-hosting]

Two-factor authentication is off by default on a self-hosted instance. Turn it on with two [environment variables](/self-hosting/environment-variables):

| Variable                    | Value                                                         |
| --------------------------- | ------------------------------------------------------------- |
| `TWO_FACTOR_ENABLED`        | `true`                                                        |
| `TWO_FACTOR_ENCRYPTION_KEY` | 32 random bytes, base64-encoded: `openssl rand -base64 32`    |

The key encrypts each user's authenticator secret and recovery codes. The API refuses to start if `TWO_FACTOR_ENABLED` is `true` without a valid key. Keep it with your other secrets and never change it once people have set up two-factor authentication: Plunk can no longer read their secrets, and they can't log in.

Setting `TWO_FACTOR_ENABLED` back to `false` stops new set-ups, but anyone who already turned it on is still asked for a code, and can still turn it off from their account settings.

The security emails go out through Plunk's platform email, which needs `PLUNK_API_KEY` and `PLUNK_FROM_ADDRESS`. Without them, the changes still happen, but nobody is emailed.