PHP code example of ahmedebead / laramultiauth

1. Go to this page and download the library: Download ahmedebead/laramultiauth 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/ */

    

ahmedebead / laramultiauth example snippets


   AhmedEbead\LaraMultiAuth\LaraMultiAuthServiceProvider::class,
   



return [
    'guards' => [
        'web' => [
            'model' => App\Models\User::class,
            'authFields' => [
                'username' => ['email','name'], // any number of fields name to check
                'password' => 'password'
            ]
        ],
        // Add more guards and their respective models here
    ],

    'sms_helper_function' => env('SMS_HELPER_FUNCTION', 'sendSmsHelperFunction'),

];

// api is the guard you can change it to web or any another

$token = LaraMultiAuth::guard('api')->login([
    'username' => '[email protected]',
    'password' => 'password123',
]);

// api is the guard you can change it to web or any another

$user = LaraMultiAuth::guard('api')->register([
    'email' => '[email protected]',
    'password' => 'password123',
    //.....other fields
]);

//By phone or email
// api is the guard you can change it to web or any another
$identifier = "010548569847"; // can be email $identifier = "[email protected]";
$otp = LaraMultiAuth::guard('api')->generateOtp($identifier);

$isVerified = LaraMultiAuth::guard('api')->verifyOtp($identifier, $otp);

//By phone or email
// api is the guard you can change it to web or any another LaraMultiAuth::guard('api')
$otp = LaraMultiAuth::guard('api')->generateAndSendOtp('1234567890');

$otp = LaraMultiAuth::guard('api')->generateAndSendOtp('[email protected]');


// api is the guard you can change it to web or any another

$token = LaraMultiAuth::guard('api')->loggedInUser();

// api is the guard you can change it to web or any another

$token = LaraMultiAuth::guard('api')->logout();

// api is the guard you can change it to web or any another
//By phone or email
$token = LaraMultiAuth::guard('api')->forgetPassword($email);
$token = LaraMultiAuth::guard('api')->forgetPassword($phone);

//By phone or email
// api is the guard you can change it to web or any another
$data = [
    'identifier'=>'[email protected]', // can be phone number
    'password'=>'password'
    'otp'=>125487
];
$token = LaraMultiAuth::guard('api')->resetPassword($data);

// api is the guard you can change it to web or any another

$otp = LaraMultiAuth::guard('api')->generateOtp('Ahmed Ebead');


$data = [
    'identifier_field_name'=>'name'
    'identifier'=>'ahmed ebead',
    'password'=>'password'
    'otp'=>125487
];
$token = LaraMultiAuth::guard('api')->resetPassword($data);

// api is the guard you can change it to web or any another
$data = [
    'password'=>'password'
];
$token = LaraMultiAuth::guard('api')->resetPassword($data,false);



namespace App\Models;

use AhmedEbead\LaraMultiAuth\Models\BaseAuthModel;

class WebUser extends BaseAuthModel
{
    protected $guard = 'web';
}



namespace App\Models;

use AhmedEbead\LaraMultiAuth\Models\BaseAuthModel;

class ApiUser extends BaseAuthModel
{
    protected $guard = 'api';
}



namespace App\Models;

use AhmedEbead\LaraMultiAuth\Models\BaseAuthModel;

class AdminUser extends BaseAuthModel
{
    protected $guard = 'admin';
}

if (!function_exists('sendSmsHelperFunction')) {
    function sendSmsHelperFunction($phone, $otp)
    {
        // Implement the SMS sending logic
        // Example: using an external SMS service
        // SmsService::send($phone, "Your OTP is: {$otp}");
    }
}

if (!function_exists('sendSmsHelperFunction')) {
    function sendSmsHelperFunction($phone, $otp)
    {
        // Implement the SMS sending logic
        // Example: using an external SMS service
        // SmsService::send($phone, "Your OTP is: {$otp}");
    }
}
 

class OrderController extends Controller
{
    public function index()
    {
        sendSmsHelperFunction(....);
    }
}
bash
php artisan multiauth:setup
php artisan passport:install # If needed 

bash
   php artisan vendor:publish --provider="AhmedEbead\LaraMultiAuth\LaraMultiAuthServiceProvider"
   
json
{
    "files": [
        "app/Helpers/helper.php"
    ]
}