Laravel Example

Authentication

Learn how to implement secure authentication systems in Laravel

Basic Authentication

Setup login and registration with Laravel Breeze

Installation

# Install Laravel Breeze
composer require laravel/breeze --dev

# Install and configure Breeze with Blade
php artisan breeze:install

# Install dependencies and build assets
npm install
npm run dev

# Run migrations
php artisan migrate

Routes

// routes/auth.php
use App\Http\Controllers\Auth\AuthenticatedSessionController;
use App\Http\Controllers\Auth\RegisteredUserController;

Route::middleware('guest')->group(function () {
    Route::get('register', [RegisteredUserController::class, 'create'])
        ->name('register');

    Route::post('register', [RegisteredUserController::class, 'store']);

    Route::get('login', [AuthenticatedSessionController::class, 'create'])
        ->name('login');

    Route::post('login', [AuthenticatedSessionController::class, 'store']);
});

Route::middleware('auth')->group(function () {
    Route::post('logout', [AuthenticatedSessionController::class, 'destroy'])
        ->name('logout');
});

Key Points

  • Laravel Breeze provides a complete starter kit
  • Includes login, registration, and password reset
  • Built with Blade templates for easy customization
  • Email verification included

API Authentication

Token-based authentication with Laravel Sanctum

Installation & Setup

# Install Sanctum
composer require laravel/sanctum

# Publish configuration and migrations
php artisan vendor:publish --provider="Laravel\Sanctum\SanctumServiceProvider"

# Run migrations
php artisan migrate

API Authentication

// routes/api.php
Route::post('/login', function (Request $request) {
    $credentials = $request->validate([
        'email' => ['required', 'email'],
        'password' => ['required'],
    ]);

    if (!Auth::attempt($credentials)) {
        return response()->json([
            'message' => 'Invalid credentials'
        ], 401);
    }

    $user = Auth::user();
    $token = $user->createToken('api-token')->plainTextToken;

    return response()->json([
        'token' => $token,
        'user' => $user
    ]);
});

Route::middleware('auth:sanctum')->group(function () {
    Route::get('/user', function (Request $request) {
        return $request->user();
    });
});

Key Points

  • Sanctum provides lightweight API authentication
  • Support for SPA authentication
  • Token-based API authentication
  • Mobile application authentication

Social Authentication

OAuth authentication with Laravel Socialite

Installation & Setup

# Install Socialite
composer require laravel/socialite

Configuration & Implementation

// config/services.php
'github' => [
    'client_id' => env('GITHUB_CLIENT_ID'),
    'client_secret' => env('GITHUB_CLIENT_SECRET'),
    'redirect' => 'http://localhost:8000/auth/github/callback',
],

// routes/web.php
Route::get('/auth/github', function () {
    return Socialite::driver('github')->redirect();
});

Route::get('/auth/github/callback', function () {
    $githubUser = Socialite::driver('github')->user();

    $user = User::updateOrCreate([
        'github_id' => $githubUser->id,
    ], [
        'name' => $githubUser->name,
        'email' => $githubUser->email,
        'github_token' => $githubUser->token,
        'github_refresh_token' => $githubUser->refreshToken,
    ]);

    Auth::login($user);

    return redirect('/dashboard');
});

Key Points

  • Support for multiple OAuth providers
  • Store social tokens for API access
  • Automatic user registration/login
  • Customizable data storage