Skip to main content

Laravel (PHP) Integration Guide

This guide provides a production-ready example of how to integrate Mubarokah ID OAuth 2.0 authentication into a Laravel application. It covers creating a service provider, handling OAuth callbacks, and managing user tokens.

Prerequisites

  • Laravel Framework installed.
  • Composer for package management.
  • Guzzle HTTP Client (or similar): composer require guzzlehttp/guzzle
  • Basic understanding of Laravel Service Providers, Controllers, and Eloquent.

Configuration

First, add your Mubarokah ID client credentials and settings to your config/services.php file and .env file. .env example:
config/services.php:

Mubarokah OAuth Service

Create a service class to handle the OAuth logic. app/Services/MubarokahOAuthService.php:
Create a custom exception App\Exceptions\MubarokahOAuthException.php if you want more specific error handling.

Auth Controller

Create a controller to handle the redirect to Mubarokah ID and the callback. app/Http/Controllers/Auth/MubarokahController.php:

Token Storage (MubarokahToken Model & Migration)

Create a model and migration to store Mubarokah ID tokens. Migration:
Modify the generated migration file:
Run php artisan migrate. app/Models/MubarokahToken.php:
Ensure your APP_KEY in .env is set correctly as it’s used for encryption.

Routes

Add routes for initiating login and handling the callback. routes/web.php:

Usage Example

Add a “Login with Mubarokah ID” button to your login page:
This comprehensive setup provides a secure way to integrate Mubarokah ID OAuth into your Laravel application, including token management and user provisioning.