1. Go to this page and download the library: Download emmanuel-saleem/social-auth library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
emmanuel-saleem / social-auth example snippets
namespace App\Models;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Laravel\Sanctum\HasApiTokens;
class User extends Authenticatable
{
use HasApiTokens, HasFactory, Notifiable;
protected $fillable = [
'name',
'email',
'password',
'google_id',
'microsoft_id',
'avatar',
'google_token',
'google_refresh_token',
'microsoft_token',
'microsoft_refresh_token',
'email_verified_at',
];
}
namespace App\Models;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Laravel\Passport\HasApiTokens; // Use Passport instead
class User extends Authenticatable
{
use HasApiTokens, HasFactory, Notifiable;
protected $fillable = [
'name',
'email',
'password',
'google_id',
'microsoft_id',
'avatar',
'google_token',
'google_refresh_token',
'microsoft_token',
'microsoft_refresh_token',
'email_verified_at',
];
}
use Illuminate\Support\Facades\Route;
Route::get('/login', function () {
return view('auth.login');
})->name('login')->middleware('guest');
Route::get('/dashboard', function () {
return view('dashboard');
})->name('dashboard')->middleware('auth');
// In tinker
User::latest()->first();
// Should show user with google_id, avatar, etc.
// Get current user
$user = Auth::user();
// Check if logged in
if (Auth::check()) {
echo "User is logged in: " . Auth::user()->email;
}
// Check login method
if ($user->google_id) {
echo "Logged in with Google";
}
if ($user->microsoft_id) {
echo "Logged in with Microsoft";
}
// Login page
GET /emmanuel-saleem/social-auth/login
// Google OAuth
GET /emmanuel-saleem/social-auth/google
GET /emmanuel-saleem/social-auth/google/callback
// Microsoft OAuth (future)
GET /emmanuel-saleem/social-auth/microsoft
GET /emmanuel-saleem/social-auth/microsoft/callback
// Logout
POST /emmanuel-saleem/social-auth/logout
// Google OAuth
GET /api/emmanuel-saleem/auth/google/url
POST /api/emmanuel-saleem/auth/google/callback
// Microsoft OAuth
GET /api/emmanuel-saleem/auth/microsoft/url
POST /api/emmanuel-saleem/auth/microsoft/callback
'labels' => [
'google_button' => 'Login with Google Account',
'microsoft_button' => 'Login with Microsoft Account',
],
html
<!-- Google button -->
<a href="{{ route('emmanuel-saleem.social-auth.google') }}" class="btn btn-google">
Sign in with Google
</a>
<!-- Microsoft button -->
<a href="{{ route('emmanuel-saleem.social-auth.microsoft') }}" class="btn btn-microsoft">
Sign in with Microsoft
</a>
html
<form action="{{ route('emmanuel-saleem.social-auth.logout') }}" method="POST">
@csrf
<button type="submit">Logout</button>
</form>