Skip to main content
Two Factor Authentication support is available in CoCart Plus starting from .

Prerequisites

CoCart Plus automatically detects the plugin and loads 2FA support only when available.
In the following request examples below, you would replace <username> and <password> with your credentials before sending the request.

Authentication Flow

1. Initial Login Attempt (No 2FA Code)

When a user with 2FA enabled attempts to login without providing a 2FA code:
Response (401 Unauthorized):
Important: If the default provider is email, an email with the verification code is automatically sent to the user’s email address, and email_sent will be true.

2. Complete Login with 2FA Code

Option B: Specifying Provider Explicitly

Default Provider Logic

The default provider is determined by the following priority order:
  1. Last Successfully Used Provider: The provider the user most recently used for successful authentication (CoCart Plus tracks this automatically)
  2. User’s Primary Provider: The provider set as primary in the user’s Two Factor settings
  3. First Enabled Provider: If no primary provider is set, uses the first provider in the enabled providers list
  4. Fallback: Empty string if no providers are enabled (shouldn’t occur in practice)

Provider Memory & Smart Defaults

CoCart Plus automatically remembers which 2FA provider each user successfully used last time, making subsequent logins more convenient:

Automatic Tracking

  • When: Every successful 2FA authentication
  • What: Stores the provider alias and timestamp
  • Where: User meta keys _cocart_2fa_last_used_provider and _cocart_2fa_last_used_timestamp

Benefits

  • Faster logins: Users don’t need to specify their preferred provider each time
  • Better UX: Reduces cognitive load for users with multiple 2FA methods
  • Fail-safe: Always validates that the remembered provider is still enabled

API Parameters

2fa_code

  • Type: String
  • Required: Conditional (required only if user has 2FA enabled)
  • Description: The verification code from the user’s 2FA provider

2fa_provider

  • Type: String
  • Required: No
  • Enum: Dynamically populated from available Two Factor providers
  • Description: Specific provider to use for verification
  • Available Providers: The list includes all providers installed with the Two Factor plugin using clean aliases (e.g., email, totp, backup)

Provider-Specific Behavior

TOTP (Time-based One-Time Password)

  • Uses apps like Google Authenticator, Authy
  • Codes are 6-digit numbers that expire every 30 seconds
  • No additional setup required during login

Email

  • Sends verification codes via email
  • Automatic Email Sending: When email is the default provider and no code is provided, an email is automatically sent
  • Codes typically expire after 15 minutes
  • Response includes "email_sent": true when email is sent

Backup Codes

  • Static codes that can be used when primary method is unavailable
  • Each code can only be used once
  • Treated like any other provider in the API

Error Handling

Invalid 2FA Code

Invalid Provider

Client Implementation Example

Security Considerations

  • All 2FA validation uses the Two Factor plugin’s security measures
  • Codes are validated with replay protection
  • Email codes have built-in expiration
  • Rate limiting is handled by the Two Factor plugin
  • TOTP validation accounts for clock drift
  • No 2FA codes are logged or stored by CoCart