Skip to content

Commit

Permalink
Add social authentication support with Provider model and controller;…
Browse files Browse the repository at this point in the history
… update environment configuration for services
  • Loading branch information
abdessamadbettal committed Dec 16, 2024
1 parent dba00e3 commit e6749c4
Show file tree
Hide file tree
Showing 11 changed files with 141 additions and 20 deletions.
36 changes: 28 additions & 8 deletions .env.example
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
APP_NAME="Laravel Starter"
APP_ENV=local
APP_KEY=
APP_KEY=base64:ehyqVwvtOWhzrNYPVA8a1G3n65A5TntWK2SSwUucqRU=
APP_DEBUG=true
APP_TIMEZONE=UTC
APP_URL=http://localhost
Expand All @@ -23,11 +23,11 @@ LOG_DEPRECATIONS_CHANNEL=null
LOG_LEVEL=debug

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_HOST=mysql
DB_PORT=3306
DB_DATABASE=laravel_starter
DB_USERNAME=root
DB_PASSWORD=
DB_USERNAME=sail
DB_PASSWORD=password

SESSION_DRIVER=database
SESSION_LIFETIME=120
Expand All @@ -45,13 +45,13 @@ CACHE_PREFIX=
MEMCACHED_HOST=127.0.0.1

REDIS_CLIENT=phpredis
REDIS_HOST=127.0.0.1
REDIS_HOST=redis
REDIS_PASSWORD=null
REDIS_PORT=6379

MAIL_MAILER=log
MAIL_HOST=127.0.0.1
MAIL_PORT=2525
MAIL_MAILER=smtp
MAIL_HOST=mailpit
MAIL_PORT=1025
MAIL_USERNAME=null
MAIL_PASSWORD=null
MAIL_ENCRYPTION=null
Expand All @@ -69,3 +69,23 @@ VITE_APP_NAME="${APP_NAME}"
SETTINGS_CACHE_ENABLED=true

MEDIA_DISK="media"

# Google
GOOGLE_CLIENT_ID=
GOOGLE_CLIENT_SECRET=
GOOGLE_REDIRECT_URI=http://localhost/login/google/callback

#Github
GITHUB_CLIENT_ID=
GITHUB_CLIENT_SECRET=
GITHUB_REDIRECT_URI=http://localhost/login/github/callback

#Facebook
FACEBOOK_CLIENT_ID=
FACEBOOK_CLIENT_SECRET=
FACEBOOK_REDIRECT_URI=http://localhost/login/facebook/callback

#linkedin
LINKEDIN_CLIENT_ID=
LINKEDIN_CLIENT_SECRET=
LINKEDIN_REDIRECT_URI=http://localhost/login/linkedin/callback
11 changes: 11 additions & 0 deletions app/Http/Controllers/Auth/ProviderController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace App\Http\Controllers\Auth;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;

class ProviderController extends Controller
{
//
}
12 changes: 12 additions & 0 deletions app/Models/Provider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Provider extends Model
{
/** @use HasFactory<\Database\Factories\ProviderFactory> */
use HasFactory;
}
4 changes: 3 additions & 1 deletion bootstrap/app.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,12 @@
health: '/up',
)
->withMiddleware(function (Middleware $middleware) {
$middleware->append([
\Spatie\MissingPageRedirector\RedirectsMissingPages::class,
]);
$middleware->web(append: [
\App\Http\Middleware\HandleInertiaRequests::class,
\Illuminate\Http\Middleware\AddLinkHeadersForPreloadedAssets::class,
\Spatie\MissingPageRedirector\RedirectsMissingPages::class,
]);

$middleware->api(prepend: [
Expand Down
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
"laravel/framework": "^11.9",
"laravel/helpers": "^1.7",
"laravel/sanctum": "^4.0",
"laravel/socialite": "^5.16",
"laravel/tinker": "^2.9",
"league/commonmark": "^2.6",
"mcamara/laravel-localization": "^2.2",
Expand Down
4 changes: 2 additions & 2 deletions config/missing-page-redirector.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@
* You can use Laravel's route parameters here.
*/
'redirects' => [
// '/non-existing-page' => '/existing-page',
// '/old-blog/{url}' => '/new-blog/{url}',
// '/non-existing-page' => '/existing-page',
// '/old-blog/{url}' => '/new-blog/{url}',
],

];
20 changes: 20 additions & 0 deletions config/services.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,25 @@
'channel' => env('SLACK_BOT_USER_DEFAULT_CHANNEL'),
],
],
'github' => [
'client_id' => env('GITHUB_CLIENT_ID'),
'client_secret' => env('GITHUB_CLIENT_SECRET'),
'redirect' => env('GITHUB_REDIRECT_URI'),
],
'google' => [
'client_id' => env('GOOGLE_CLIENT_ID'),
'client_secret' => env('GOOGLE_CLIENT_SECRET'),
'redirect' => env('GOOGLE_REDIRECT_URI'),
],
'linkedin' => [
'client_id' => env('LINKEDIN_CLIENT_ID'),
'client_secret' => env('LINKEDIN_CLIENT_SECRET'),
'redirect' => env('LINKEDIN_REDIRECT_URI'),
],
'facebook' => [
'client_id' => env('FACEBOOK_CLIENT_ID'),
'client_secret' => env('FACEBOOK_CLIENT_SECRET'),
'redirect' => env('FACEBOOK_REDIRECT_URI'),
],

];
23 changes: 23 additions & 0 deletions database/factories/ProviderFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace Database\Factories;

