---
url: https://game-engine-io.github.io/enginex-frontend-sdk/flows/login.md
---
# Login and 2FA

The demo above runs the real SDK (`createEnginexClient`) with `fetch: createMockFetch(...)` from
[`@getenginex/sdk/testing`](/guide/testing): no call goes out to the network. Switch the scenario to
see the 2FA step and the `INVALID_CREDENTIALS` error. The **Under the hood** panel shows the call made
(passwords and tokens masked), the requests seen by the [hooks](/concepts/extensibility), the result or
the `EnginexError`, and the session. The code below is the equivalent for your app.

```ts
let result = await enginex.auth.login({ email, password })   // or { cpf, password }

if (result.status === '2fa_required') {
  const twoFactorCode = await askUserFor2faCode()
  result = await enginex.auth.login({ email, password, twoFactorCode })
}
// result.status === 'authenticated'
```

If someone is already logged in and a login for **another account** returns `2fa_required`, the current
session stays active until the login with `twoFactorCode` completes. To switch accounts cleanly, call
`enginex.auth.logout()` first.

## Enable 2FA for the logged-in player

```ts
const { qrCode, backupCodes } = await enginex.auth.enable2fa()
// show the QR (qrCode is an otpauth:// URL) and the recovery codes
await enginex.auth.verify2fa({ twoFactorCode: firstCodeFromApp })
```

To disable: `await enginex.auth.disable2fa({ twoFactorCode, password })`.

## Log out

```ts
await enginex.auth.logout() // never throws; the local session is always cleared
```

Logout sends the current access token as is (it does not refresh the session first) and never fires
`onSessionExpired`. It also sends the stored refresh token in the body, so the gateway revokes it: a copy
of it cannot renew the session after the logout. The logout route requires a valid access token, so with
an expired one the gateway refuses the call before revoking; logout then renews once, without storing the
new tokens, and revokes the renewed refresh token. If the renewal or the request fails, the local session
is cleared anyway, but the refresh token may stay valid on the server until it expires.
