Complete Guide to Building Secure Authentication (Part 1)

For Young Engineers and Vibe Coders using Replit

Seriously, this is meant for the beginners.

Introduction - Why Security Matters

The Real Cost of Poor Security

Bad security can:

  • Get your app hacked and customer data stolen

  • Destroy your reputation and lose customers

  • Result in legal problems and fines

  • Cost thousands of dollars to fix after launch

  • Get your app shut down by hosting providers

Good security:

  • Protects your users' personal information

  • Builds trust with your customers

  • Prevents costly breaches and lawsuits

  • Makes your app professional and credible

  • Helps you sleep better at night

What You'll Learn

This guide teaches you how to build authentication (login/signup) that:

  1. Actually works - Users can easily create accounts and log in

  2. Is secure - Hackers can't break in or steal data

  3. Follows best practices - Industry-standard security

  4. Is user-friendly - Clear messages, no confusion

Prerequisites

You should know:

  • Basic programming (variables, functions, if/else)

  • How to use Replit to create a project

  • Basic understanding of how websites work

You DON'T need to know:

  • Advanced security concepts

  • Complex cryptography

  • Database administration (Replit handles most of it)

Understanding Authentication Basics

What is Authentication?

Authentication = Proving you are who you say you are

Think of it like:

  • Showing ID to enter a club (you prove it's really you)

  • Using a key to unlock your house (only you have the key)

  • Entering a password to unlock your phone (verification)

The Three Parts of Authentication

1. REGISTRATION (Creating an Account)

  • User provides: Name, Email, Password

  • System creates: Account in database

  • System sends: Verification email

  • Result: User has an account (but can't login yet)

2. VERIFICATION (Confirming Email)

  • User clicks: Link in verification email

  • System checks: Is link valid? Not expired?

  • Result: Account is verified, user can now login

3. LOGIN (Accessing the App)

  • User provides: Email and Password

  • System checks: Do these match what's in database?

  • Result: User gets access to their account

Why All Three Parts Matter

Without Registration: No accounts = no app
Without Verification: Fake accounts and spam everywhere
Without Login: Anyone can access anyone's account

Registration System

What Information to Collect

MUST COLLECT:

  • First Name - so you know what to call them

  • Last Name - for full name identification

  • Email Address - considered a unique identifier (username)

  • Password - their secret key to login

SHOULD NOT COLLECT (unless needed):

  • Phone number - only if you actually need it

  • Address - only for shipping/billing

  • Birth date - only if age verification needed

  • Social security number - never, unless you're finance or govt.

Why less is better:

  • Less data to protect = less liability

  • Faster signup = more users complete it

  • Privacy-friendly = builds trust

Password Requirements Explained

Minimum 12 Characters - Why?

  • Short passwords are easy to guess

  • 12+ characters = trillions of possible combinations

  • Makes brute force attacks nearly impossible

  • Modern best practice (traditional standard is 8, but that's increasingly considered weak)

Password Strength Examples:

  • Weak: "password123" - Common word + numbers

  • Okay: "MyPassword123" - Better but still predictable

  • Strong: "MyP@ssw0rd#2024" - Mixed types, harder to guess

  • Stronger: "Tr3e$H0use!B1ue" - Even better!

Character types needed:

  • Uppercase letters (A-Z)

  • Lowercase letters (a-z)

  • Numbers (0-9)

  • Special characters (!@#$%^&*)

Password Strength Indicator

Show users in real-time how strong their password is:

User types: "pass"
Show: WEAK - Too short

User types: "password"
Show: WEAK - Add numbers and special characters

User types: "Password123"
Show: MEDIUM - Add special characters

User types: "P@ssword123"
Show: STRONG - Good to go!

Why this helps:

  • Users understand what's needed

  • No frustrating "Password rejected" after submitting

  • Encourages stronger passwords

  • Better user experience

Email Verification - Why It's Required

Without email verification:

  • User enters: fake@notreal.com

  • Creates account with fake email

  • You can never contact them

  • They might be a bot or spammer

With email verification:

  • User enters: real@email.com

  • We send verification link

  • User clicks link to confirm

  • Now we know email is real

  • We can contact them if needed

Verification Link Rules:

  1. Expires in 24 hours - Security (old links don't work forever)

  2. One-time use - Can't be reused (prevents sharing)

  3. Unique per user - Can't be guessed

  4. Random token - Cryptographically secure

Example Registration Flow

STEP 1: User fills form

First Name: John
Last Name: Smith
Email: john@email.com
Password: ************
[Create Account Button]

STEP 2: System validates

  • ✓ Is email format valid?

  • ✓ Does password meet requirements?

  • ✓ Is email already registered?

  • ✓ All fields filled out?

STEP 3: System creates account

  • Hash the password (never store plain text!)

  • Create database record

  • Generate unique verification token

  • Send verification email

STEP 4: User checks email

Welcome to Our App!

Click to verify:
[Verify Email]

Link expires in 24 hrs

STEP 5: User clicks link

  • System checks: Is token valid?

  • System checks: Is it expired?

  • If yes: Mark account as verified

  • Show: "Email verified! You can now login"

STEP 6: User can login

  • Now the account is active and ready!

Cleaning Up Unverified Accounts

The Problem:

  • Day 1: User creates account but never verifies

  • Day 2: Still no verification

  • Day 3: Still no verification

  • ... (account just sits there forever taking up space)

The Solution:

  • Automatic cleanup after 48 hours:

  • Background job runs daily

  • Finds accounts created >48 hours ago

  • Finds accounts still not verified

  • Deletes them automatically

  • Frees up database space

Why 48 hours?

  • Gives users plenty of time (2 full days)

  • Removes abandoned registrations

  • Reduces database clutter

  • Prevents fake account buildup

The Login System

Simple Login - Email and Password Only

Email: john@email.com
Password: *************
[Login Button]
Remember me

Forgot password?

Why not username?

  • Emails are unique (can't duplicate)

  • Users already know their email

  • No "what was my username?" confusion

  • Industry standard practice

Password Field Best Practices

ALWAYS hide password by default:

Wrong: Password: MyPassword123 (Anyone can see it!)
Right: Password: ************* (Hidden from view)

Include show/hide toggle:

Password: ************* (Click eye to show/hide)

Why?

  • Protects from shoulder surfing (someone looking over shoulder)

  • Still lets users check if they typed correctly

  • Best of both worlds

Account Lockout - Your Shield Against Hackers

The Attack (Brute Force):

Hacker tries:

  1. password

  2. password123

  3. Password123

  4. P@ssword

  5. tries thousands of passwords

Without lockout:

  • Hacker keeps trying forever

  • Eventually might guess correctly

  • Gets into the account

With lockout after 20 attempts:

  • Hacker tries 20 times

  • Account locks automatically

  • Can't try anymore

  • Account is protected!

Lockout Details:

  • When: After 20 consecutive failed login attempts

  • Duration: 24 hours OR until admin unlocks

  • User notification: Email sent immediately

  • Reset: Counter resets to 0 after successful login

Important: The counter tracks failed attempts PER ACCOUNT, not per IP address. This prevents an attacker from bypassing the lockout by changing their IP address.

Rate Limiting - Slowing Down Attackers

Without rate limiting:

  • Attacker tries 1000 passwords per second

  • 1000 attempts × 60 seconds = 60,000 attempts per minute

  • Can try millions of passwords quickly

With rate limiting:

  • After 3 failed attempts: Wait 1 second

  • After 5 failed attempts: Wait 5 seconds

  • After 10 failed attempts: Wait 30 seconds

  • After 15 failed attempts: Wait 2 minutes

  • Massively slows down the attack!

Exponential backoff = waiting time grows:

  • Attempt 1: Instant

  • Attempt 2: Instant

  • Attempt 3: Wait 1 second

  • Attempt 4: Wait 2 seconds

  • Attempt 5: Wait 4 seconds

  • Attempt 6: Wait 8 seconds

  • ...doubles up each time

Session Management - Staying Logged In Safely

What is a session?

  • When you login, system creates a "session"

  • Session = temporary permission to be logged in

  • Session has an ID (like a temporary password)

  • Session stored in secure cookie

Session timeout - Why it matters:

Scenario: You login at library computer, walk away, forget to logout

Without timeout:

  • You're logged in forever

  • Next person uses computer

  • They access your account!

With 30-minute timeout:

  • After 30 minutes of no activity

  • System logs you out automatically

  • Your account is safe!

Timeout durations:

  • Customers: 30 minutes (balance of convenience and security)

  • Admins: 15 minutes (more sensitive, shorter timeout)

"Remember Me" option:

☑️ Remember me for 30 days

What this does:

  • Keeps you logged in for 30 days

  • Even after closing browser

  • Skip login on your personal devices

  • Still requires password if session expires

When to use "Remember Me":

  • Your personal computer

  • Your phone

  • Public computers

  • Shared devices

  • Work computers (if personal account)

Password Management

Password Hashing - The #1 Rule

NEVER STORE PASSWORDS IN PLAIN TEXT!

ABSOLUTELY WRONG (NEVER DO THIS):

Database:
┌──────┬───────────────┬──────────────┐
│ User │ Email         │ Password     │
├──────┼───────────────┼──────────────┤
│ 1    │ john@mail.com │ MyPass123!   │
│ 2    │ jane@mail.com │ Secret456#   │
└──────┴───────────────┴──────────────┘
If database is hacked → All passwords stolen!

RIGHT (ALWAYS DO THIS):

Database:
┌──────┬───────────────┬────────────────────────────┐
│ User │ Email         │ Password Hash              │
├──────┼───────────────┼────────────────────────────┤
│ 1    │ john@mail.com │ $2b$10$N9qo8uL...          │ 
│ 2    │ jane@mail.com │ $2b$10$8xKzQ2...           │ 
└──────┴───────────────┴────────────────────────────┘

If database is hacked → Passwords are gibberish!
Hacker can't reverse the hash to get real password!!!

What is Hashing?

Hashing = One-way encryption

Password: "MyPass123!"
   ↓ (hash with bcrypt)
Hash: "$2b$10$N9qo8uLTnmicShA..."
   ↓ (IMPOSSIBLE to reverse!)
   ✗ Can't get back to "MyPass123!"

How login works with hashing:

Login attempt:

  1. User enters: "MyPass123!"

  2. System hashes input: $2b$10$N9qo8uLTnmi...

  3. System compares to stored hash

  4. If they match → Password correct!

  5. If they don't match → Password wrong!

Use bcrypt or Argon2:

  • These are trusted hashing algorithms

  • Specifically designed for passwords

  • Include "salt" (random data) to prevent attacks

  • Industry Standard

Example Code Concept

When user registers:
plainPassword = "MyPass123!"
hashedPassword = bcrypt.hash(plainPassword)
save hashedPassword to the database

When user logs in:
inputPassword = "MyPass123!"
storedHash = fetch from database
if bcrypt.compare(inputPassword, storedHash):
    login successful!
else:
    login failed.

Password Reset - The Secure Way

User clicks "Forgot Password":

STEP 1: User enters email

Enter your email: john@email.com
[Send Reset Link]

STEP 2: System generates token

  • Create random secure token (e.g., "7x9K2mP...")

  • Save token in database with:

    • * User ID

    • * Expiration time (24 hours from now)

    • * Status: unused

      Send email with reset link

STEP 3: Email sent

Password Reset Request

Click to reset: [Reset Password]

Link expires in 24 hrs

STEP 4: User clicks link

  • System checks: Is token valid?

  • System checks: Is it expired?

  • System checks: Was it already used?

  • If all okay: Show new password form

STEP 5: User sets new password

New Password:
*************

Confirm Password:
*************

[Set New Password button]

STEP 6: System saves new password

  • Hash the new password

  • Save hash to database

  • Mark token as "used"

  • Send confirmation email

  • User can now login!

Security features:

  1. 24-hour expiration - Old links stop working

  2. One-time use - Can't use same link twice

  3. Random token - Impossible to guess

  4. Rate limiting - After 8 attempts, delay 5 minutes

  5. Email notification - User knows about reset request

Why rate limiting on reset?

Attacker spam-requests resets for victim's email:

  • 1st-8th request: Immediate

  • 9th request: WAIT 5 MINUTES

  • 10th request: WAIT 5 MINUTES

This prevents:

  • Inbox flooding

  • Denial of service

  • Harassment

Password History - Prevent Reuse

The problem:

  • User's password leaked in data breach

  • Hacker knows password is "MyPass123!"

Without password history:

  • User resets to "NewPass456"

  • Later changes back to "MyPass123!"

  • Hacker still has valid password!

With password history:

  • User tries to change to "MyPass123!"

  • System: "You've used this before"

  • User must choose different password

How it works:

System stores hashes of last 5 passwords:

  1. $2b$10$N9qo8uL... (current)

  2. $2b$10$K7mP3sT... (previous)

  3. $2b$10$R4xY9wQ... (2 passwords ago)

  4. $2b$10$L1nB5vF... (3 passwords ago)

  5. $2b$10$M8pT2rD... (4 passwords ago)

When user changes password:

  • System checks new password against all 5

  • If match found: Reject change

  • If no match: Accept new password

  • Add new hash to history

  • Remove oldest hash

Why 5 passwords?

  • Balances security and usability

  • Prevents immediate reuse

  • Not too restrictive - users can still find a new password

  • Industry standard


This is Part 1 of the guide. The complete guide continues with:

  • Admin Security

  • Error Messages Done Right

  • Email Integration

  • Database Security

  • Common Pitfalls

  • Testing Your Security

  • Troubleshooting Guide

Key Takeaways So Far

You've learned:

  • Why security matters for your app's success

  • The three parts of authentication (registration, verification, login)

  • How to collect the right information from users

  • Why password requirements protect your users

  • How email verification prevents fake accounts

  • Why account lockout stops brute force attacks

  • The critical importance of password hashing

  • How to implement secure password reset

Next up: Admin security, error messages, email integration, and more!

This is for: Non-HIPAA Customer-Facing Applications

About this guide: This is a comprehensive security guide specifically designed for young engineers and vibe coders building authentication systems in Replit. It explains not just what to do, but why each security feature matters.

Looking for a Replit Prompt? Soon.

Previous
Previous

Complete Replit Authentication Prompt