use Illuminate\Database\Eloquent\Factories\Factory;

/**
* @extends \Illuminate\Database\Eloquent\Factories\Factory<\App\Models\Provider>
*/
class ProviderFactory extends Factory
{
/**
* Define the model's default state.
*
* @return array<string, mixed>
*/
public function definition(): array
{
return [
//
];
}
}
12 changes: 11 additions & 1 deletion database/migrations/0001_01_01_000000_create_users_table.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ public function up(): void
$table->date('date_of_birth')->nullable();
$table->text('address')->nullable();
$table->text('bio')->nullable();
$table->json('social_profiles')->nullable();
$table->string('avatar')->nullable();

$table->string('last_ip')->nullable();
Expand All @@ -42,6 +41,17 @@ public function up(): void
$table->timestamps();
});

Schema::create('providers', function (Blueprint $table) {
$table->id();
$table->foreignId('user_id')->constrained()->onDelete('cascade'); // Link to users table
$table->string('provider'); // e.g., 'google', 'github'
$table->string('provider_id'); // Provider's unique user ID
$table->string('provider_token')->nullable(); // Optional: Store access token
$table->timestamps();

$table->unique(['provider', 'provider_id']); // Ensure unique provider-user combination
});

Schema::create('password_reset_tokens', function (Blueprint $table) {
$table->string('email')->primary();
$table->string('token');
Expand Down
17 changes: 17 additions & 0 deletions database/seeders/ProviderSeeder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

namespace Database\Seeders;

use Illuminate\Database\Console\Seeds\WithoutModelEvents;
use Illuminate\Database\Seeder;

class ProviderSeeder extends Seeder
{
/**
* Run the database seeds.
*/
public function run(): void
{
//
}
}
21 changes: 13 additions & 8 deletions routes/auth.php
Original file line number Diff line number Diff line change
@@ -1,17 +1,22 @@
<?php

use App\Http\Controllers\Auth\AuthenticatedSessionController;
use App\Http\Controllers\Auth\ConfirmablePasswordController;
use App\Http\Controllers\Auth\EmailVerificationNotificationController;
use App\Http\Controllers\Auth\EmailVerificationPromptController;
use App\Http\Controllers\Auth\NewPasswordController;
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\Auth\PasswordController;
use App\Http\Controllers\Auth\PasswordResetLinkController;
use App\Http\Controllers\Auth\RegisteredUserController;
use App\Http\Controllers\Auth\ProviderController;
use App\Http\Controllers\Auth\NewPasswordController;
use App\Http\Controllers\Auth\VerifyEmailController;
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\Auth\RegisteredUserController;
use App\Http\Controllers\Auth\PasswordResetLinkController;
use App\Http\Controllers\Auth\ConfirmablePasswordController;
use App\Http\Controllers\Auth\AuthenticatedSessionController;
use App\Http\Controllers\Auth\EmailVerificationPromptController;
use App\Http\Controllers\Auth\EmailVerificationNotificationController;

Route::middleware('guest')->group(function () {
Route::get('login/{provider}', [ProviderController::class, 'redirectToProvider'])
->name('login.provider');
Route::get('login/{provider}/callback', [ProviderController::class, 'handleProviderCallback'])
->name('login.provider.callback');
Route::get('register', [RegisteredUserController::class, 'create'])
->name('register');

Expand Down

0 comments on commit e6749c4

Please sign in to comment.