Skip to content

Latest commit

Β 

History

History
93 lines (75 loc) Β· 2.72 KB

File metadata and controls

93 lines (75 loc) Β· 2.72 KB

βœ… Wallet Auto-Creation Fixed!

What Was Wrong:

  1. Profile Creation: The database trigger might not be set up in Supabase
  2. Timing Issue: Frontend used setTimeout which didn't properly wait
  3. No Profile Fallback: Backend assumed profile existed when creating wallet

What I Fixed:

Backend (auth.routes.js):

  • βœ… Now creates profile automatically if it doesn't exist
  • βœ… Checks for existing wallet before creating new one
  • βœ… Better error messages (409 for duplicates, not 500)

Frontend (AuthContext.jsx):

  • βœ… Removed setTimeout (unreliable)
  • βœ… Calls wallet initialization immediately after signup
  • βœ… Reuses same initializeWallet() function for signup and login
  • βœ… Doesn't block signup if wallet creation fails

How It Works Now:

User Signs Up
    ↓
Supabase creates auth user
    ↓
Frontend calls /api/auth/init-wallet
    ↓
Backend checks if profile exists
    β”œβ”€ No β†’ Creates profile with user info
    └─ Yes β†’ Uses existing profile
    ↓
Backend checks if wallet_id exists
    β”œβ”€ No β†’ Creates Bitnob wallet
    └─ Yes β†’ Returns existing wallet
    ↓
Backend saves wallet_id to profile
    ↓
βœ… User has wallet ready!

Test It:

  1. Sign up with a NEW email (fresh user)
  2. Check browser console - you should see:
    βœ… User signed up successfully, initializing wallet...
    πŸ”„ Initializing wallet...
    βœ… Wallet initialized: Wallet created successfully
    
  3. Go to Dashboard - wallet info should display
  4. Try creating an invoice - should work immediately!

Still Need To Do:

Run this SQL in your Supabase dashboard (optional but recommended):

-- Create automatic profile on user signup
CREATE OR REPLACE FUNCTION handle_new_user()
RETURNS TRIGGER AS $$
BEGIN
  INSERT INTO public.profiles (id, full_name, email)
  VALUES (
    NEW.id,
    COALESCE(NEW.raw_user_meta_data->>'full_name', SPLIT_PART(NEW.email, '@', 1)),
    NEW.email
  );
  RETURN NEW;
END;
$$ LANGUAGE plpgsql SECURITY DEFINER;

DROP TRIGGER IF EXISTS on_auth_user_created ON auth.users;
CREATE TRIGGER on_auth_user_created
  AFTER INSERT ON auth.users
  FOR EACH ROW EXECUTE FUNCTION handle_new_user();

This makes profile creation even more reliable (happens in database automatically).

Why The 404 Errors?

The 404s on /api/wallet/info and /api/wallet/balance happen because:

  • Wallet doesn't exist yet (first time user)
  • The endpoints check for wallet and return 404 if not found
  • This is expected behavior until wallet is created

Once wallet is initialized, these 404s will disappear!

Try It Now:

Sign up with a brand new email and watch the console - wallet should be created automatically! πŸš€