Common issues and solutions for the PHP Authentication System
Problem: reCAPTCHA not working or showing "Invalid domain" errors.
Problem: Application URLs not working correctly, email links broken, or redirects failing.
Problem: Cannot complete 2FA setup, QR code not showing, or codes rejected.
Why required? Prevents a hijacked but unlocked session from silently adding 2FA (which attacker could capture) or disabling security factors.
user_2fa_reauth_passed
is set./2fa-setup
is blocked without that flag (HTTP redirect back).Controls: Session regeneration after login, forced logout after 2FA enrollment, role-based gating.
SESSION_COOKIE_SECURE=true
in production (HTTPS required).SameSite=Strict
to reduce CSRF surface.Overview: The application uses isolated session keys to separate pre-auth states (verification / 2FA) from fully privileged sessions.
Key | When Set | Purpose | Cleared |
---|---|---|---|
csrf_token | First protected form | CSRF protection | Session end |
pending_verification | Post-register / login (unverified) | Gate email verification | On verification |
user_email | Verification workflow | Resend context | On verification |
verification_attempts | Verification resend | Rate limiting | Success / end |
registration_time | Registration | Window control | End |
pending_2fa_user_id | Password accepted (2FA enabled) | Stage identity awaiting TOTP | TOTP success / logout |
pending_2fa_is_admin | Same as above (admin) | Marks admin pending flow | TOTP success / logout |
role | Full login | User vs Admin canonical role | Logout |
is_admin | Full admin login | Legacy flag | Logout |
admin_id | Admin login | Admin user id | Logout |
admin_username | Admin login | Display name | Logout |
user_id | User login | User id | Logout |
username | User login | Display name | Logout |
twofa_setup_secret | 2FA enrollment start | Proposed TOTP secret | Enroll success / cancel |
twofa_setup_uri | 2FA enrollment | Provisioning URI for QR | Enroll success / cancel |
force2fa_email_passed | Admin email code success | Permit TOTP setup | After enrollment |
user_2fa_reauth_passed | User password modal success | Permit TOTP setup (user) | After enrollment |
reauth_2fa_attempts | User re-auth failures | Throttle password prompts | On success / end |
Scopes: Login attempts, forced admin 2FA email code attempts (max 3), password re-auth (e.g., 5), resend email verification, contact form, newsletter join.
MongoDB attempts
collection (pattern: key + window). You can add TTL index:
Idea: Add IP + User composite keys to slow credential stuffing.
See .env.example
for full list. Highlights:
APP_URL
Base URL for email links (verification/reset/2FA).MONGODB_URI
Connection string; keep secret.MAIL_*
SMTP credentials & debug level.RECAPTCHA_SITE_KEY / SECRET_KEY
Bot mitigation.FORCE_ADMIN_2FA
(bool) Enforce admin 2FA bootstrap.SESSION_COOKIE_SECURE
Force secure cookies when HTTPS.LOG_LEVEL
Adjust verbosity (error|warning|info|debug
)..env
, load with Dotenv before other bootstrapping.
Problem: Third-party resources blocked, CSP violations, or external API calls failing.
The .htaccess file contains strict Content Security Policy (CSP) rules that only allow:
Problem: No error logs, debugging information not available, or issues are hard to diagnose.
Problem: Emails not sending, verification emails not received, or SMTP errors.
Issue: Emails land in spam or are silently dropped.
v=spf1 include:sendgrid.net ~all
).
p=none
policy to monitor, then tighten.MAIL_FROM_NAME
stable to build reputation.Problem: Database connection errors, authentication failures, or collection access issues.
Recommended Indexes:
TTL Note: Use date fields with TTL for automatic cleanup of transient tokens.
Problem: Permission denied errors, unable to write logs, or file access issues.
Issue: Slow loading times, high server load, or timeout errors.
Behavior: After successful 2FA enrollment the system destroys the current session and
redirects to login with a query flag (e.g., ?twofa_enabled=1
) so the user authenticates under
elevated security context.
Why: Prevents reuse of pre-2FA session cookies and enforces fresh TOTP usage on next login.
Tip: Display a flash message acknowledging success on the login page.
Problem: Installation errors, missing dependencies, or setup failures.
If you're still experiencing issues after following this FAQ: