Step 1: Install Laravel Project Run the following command to install a Laravel 8 project:
composer create-project --prefer-dist laravel/laravel ProjectManagementStudent "8.*"
Step 2: Update Composer
composer self-update --2
Step 3: Install Laravel/Socialite. Socialite is a Laravel package that handles OAuth logins. Install it using:
composer require laravel/socialite
Step 4: Setup Database Credentials
Create a new database and update the .env
file with your database details:
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=project_management_student
DB_USERNAME=root
DB_PASSWORD=your_database_password
Modify users
Migration file:
public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->string('role_id')->nullable();
$table->string('role_name')->nullable();
$table->string('provider_id')->unique()->nullable();
$table->string('avatar')->nullable();
$table->string('email')->unique();
$table->string('password')->nullable();
$table->timestamp('email_verified_at')->nullable();
$table->rememberToken();
$table->timestamps();
});
}
Before running migrations, update AppServiceProvider.php
:
Schema::defaultStringLength(191);
Run the migration:
php artisan migrate
Step 5: Define Routes
Route::get('/', 'App\Http\Controllers\Socialite\LoginController@redirectToProvider')->name('login.facebook');
Route::get('/callback', 'App\Http\Controllers\Socialite\LoginController@handleProviderCallback');
Step 6: Create LoginController. Run the command:
php artisan make:controller Socialite/LoginController
Modify LoginController.php
:
namespace App\Http\Controllers\Socialite;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Auth;
use Socialite;
use App\Models\User;
class LoginController extends Controller
{
public function redirectToProvider()
{
return Socialite::driver('facebook')->redirect();
}
public function handleProviderCallback()
{
try {
$user = Socialite::driver('facebook')->user();
$user = User::firstOrCreate([
'provider_id' => $user->getId(),
'name' => $user->getName(),
'email' => $user->getEmail(),
'avatar' => $user->getAvatar(),
]);
Auth::login($user, true);
return redirect()->route('home');
} catch (\Exception $e) {
return redirect()->back()->with('status', 'Authentication failed, please try again!');
}
}
}
Step 7: Update Login View
Modify resources/views/auth/login.blade.php
:
@extends('layouts.app')
@section('content')
<div class="login-form">
<form method="POST" action="{{ route('login') }}">
@csrf
<div class="text-center">
<img src="{{ URL::to('assets/images/logo_log.png') }}" alt="Logo" width="60" height="60">
</div>
<h1 class="h3 mb-0 text-center">Please sign in</h1>
@if(session()->has('error'))
<div class="alert alert-danger">{{ session()->get('error') }}</div>
@endif
<input type="email" class="form-control" name="email" placeholder="Email">
<input type="password" class="form-control" name="password" placeholder="Password">
<button type="submit" class="btn btn-primary login-btn">Signup</button>
<div class="text-center">
<p>Have an account? <a href="{{ route('register') }}">Signin</a></p>
</div>
<div class="or-separator"><i>OR</i></div>
<a href="{{ route('login.facebook') }}" class="btn btn-outline-secondary">Sign in with Facebook</a>
</form>
</div>
@endsection
Step 8: Configure Facebook Authentication.
Add Facebook credentials in config/services.php
:
'facebook' => [
'client_id' => env('FACEBOOK_KEY'),
'client_secret' => env('FACEBOOK_SECRET'),
'redirect' => env('FACEBOOK_REDIRECT_URI'),
],
Update the .env
file:
FACEBOOK_KEY=your_fb_id
FACEBOOK_SECRET=your_fb_secret_key
FACEBOOK_REDIRECT_URI=http://localhost:8000/callback