# Quick Start Guide: Switch to Username Login

This guide will help you quickly implement username-based login in your banking system.

## Overview

Your system currently uses account numbers for login. This migration adds a separate username field for authentication while keeping account numbers for reference.

## Files Created

1. **database_migration_add_username.sql** - Database schema changes
2. **populate_usernames.php** - Script to assign usernames to existing accounts
3. **test_username_login.php** - Test script to verify everything works
4. **MIGRATION_INSTRUCTIONS.md** - Detailed migration guide
5. **USERNAME_LOGIN_CHANGES_SUMMARY.md** - Complete technical documentation

## Files Modified

1. **user_access.php** - Main login page (form and authentication logic)
2. **include/class.user.php** - User class login method

## Step-by-Step Implementation

### Step 1: Backup Your Database (CRITICAL!)

```bash
mysqldump -u your_username -p your_database > backup_$(date +%Y%m%d).sql
```

Or use phpMyAdmin to export your database.

### Step 2: Run Database Migration

**Option A: Using MySQL command line**
```bash
mysql -u your_username -p your_database < database_migration_add_username.sql
```

**Option B: Using phpMyAdmin**
1. Open phpMyAdmin
2. Select your database
3. Click "Import" tab
4. Choose `database_migration_add_username.sql`
5. Click "Go"

### Step 3: Populate Usernames

1. Upload `populate_usernames.php` to your website root
2. Open it in your browser: `https://yoursite.com/populate_usernames.php`
3. The script will automatically assign usernames to all accounts

**Default behavior:** Uses account numbers as usernames (simplest and safest)

**To use name-based usernames:** Edit line 14 in `populate_usernames.php`:
```php
$strategy = 'name_based'; // Instead of 'account_number'
```

### Step 4: Test the Changes

1. Upload `test_username_login.php` to your website root
2. Open it in your browser: `https://yoursite.com/test_username_login.php`
3. Review all test results
4. All tests should show green checkmarks ✓

### Step 5: Test Login

1. Go to your login page
2. Try logging in with a username (instead of account number)
3. Verify:
   - Login works with correct credentials
   - Error message shows for wrong credentials
   - OTP email is sent
   - PIN authentication works
   - Dashboard loads correctly

### Step 6: Make Username Required (Optional but Recommended)

Once everything is working, make username mandatory:

```sql
ALTER TABLE account MODIFY COLUMN username VARCHAR(100) NOT NULL;
```

### Step 7: Clean Up (IMPORTANT!)

Delete these files from your server for security:
- `populate_usernames.php`
- `test_username_login.php`
- `QUICK_START_GUIDE.md` (this file)
- `MIGRATION_INSTRUCTIONS.md`
- `USERNAME_LOGIN_CHANGES_SUMMARY.md`
- `database_migration_add_username.sql`

Keep only the modified code files.

## What Changed for Users?

### Before:
- Login with: Account Number + Password
- Example: `1234567890` + password

### After:
- Login with: Username + Password
- Example: `john.doe` + password

## What Stayed the Same?

- Account numbers still exist and are displayed everywhere
- All dashboard functionality unchanged
- Transaction history unchanged
- Admin panel unchanged (uses email login)
- OTP and PIN authentication unchanged

## Troubleshooting

### "Username column does not exist"
- Run the database migration script again
- Check if you're connected to the correct database

### "Some accounts don't have usernames"
- Run `populate_usernames.php`
- Or manually assign usernames through database

### "Duplicate username error"
- Check for duplicate usernames in database
- Manually fix duplicates or change username generation strategy

### "Login not working"
- Clear browser cache
- Check if files were uploaded correctly
- Verify database changes were applied
- Check error logs

### "Can't access test scripts"
- Make sure files are uploaded to website root
- Check file permissions (should be readable)
- Try accessing with full URL

## Rollback Procedure

If something goes wrong:

1. **Restore database backup:**
   ```bash
   mysql -u your_username -p your_database < backup_YYYYMMDD.sql
   ```

2. **Restore original files:**
   - Replace modified `user_access.php` with original
   - Replace modified `include/class.user.php` with original

## Support

If you need help:

1. Check `MIGRATION_INSTRUCTIONS.md` for detailed information
2. Review `USERNAME_LOGIN_CHANGES_SUMMARY.md` for technical details
3. Run `test_username_login.php` to diagnose issues
4. Check your database error logs
5. Check PHP error logs

## Security Recommendations

After migration, consider implementing:

1. **Password hashing** - Currently passwords are stored in plain text
2. **Login attempt limiting** - Prevent brute force attacks
3. **Session security** - Regenerate session IDs after login
4. **Username validation** - Enforce username format rules
5. **HTTPS** - Ensure all login pages use SSL/TLS

## Timeline Estimate

- Database backup: 5 minutes
- Run migration: 2 minutes
- Populate usernames: 5 minutes
- Testing: 15 minutes
- **Total: ~30 minutes**

## Success Criteria

✓ Database has username column
✓ All accounts have unique usernames
✓ Login page shows "USERNAME" field
✓ Login works with username
✓ OTP email sent correctly
✓ Dashboard loads normally
✓ Account number still displays everywhere

## Questions?

Review the detailed documentation files:
- `MIGRATION_INSTRUCTIONS.md` - Complete migration guide
- `USERNAME_LOGIN_CHANGES_SUMMARY.md` - Technical details

---

**Remember:** Always test on a staging/development environment first before applying to production!
