PHP code example of devmahmoudmustafa / laravel-nafath
1. Go to this page and download the library: Download devmahmoudmustafa/laravel-nafath 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/ */
devmahmoudmustafa / laravel-nafath example snippets
// In EventServiceProvider
use Nafath\LaravelNafath\Events\NafathRequestCompleted;
use Nafath\LaravelNafath\Events\NafathRequestRejected;
protected $listen = [
NafathRequestCompleted::class => [
\App\Listeners\HandleNafathSuccess::class,
],
NafathRequestRejected::class => [
\App\Listeners\HandleNafathRejection::class,
],
];
use Nafath\LaravelNafath\Facades\Nafath;
use Nafath\LaravelNafath\DTO\CreateRequestDTO;
// Using DTO (optional)
$dto = CreateRequestDTO::fromArray([
'national_id' => '1234567890',
'locale' => 'ar',
]);
$result = Nafath::createRequest($dto);
if ($result->isSuccess()) {
$transId = $result->data['trans_id'];
$random = $result->data['random'];
// Show $random to user
// Save $transId and $random for status checking
}
// In EventServiceProvider
use Nafath\LaravelNafath\Events\NafathRequestCompleted;
use Nafath\LaravelNafath\Events\NafathRequestRejected;
protected $listen = [
NafathRequestCompleted::class => [
\App\Listeners\HandleNafathSuccess::class,
],
];
// In your Listener
namespace App\Listeners;
use Nafath\LaravelNafath\Events\NafathRequestCompleted;
class HandleNafathSuccess
{
public function handle(NafathRequestCompleted $event): void
{
$jwtPayload = $event->jwtPayload; // Decrypted user data
$nationalId = $jwtPayload['nationalId'] ?? null;
$fullName = $jwtPayload['fullName'] ?? null;
// Authenticate user, create session, etc.
$user = User::firstOrCreate(
['national_id' => $nationalId],
['name' => $fullName]
);
Auth::login($user);
}
}
// In your Listener (from Step 3)
public function handle(NafathRequestCompleted $event): void
{
$userData = $event->jwtPayload;
// Your business logic here
$user = User::firstOrCreate(
['national_id' => $userData['nationalId']],
['name' => $userData['fullName']]
);
// Generate session/token
Auth::login($user);
// Redirect or return response
return redirect('/dashboard');
}
use Nafath\LaravelNafath\Facades\Nafath;
use Nafath\LaravelNafath\DTO\RequestInfoDTO;
// Using DTO (optional)
$dto = RequestInfoDTO::fromArray([
'national_id' => '1234567890',
'trans_id' => $transId,
'random' => $random,
]);
$info = Nafath::requestInfo($dto);
if ($info->isSuccess() && isset($info->data['user_data'])) {
$userData = $info->data['user_data'];
// Use user data for UI updates
}
use Nafath\LaravelNafath\Facades\Nafath;
use Nafath\LaravelNafath\DTO\GetStatusDTO;
// Using DTO (optional)
$dto = GetStatusDTO::fromArray([
'national_id' => '1234567890',
'trans_id' => $transId, // From Step 1
'random' => $random, // From Step 1
]);
$status = Nafath::getStatus($dto);
if ($status->isSuccess()) {
$currentStatus = $status->data['status'];
switch ($currentStatus) {
case 'WAITING':
// User hasn't acted yet - show waiting message
break;
case 'COMPLETED':
// User approved - proceed with authentication
break;
case 'REJECTED':
// User rejected - show error message
break;
case 'EXPIRED':
// Request expired - show expired message
break;
}
}
// In EventServiceProvider
use Nafath\LaravelNafath\Events\NafathRequestCompleted;
protected $listen = [
NafathRequestCompleted::class => [
\App\Listeners\AuthenticateUser::class,
],
];
// In your Listener
use Nafath\LaravelNafath\Events\NafathRequestCompleted;
class AuthenticateUser
{
public function handle(NafathRequestCompleted $event): void
{
$userData = $event->jwtPayload;
// Your authentication logic
$user = User::firstOrCreate(
['national_id' => $userData['nationalId']],
['name' => $userData['fullName']]
);
Auth::login($user);
}
}