Authentication & API Testing Guide
Quick Start
1. Start the Development Server
npm run dev
Visit http://localhost:3000
2. Test User Registration
Via UI:
- Go to http://localhost:3000/signup
- Fill in the form:
- Full Name: Test User
- Email: test@example.com
- Password: password123
- Confirm Password: password123
- Click "Create Account"
- Should be signed in automatically and redirected to home
Via API (curl):
curl -X POST http://localhost:3000/api/auth/signup \
-H "Content-Type: application/json" \
-d '{
"name": "Test User",
"email": "testuser@example.com",
"password": "password123"
}'
3. Test User Login
Via UI:
- Go to http://localhost:3000/signin
- Enter email: test@example.com
- Enter password: password123
- Click "Sign in to account"
- Should see your name in the header
Expected Flow:
- Form submits
- Shows "Signing in..." button state
- On success: toast notification + redirect to home
- On failure: error message displayed + button returns to normal
4. Test Session State in Header
When Not Logged In:
- Header shows: "ACCOUNT" / "Sign In" link
When Logged In:
- Header shows: "ACCOUNT" / Your username
- Click on username to see dropdown with:
- My Account
- Orders
- Sign Out
5. Test Sign Out
- Click your username in header
- Click "Sign Out"
- Redirected to home page
- Header reverts to "Sign In" link
API Testing
Cart Operations (Require Authentication)
Add Item to Cart:
# First, get a valid session by signing in via UI or using signIn
curl -X POST http://localhost:3000/api/cart \
-H "Content-Type: application/json" \
-H "Cookie: <session-cookie-from-browser>" \
-d '{
"productId": 1,
"quantity": 1
}'
Get Cart:
curl -X GET http://localhost:3000/api/cart \
-H "Cookie: <session-cookie-from-browser>"
Update Cart Item:
curl -X PATCH http://localhost:3000/api/cart/1 \
-H "Content-Type: application/json" \
-H "Cookie: <session-cookie-from-browser>" \
-d '{
"quantity": 2
}'
Remove from Cart:
curl -X DELETE http://localhost:3000/api/cart/1 \
-H "Cookie: <session-cookie-from-browser>"
Wishlist Operations (Require Authentication)
Add to Wishlist:
curl -X POST http://localhost:3000/api/wishlist \
-H "Content-Type: application/json" \
-H "Cookie: <session-cookie-from-browser>" \
-d '{
"productId": 1
}'
Get Wishlist:
curl -X GET http://localhost:3000/api/wishlist \
-H "Cookie: <session-cookie-from-browser>"
Remove from Wishlist:
curl -X DELETE http://localhost:3000/api/wishlist/1 \
-H "Cookie: <session-cookie-from-browser>"
Testing Without Authentication
Try accessing protected endpoints without a session cookie - should return:
{
"error": "Unauthorized"
}
With status code 401.
Test Scenarios
Scenario 1: Complete Auth Flow
- ✅ Signup new user
- ✅ Verify auto-signin works
- ✅ Check header shows user name
- ✅ Add item to cart (authenticated)
- ✅ Add to wishlist (authenticated)
- ✅ Sign out
- ✅ Verify header reverts to "Sign In"
- ✅ Signin with same user
- ✅ Verify cart/wishlist still there
Scenario 2: Error Handling
- ✅ Try signup with existing email → "Email already registered"
- ✅ Try signup with non-matching passwords → "Passwords do not match"
- ✅ Try signup with short password → "Password must be at least 6 characters"
- ✅ Try signin with wrong password → "Invalid credentials"
- ✅ Try signin with non-existent email → "Invalid credentials"
Scenario 3: Protected Routes
- ✅ Try adding to cart without signin → 401 Unauthorized
- ✅ Try adding to wishlist without signin → 401 Unauthorized
- ✅ Try getting cart without signin → 401 Unauthorized
- ✅ Try getting wishlist without signin → 401 Unauthorized
Scenario 4: Session Persistence
- ✅ Signin user
- ✅ Close browser tab
- ✅ Open new tab to http://localhost:3000
- ✅ User should still be logged in
- ✅ Session cookie should persist
Browser DevTools Tips
Viewing Session
- Open DevTools (F12)
- Go to Application → Cookies
- Look for cookies like:
next-auth.session-token(JWT session)next-auth.csrf-token(CSRF protection)
Viewing API Requests
- Open DevTools → Network tab
- Perform auth action (signin/signup)
- Look for requests to
/api/auth/* - Check response status and body
Console for Debugging
- Open DevTools → Console
- Check for any JavaScript errors
- NextAuth logs debugging info in console
Common Issues & Solutions
Issue: "Unauthorized" on All Cart Requests
Solution:
- Make sure you're signed in
- Check that session cookie exists in DevTools
- Try signing out and signing in again
- Clear browser cookies and start fresh
Issue: Signup Shows "Email already registered"
Solution:
- Use a different email address
- Or clear database and reseed it:
npx prisma migrate reset --force
Issue: Signin Doesn't Work
Solution:
- Verify email and password are correct
- Check that user exists in database:
npx prisma studio # Navigate to User table and check records
Issue: Header Not Showing User Name After Signin
Solution:
- Refresh the page (F5)
- Check browser console for errors
- Check that NEXTAUTH_SECRET is set in .env
Issue: "NEXTAUTH_SECRET not set" Error
Solution:
- Add to .env:
NEXTAUTH_SECRET="your-secret-key" - Restart dev server with
npm run dev
Database Testing
View Users
npx prisma studio
# Then navigate to User table
Query Users via Prisma CLI
npx prisma db execute --stdin < query.sql
Reset Database
⚠️ Warning: This deletes all data!
npx prisma migrate reset --force
Performance Testing
Load Testing Auth Endpoints
# Using Apache Bench
ab -n 100 -c 10 http://localhost:3000/api/auth/signup
# Using wrk
wrk -t4 -c100 -d30s http://localhost:3000/
Memory Profiling
- Open DevTools → Memory
- Take heap snapshots
- Perform auth actions
- Take another snapshot
- Compare for memory leaks
Security Testing
Password Validation
- ✅ Min 6 characters required
- ✅ Passwords hashed with bcryptjs
- ✅ Password never logged or exposed
Email Uniqueness
- ✅ Trying to signup twice with same email fails
- ✅ Case-insensitive email checking
Session Security
- ✅ JWT tokens signed with NEXTAUTH_SECRET
- ✅ Session cookies HTTP-only
- ✅ CSRF protection enabled
- ✅ Unauthorized requests return 401
SQL Injection Prevention
- ✅ All queries use Prisma (parameterized)
- ✅ No raw SQL strings exposed to user input
Checklist Before Deploying
- Test signup flow in production build
- Test signin flow in production build
- Test cart operations require auth
- Test wishlist operations require auth
- Test session persistence
- Test sign out functionality
- Verify NEXTAUTH_SECRET is set
- Verify NEXTAUTH_URL is correct domain
- Test HTTPS works (required for cookies)
- Test error handling and validation
- Performance test under load
- Security audit complete