Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# OAuth Configuration Environment Variables
# Copy this file to .env and update with your actual values

# Google OAuth Configuration
GOOGLE_CLIENT_ID=your_google_client_id_here
GOOGLE_CLIENT_SECRET=your_google_client_secret_here
GOOGLE_REDIRECT_URI=http://localhost:8080/oauth/callback.php

# Application Configuration
APP_URL=http://localhost:8080
APP_NAME="PHP API Seed with OAuth"

# Database Configuration (if needed)
DB_HOST=localhost
DB_PORT=3306
DB_NAME=appdb
DB_USER=root
DB_PASSWORD=rootpassword
26 changes: 26 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# Dependencies
/vendor/
composer.lock

# Environment variables
.env

# IDE files
.vscode/
.idea/
*.swp
*.swo

# OS files
.DS_Store
Thumbs.db

# Log files
*.log

# Cache files
cache/
tmp/

# Docker
docker-compose.override.yml
147 changes: 145 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,23 @@
# PHP Seed Project in Docker
# PHP Seed Project with OAuth Template

This is a simple PHP project designed to run in a **Docker** container. It uses **PHP-FPM**, **MySQL** (with **phpMyAdmin** for database management), and Docker Compose to set up the environment.
This is a simple PHP project designed to run in a **Docker** container with **Google OAuth integration**. It uses **PHP-FPM**, **MySQL** (with **phpMyAdmin** for database management), and Docker Compose to set up the environment.

## Features

- ✅ Simple PHP API structure
- 🔐 Google OAuth integration template
- 🐳 Docker containerization
- 🗄️ MySQL database with phpMyAdmin
- 📝 Ready-to-use OAuth endpoints
- 🎨 Demo frontend for testing OAuth flow

## Prerequisites

Before starting, make sure you have the following installed:

- **Docker**: [Install Docker](https://docs.docker.com/get-docker/)
- **Docker Compose**: [Install Docker Compose](https://docs.docker.com/compose/install/)
- **Composer**: [Install Composer](https://getcomposer.org/download/)

## Getting Started

Expand All @@ -16,3 +26,136 @@ Before starting, make sure you have the following installed:
```bash
git clone git@github.com:somashaker23/php-api-seed.git
cd php-api-seed
```

### 2. Install Dependencies:

```bash
composer install
```

### 3. Set Up OAuth Configuration:

#### 3.1 Create Google OAuth Credentials:

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** and **Google OAuth2 API**
4. Go to **Credentials** → **Create Credentials** → **OAuth 2.0 Client ID**
5. Configure the consent screen if prompted
6. Set application type to **Web application**
7. Add authorized redirect URI: `http://localhost:8080/oauth/callback.php`
8. Copy the **Client ID** and **Client Secret**

#### 3.2 Update Configuration:

Edit `src/config/oauth.php` and replace:
- `YOUR_GOOGLE_CLIENT_ID` with your actual Client ID
- `YOUR_GOOGLE_CLIENT_SECRET` with your actual Client Secret

```php
'client_id' => 'your-actual-client-id-here',
'client_secret' => 'your-actual-client-secret-here',
```

### 4. Start the Application:

#### Using Docker:

```bash
docker compose up --build
```

#### Using PHP built-in server:

```bash
php -S localhost:8080 -t src/public
```

## Usage

### OAuth Demo

Visit `http://localhost:8080/oauth-demo.html` to see the OAuth integration in action:

- **Demo Page**: `http://localhost:8080/oauth-demo.html`
- **Login**: `http://localhost:8080/oauth/login.php`
- **User Profile**: `http://localhost:8080/oauth/user.php`
- **Logout**: `http://localhost:8080/oauth/logout.php`

### API Endpoints

- **Main API**: `http://localhost:8080/` - Returns basic API response
- **OAuth Login**: `http://localhost:8080/oauth/login.php` - Redirects to Google OAuth
- **OAuth Callback**: `http://localhost:8080/oauth/callback.php` - Handles OAuth callback
- **User Profile**: `http://localhost:8080/oauth/user.php` - Returns authenticated user info
- **Logout**: `http://localhost:8080/oauth/logout.php` - Clears session

### Example OAuth Flow

1. **Login**: Visit `/oauth/login.php`
2. **Authenticate**: User logs in with Google
3. **Callback**: Google redirects to `/oauth/callback.php`
4. **Profile**: Access user data via `/oauth/user.php`
5. **Logout**: Clear session with `/oauth/logout.php`

## Development

### File Structure

```
src/
├── config/
│ └── oauth.php # OAuth configuration
├── oauth/
│ ├── login.php # OAuth login endpoint
│ ├── callback.php # OAuth callback handler
│ ├── user.php # User profile endpoint
│ └── logout.php # Logout endpoint
└── public/
├── index.php # Main API endpoint
└── oauth-demo.html # Demo frontend
```

### Adding OAuth to Your Project

1. **Include the OAuth files** in your project
2. **Update configuration** in `src/config/oauth.php`
3. **Start OAuth flow** by redirecting to `/oauth/login.php`
4. **Handle user data** from the session after authentication

### Session Management

User data is stored in PHP sessions after successful authentication:

```php
$_SESSION['user'] = [
'id' => $userInfo->getId(),
'email' => $userInfo->getEmail(),
'name' => $userInfo->getName(),
'picture' => $userInfo->getPicture(),
'verified_email' => $userInfo->getVerifiedEmail()
];
```

## Docker Services

- **PHP Application**: `http://localhost:8080`
- **phpMyAdmin**: `http://localhost:3000`
- **MySQL**: `localhost:3306`

## Troubleshooting

### Common Issues

1. **"OAuth not configured"** - Update `src/config/oauth.php` with your Google OAuth credentials
2. **"Invalid redirect URI"** - Make sure the redirect URI in Google Console matches exactly
3. **"Access denied"** - Check if the Google+ API is enabled in your Google Cloud project
4. **Composer dependencies missing** - Run `composer install`

### Security Notes

- Never commit real OAuth credentials to version control
- Use environment variables for production deployments
- Always validate the `state` parameter in OAuth callbacks
- Implement proper session management for production use
16 changes: 16 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"name": "somashaker23/php-api-seed",
"description": "PHP API Seed Project with OAuth Template",
"type": "project",
"require": {
"php": "^8.0",
"google/apiclient": "^2.15"
},
"autoload": {
"psr-4": {
"App\\": "src/"
}
},
"minimum-stability": "stable",
"prefer-stable": true
}
28 changes: 28 additions & 0 deletions src/config/oauth.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php
/**
* OAuth Configuration Template
*
* This file contains the OAuth configuration for Google OAuth integration.
* Replace the placeholder values with your actual Google OAuth credentials.
*/

return [
'google' => [
// Get these from Google Cloud Console: https://console.cloud.google.com/
'client_id' => 'YOUR_GOOGLE_CLIENT_ID',
'client_secret' => 'YOUR_GOOGLE_CLIENT_SECRET',
'redirect_uri' => 'http://localhost:8080/oauth/callback.php',

// OAuth scopes - what permissions to request
'scopes' => [
'openid',
'email',
'profile'
],

// OAuth endpoints
'authorization_base_url' => 'https://accounts.google.com/o/oauth2/v2/auth',
'token_url' => 'https://oauth2.googleapis.com/token',
'userinfo_url' => 'https://www.googleapis.com/oauth2/v2/userinfo'
]
];
Loading