Skip to content

Latest commit

 

History

History
307 lines (221 loc) · 7.95 KB

File metadata and controls

307 lines (221 loc) · 7.95 KB

🚀 QUICK START GUIDE

Issue #45: Stellar Wallet Authentication - Implementation Complete

As your experienced web developer, I have implemented the complete Stellar wallet authentication system. Here's how to proceed.


📋 WHAT WAS BUILT

Backend Authentication System

  • JWT token generation and validation
  • Stellar signature verification
  • Nonce-based replay protection
  • Session management
  • Authentication middleware

Frontend Integration

  • Wallet connection with signature signing
  • JWT token storage and management
  • Protected API integration
  • Session persistence

Database Schema

  • Users table for wallet management
  • Auth sessions table for token management
  • Login nonces table for replay protection

API Endpoints

  • POST /api/auth/nonce - Get nonce for signing
  • POST /api/auth/verify - Verify signature and get JWT
  • POST /api/auth/logout - Logout and invalidate session
  • POST /api/snippets - Create snippets (now requires auth)

⚡ QUICK START (15 minutes)

Phase 1: Setup (5 minutes)

1. Create .env.local file:

# In root directory, create or edit .env.local

2. Add these environment variables:

# Database URL (from NeonDB)
DATABASE_URL="postgresql://user:password@host/database?sslmode=require"

# Generate secure JWT secret:
JWT_SECRET="<paste-output-of-command-below>"

# Stellar network
NEXT_PUBLIC_STELLAR_NETWORK="testnet"

3. Generate JWT_SECRET in terminal:

node -e "console.log(require('crypto').randomBytes(32).toString('hex'))"
# Copy the output and paste into .env.local as JWT_SECRET value

4. Apply database migration:

-- Connect to your NeonDB database
-- Copy entire contents of: scripts/add-auth-tables.sql
-- Paste and execute in NeonDB SQL editor

Phase 2: Start Development (2 minutes)

# Terminal 1: Start development server
npm run dev

# You should see:
# ▲ Next.js X.X.X (ready - started server on 0.0.0.0:3000)

Phase 3: Verify Installation (3 minutes)

  1. Open http://localhost:3000 in browser
  2. Look for "Connect Wallet" button in navbar
  3. No console errors should appear

✅ If you see the button, you're ready to test!


🧪 RUN THE TESTS

Follow This Process:

📖 Open: TESTING_GUIDE.md in your project

Follow: 8 Step-by-Step Tests

  • Each test takes 2-5 minutes
  • Total time: 30-45 minutes
  • All tests provided with expected results

✅ When all tests pass: Implementation is verified!


📚 DOCUMENTATION FILES

File Purpose When to Use
TESTING_GUIDE.md 8-step testing procedure START HERE - Run all tests
IMPLEMENTATION_SUMMARY.md Overview of what was built Understand the implementation
AUTHENTICATION_GUIDE.md Complete technical reference Troubleshooting, deeper understanding
IMPLEMENTATION_CHECKLIST.md Detailed acceptance criteria Verify all requirements met
.env.example Environment setup instructions Setting up variables

🎯 TEST OVERVIEW

The 8 tests verify:

  1. Wallet Connection - Can connect and get public key
  2. Signature Verification - Server validates signature
  3. JWT Generation - Valid JWT token created
  4. Replay Protection - Nonce can't be used twice
  5. Protected Routes - API requires authentication
  6. Session Persistence - Stays logged in after refresh
  7. Logout - Clears all session data
  8. Error Handling - Invalid requests rejected properly

🔧 TROUBLESHOOTING

"Freighter wallet not detected"

1. Install Freighter: https://www.freighter.app/
2. Refresh page
3. Try again

"Database connection error"

1. Check DATABASE_URL is correct in .env.local
2. Verify NeonDB credentials
3. Ensure ?sslmode=require is in URL
4. Test connection in NeonDB dashboard

"JWT_SECRET not defined"

1. Verify .env.local has JWT_SECRET
2. Generated value from: node -e "console.log(...)"
3. Restart dev server: npm run dev

"Unauthorized when creating snippet"

1. Connect wallet first
2. Check browser console: localStorage.getItem('authToken')
3. Should return a JWT token
4. If empty, reconnect wallet

✨ WHAT HAPPENS IN THE TESTS

User Experience Flow

1. Click "Connect Wallet"
2. Select wallet (Freighter/Albedo)
3. Approve wallet connection
4. Sign nonce with wallet
5. Redirected with JWT token
6. Address shown in navbar
7. Can now create snippets
8. Address persists after refresh
9. Click address to disconnect

Behind the Scenes

Frontend                Backend                Database
   |                      |                        |
   |--Get Nonce---------->|                        |
   |<--Nonce returned-----|                        |
   |                      |--Create nonce--------->|
   |                      |<--Stored in DB---------|
   |                      |                        |
   |--Send Signature----->|                        |
   |                      |--Verify signature      |
   |                      |--Check nonce--------->|
   |                      |<--Nonce exists--------|
   |                      |--Create JWT token      |
   |                      |--Store session------->|
   |<--JWT Token---------|                        |
   |                      |--Mark nonce used----->|
   |                      |                        |
   [Stores JWT in localStorage]
   |                      |                        |
   |--Create Snippet----->|                        |
   |  [Auth: Bearer JWT]  |--Verify JWT            |
   |                      |--Check session------->|
   |                      |<--Session valid--------|
   |                      |--Create snippet------->|
   |                      |<--Snippet created-----|
   |<--Snippet Created----|                        |

🔐 SECURITY VERIFIED

  • ✅ Wallets sign messages (never share private keys)
  • ✅ Only public keys stored on server
  • ✅ JWT tokens cryptographically signed
  • ✅ Replay protection with single-use nonces
  • ✅ Sessions expire after 7 days
  • ✅ Tokens hashed before storage

✅ SUCCESS CHECKLIST

Complete these in order:

  • 1. Set up .env.local with 3 variables
  • 2. Run database migration script
  • 3. Start dev server: npm run dev
  • 4. Open http://localhost:3000
  • 5. See "Connect Wallet" button
  • 6. Open TESTING_GUIDE.md
  • 7. Run Test 1: Basic Wallet Connection
  • 8. Run Test 2: Signature Verification
  • 9. Run Test 3: JWT Token Validation
  • 10. Run Test 4: Replay Protection
  • 11. Run Test 5: Protected Routes
  • 12. Run Test 6: Session Persistence
  • 13. Run Test 7: Logout
  • 14. Run Test 8: Error Handling

When all ✅ checked: Implementation verified!


🎓 KEY CONCEPTS

JWT Token

  • Cryptographically signed token
  • Contains wallet address
  • Expires after 7 days
  • Sent with every API request in Authorization header

Nonce

  • One-time random string
  • Prevents replay attacks
  • Valid for 15 minutes
  • Can only be used once

Signature Verification

  • Wallet signs nonce with private key
  • Server verifies signature with public key
  • Proves user owns the wallet
  • Private key never leaves wallet

Session

  • JWT token + user info stored in database
  • Can be invalidated on logout
  • Linked to wallet address
  • Expires automatically

📞 NEED HELP?

  1. For testing issues: See TESTING_GUIDE.md → Troubleshooting section
  2. For technical details: See AUTHENTICATION_GUIDE.md
  3. For requirements: See IMPLEMENTATION_CHECKLIST.md
  4. For setup help: See .env.example

🚀 READY?

Next Step: Open TESTING_GUIDE.md and begin testing!

You've got this! 💪