# Google OAuth Setup Guide

## Overview
RMBXchange now supports "Sign in with Google" functionality, allowing users to authenticate using their Google accounts.

## Features
- One-click Google authentication
- Automatic user creation for new Google users
- Secure OAuth 2.0 implementation
- Admin-configurable credentials
- Works for both Sign In and Sign Up

## Setup Instructions

### 1. Install Laravel Socialite
Run the following command to install Laravel Socialite:
```bash
composer require laravel/socialite
```

### 2. Configure Google Cloud Console

1. Go to [Google Cloud Console](https://console.cloud.google.com/)
2. Create a new project or select an existing one
3. Enable the Google+ API:
   - Go to "APIs & Services" > "Library"
   - Search for "Google+ API"
   - Click "Enable"

4. Create OAuth 2.0 Credentials:
   - Go to "APIs & Services" > "Credentials"
   - Click "Create Credentials" > "OAuth client ID"
   - Choose "Web application"
   - Add authorized redirect URI:
     ```
     http://localhost:8000/auth/google/callback
     ```
     For production, use:
     ```
     https://yourdomain.com/auth/google/callback
     ```
   - Click "Create"
   - Copy the Client ID and Client Secret

### 3. Configure in Admin Dashboard

1. Log in to the admin dashboard
2. Navigate to Settings (`/admin/settings`)
3. Scroll to "Google OAuth Configuration" section
4. Enter your Google Client ID
5. Enter your Google Client Secret
6. Click "Save Settings"

### 4. Test the Integration

1. Log out of your account
2. Go to the login page (`/my-account`)
3. Click "Continue with Google" button
4. Authorize the application
5. You should be redirected to the dashboard

## How It Works

### User Flow
1. User clicks "Continue with Google" on login/signup page
2. User is redirected to Google's authorization page
3. User grants permission to the application
4. Google redirects back to the callback URL
5. Application receives user information from Google
6. If user exists, they are logged in
7. If user is new, an account is created automatically
8. User is redirected to the dashboard

### Technical Implementation

**Routes:**
- `/auth/google` - Initiates OAuth flow
- `/auth/google/callback` - Handles OAuth callback

**Controller Methods:**
- `redirectToGoogle()` - Redirects to Google OAuth
- `handleGoogleCallback()` - Processes OAuth response

**Database:**
- Google OAuth users are stored in the same `users` table
- A random password is generated for OAuth users
- Email is automatically verified

## Security Features

1. **Dynamic Configuration**: OAuth credentials are loaded from database settings
2. **Secure Storage**: Client secrets are stored securely in the database
3. **Email Verification**: Google-authenticated users have verified emails
4. **Random Passwords**: OAuth users get secure random passwords
5. **Error Handling**: Comprehensive error handling for failed authentications

## Troubleshooting

### "Google login is not configured" Error
- Ensure you've added Google Client ID and Secret in admin settings
- Verify the credentials are correct
- Check that settings were saved successfully

### "Failed to authenticate with Google" Error
- Check that the redirect URI in Google Console matches exactly
- Ensure Google+ API is enabled
- Verify the Client ID and Secret are correct
- Check Laravel logs for detailed error messages

### Redirect URI Mismatch
- The redirect URI in Google Console must match exactly:
  ```
  http://localhost:8000/auth/google/callback  (development)
  https://yourdomain.com/auth/google/callback  (production)
  ```
- No trailing slashes
- Protocol (http/https) must match

## Production Deployment

### Update Redirect URI
1. Add production URL to Google Console authorized redirect URIs
2. Keep localhost URL for development
3. Test on production before removing development URL

### Environment Variables (Optional)
While credentials are stored in the database, you can also use environment variables:
```env
GOOGLE_CLIENT_ID=your-client-id
GOOGLE_CLIENT_SECRET=your-client-secret
```

## Benefits

### For Users
- Faster registration and login
- No need to remember another password
- Trusted authentication provider
- Automatic email verification

### For Administrators
- Reduced support requests for password resets
- Higher conversion rates
- Better user experience
- Configurable through admin panel

## API Endpoints

### Initiate OAuth
```
GET /auth/google
```
Redirects user to Google's authorization page.

### OAuth Callback
```
GET /auth/google/callback
```
Handles the OAuth callback from Google.
Parameters are automatically handled by Laravel Socialite.

## Database Schema

No additional tables required. Google users are stored in the existing `users` table:
- `name`: From Google profile
- `email`: From Google account
- `password`: Random secure password
- `email_verified_at`: Set to current timestamp

## Support

For issues or questions:
1. Check Laravel logs: `storage/logs/laravel.log`
2. Verify Google Console configuration
3. Test with a different Google account
4. Contact support if issues persist

## Future Enhancements

Potential improvements:
- Add Facebook OAuth
- Add GitHub OAuth
- Link multiple OAuth providers to one account
- OAuth account management in user dashboard
- Remember OAuth provider preference
