This project demonstrates how to implement Google OAuth authentication in a Laravel application using Laravel Socialite. It provides a simple and secure way to allow users to log in to your application using their Google accounts.
Before you begin, ensure you have the following installed:
- PHP >= 8.1
- Composer
- MySQL or any other compatible database
- Node.js and npm (for frontend assets)
- Google Cloud Console account
- Clone the repository:
git clone https://github.com/yourusername/social-auth-example.git
cd social-auth-example
- Install PHP dependencies:
composer install
- Install and compile frontend dependencies:
npm install
npm run dev
- Create a copy of the
.env
file:
cp .env.example .env
- Generate application key:
php artisan key:generate
- Configure your database in the
.env
file:
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=your_database_name
DB_USERNAME=your_database_username
DB_PASSWORD=your_database_password
- Run database migrations:
php artisan migrate
-
Go to the Google Cloud Console
-
Create a new project or select an existing one
-
Enable the Google+ API
-
Go to Credentials → Create Credentials → OAuth Client ID
-
Configure the OAuth consent screen
-
Create OAuth 2.0 Client ID credentials
-
Add your application URLs to the authorized redirect URIs:
http://localhost:8000/auth/google/callback
(for local development)https://yourdomain.com/auth/google/callback
(for production)
-
Add the following to your
.env
file:
GOOGLE_CLIENT_ID=your_client_id_here
GOOGLE_CLIENT_SECRET=your_client_secret_here
GOOGLE_REDIRECT_URI=http://localhost:8000/auth/google/callback
- Start the Laravel development server:
php artisan serve
- Visit
http://localhost:8000
in your browser
- Click on the "Login with Google" button
- Select your Google account
- Allow the requested permissions
- You will be redirected back to the application and logged in
Feel free to submit issues and enhancement requests.
This project is open-sourced software licensed under the MIT license.