๐Ÿ”

Authentication

๐Ÿ‘จโ€๐Ÿณ Chef

Identity and security

Authentication verifies who you are. Authorization verifies what you can do.


Authentication methods

MethodWhen to use
OAuthLogin with Google, GitHub
Email/PasswordOwn users
Magic LinksPasswordless
JWTStateless APIs

OAuth 2.0 Flow

User โ†’ Your app โ†’ Provider (Google)
                        โ†“
User โ† Your app โ† Token + Info

JWT (JSON Web Tokens)

// Structure: header.payload.signature
const token = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9..."

// Content (payload)
{
  "sub": "user123",
  "email": "user@email.com",
  "exp": 1699999999
}

Recommended services

ServiceTypeIdeal for
Firebase AuthBaaSMobile, web apps
Supabase AuthBaaSFull-stack
Auth0SaaSEnterprise
NextAuth.jsLibraryNext.js apps

Basic security

PracticeWhy
HTTPS alwaysEncrypts traffic
Short tokensLimits damage if stolen
Refresh tokensRenew without re-login
Rate limitingPrevents brute force

๐Ÿฆ Security for Financial Apps

If your app handles money or financial data, you need additional measures.

Required Checklist

RequirementWhyStandard
โœ… Mandatory MFAPSD2/Open Banking regulationNIST 800-63B
โœ… Immutable logsRegulatory auditsSOC 2
โœ… Encryption at restSensitive data protectionPCI DSS
โœ… Session timeoutFraud preventionOWASP
โœ… Aggressive rate limitingAnti-scraping-

What is PCI DSS?

If your app processes, stores or transmits payment card data, you must comply with PCI DSS.

LevelTransactions/yearRequirements
4< 20,000SAQ (Self-Assessment)
320,000 - 1MSAQ + quarterly scan
21M - 6MSAQ + audit
1> 6MFull annual audit

๐Ÿ’ก Tip: Use Stripe, PayPal or similar to avoid handling card data directly. They assume the compliance burden.

MFA Implementation

// Verify second factor before sensitive operations
async function requireMFA(userId: string, action: string) {
  const user = await getUser(userId)

  if (SENSITIVE_ACTIONS.includes(action) && !user.mfaVerifiedAt) {
    throw new Error('MFA_REQUIRED')
  }

  // Audit log
  await auditLog({
    userId,
    action,
    mfaVerified: true,
    timestamp: new Date().toISOString(),
    ip: request.ip
  })
}

const SENSITIVE_ACTIONS = [
  'TRANSFER_FUNDS',
  'CHANGE_PASSWORD',
  'ADD_BENEFICIARY',
  'EXPORT_DATA'
]

Practice

โ†’ Auth with Firebase Google