Laravel Socialite provides an expressive, fluent interface to OAuth authentication with various providers including Facebook, Twitter, Google, LinkedIn, GitHub, GitLab, Bitbucket, and Microsoft. In this example, we will create a simple login with Microsoft account functionality.
So, let’s follow the below steps to implement Laravel 12 Microsoft login functionality.
Step 1: Install Laravel 12
First, we need to install a fresh Laravel 12 application. If you already have Laravel 12 installed, you can skip this step. Run the following command to install Laravel 12:
composer create-project laravel/laravel example-app
Step 2: Install Socialite Package
In this step, we need to install the Laravel Socialite package. Socialite provides a simple way to handle OAuth authentication. Run the following command:
composer require laravel/socialite
Step 3: Create Microsoft App
Now we need to create a Microsoft Azure application to get the Client ID and Client Secret. Follow these steps:
Go to Azure Portal
Navigate to Azure Active Directory → App registrations → New registration
Enter application name (e.g., “Laravel Microsoft Login”)
Select “Accounts in any organizational directory and personal Microsoft accounts”
Add Redirect URI: http://localhost:8000/auth/microsoft/callback
Click Register
Copy the Application (client) ID
Go to Certificates & secrets → New client secret
Add a description and select expiry period
Copy the Client Secret Value (you won’t be able to see it again)
Step 4: Configure Microsoft Credentials
Open your .env file and add the following configuration:
MICROSOFT_CLIENT_ID=your-client-id
MICROSOFT_CLIENT_SECRET=your-client-secret
MICROSOFT_REDIRECT_URL=http://localhost:8000/auth/microsoft/callback
return [
// ... other services
'microsoft' => [
'client_id' => env('MICROSOFT_CLIENT_ID'),
'client_secret' => env('MICROSOFT_CLIENT_SECRET'),
'redirect' => env('MICROSOFT_REDIRECT_URL'),
'tenant' => env('MICROSOFT_TENANT', 'common'), // 'common', 'organizations', 'consumers', or tenant ID
],
];
Step 5: Create Migration for Users Table
We need to add additional columns to store Microsoft ID and token. Create a migration:
php artisan make:migration add_microsoft_fields_to_users_table
Update the migration file: database/migrations/xxxx_xx_xx_add_microsoft_fields_to_users_table.php
string('microsoft_id')->nullable()->after('email');
$table->string('microsoft_token')->nullable()->after('microsoft_id');
$table->string('microsoft_refresh_token')->nullable()->after('microsoft_token');
});
}
/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::table('users', function (Blueprint $table) {
$table->dropColumn(['microsoft_id', 'microsoft_token', 'microsoft_refresh_token']);
});
}
};
Run the migration:
php artisan migrate
Step 6: Update User Model
Update the User model to make the password field nullable and add the new fields to $fillable:
app/Models/User.php
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
class User extends Authenticatable
{
use HasFactory, Notifiable;
/**
* The attributes that are mass assignable.
*
* @var array<int, string>
*/
protected $fillable = [
'name',
'email',
'password',
'microsoft_id',
'microsoft_token',
'microsoft_refresh_token',
];
/**
* The attributes that should be hidden for serialization.
*
* @var array<int, string>
*/
protected $hidden = [
'password',
'remember_token',
'microsoft_token',
'microsoft_refresh_token',
];
/**
* Get the attributes that should be cast.
*
* @return array<string, string>
*/
protected function casts(): array
{
return [
'email_verified_at' => 'datetime',
'password' => 'hashed',
];
}
}
Step 7: Create Controller
Create a new controller to handle Microsoft authentication
php artisan make:controllerMicrosoftController
public function handleMicrosoftCallback()
{
try {
$microsoftUser = Socialite::driver('microsoft')->user();
$user = User::where('microsoft_id', $microsoftUser->id)
->orWhere('email', $microsoftUser->email)
->first();
if ($user) {
// Update existing user
$user->update([
'microsoft_id' => $microsoftUser->id,
'microsoft_token' => $microsoftUser->token,
'microsoft_refresh_token' => $microsoftUser->refreshToken,
]);
} else {
// Create new user
$user = User::create([
'name' => $microsoftUser->name,
'email' => $microsoftUser->email,
'microsoft_id' => $microsoftUser->id,
'microsoft_token' => $microsoftUser->token,
'microsoft_refresh_token' => $microsoftUser->refreshToken,
'password' => Hash::make(uniqid()), // Generate random password
]);
}
Auth::login($user);
return redirect()->intended('dashboard');
} catch (Exception $e) {
return redirect('auth/microsoft')->with('error', 'Unable to login with Microsoft. Please try again.');
}
}
}

Hi, this is a comment.
To get started with moderating, editing, and deleting comments, please visit the Comments screen in the dashboard.
Commenter avatars come from Gravatar.