PHP code example of libaro / miqey-client

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

    

libaro / miqey-client example snippets


php artisan vendor:publish --provider="Libaro\MiQey\MiQeyServiceProvider" --tag="config"

return [
    'api_key' => env('MIQEY_API_KEY'),

    'webhook_secret' => env('MIQEY_WEBHOOK_SECRET'),

    'webhook_endpoint' => env('MIQEY_WEBHOOK_ENDPOINT'),

    'user_model' => env('MIQEY_USER_MODEL', '\App\Models\User'),
];

class SignSmsRequestReceived implements ShouldBroadcastNow
{
    use Dispatchable;
    use InteractsWithSockets;
    use SerializesModels;

    public function __construct(
        public string $code,
        public string $token
    ) {
    }

    /**
     * Get the channels the event should broadcast on.
     *
     * @return array
     */
    public function broadcastOn(): array
    {
        return ["signRequest_$this->code"];
    }

    public function broadCastAs(): string
    {
        return 'sign-request-received';
    }
}

class WebhookHandler
{
    public function __invoke(WebhookReceivedRequest $request)
    {
        $data = $request->validated();

        $signature = $request->header('Signature');

        $calculatedSignature = hash_hmac('sha256', $request->getContent(), config('miqey-login.webhook_secret'));

        if (! hash_equals($signature, $calculatedSignature)) {
            return response()->json(status: 401);
        }

        $phone = $data['phone'];
        $code = $data['code'];

        $token = Str::random(32);

        Cache::put($token, $phone, now()->addSeconds(10));

        event(new SignSmsRequestReceived($code, $token));

        $now = now();

        for($i = 0; $i < 10; $i++) {
            dispatch(function() use ($token, $code) {
                event(new SignSmsRequestReceived($code, $token));
            })->delay($now->addSeconds($i+1));
        }

        return response()->json(status: 204);
    }
}

class ValidationController extends Controller
{
    public function __invoke(Request $request)
    {
        $hasToken = Cache::has($request->get('token'));

        if (! $hasToken) {
            // todo: change to exception?
            abort(403, 'token mismatch');
        }

        $phoneNumber = Cache::pull($request->get('token'));

        $userClass = config('miqey.user_model');
        $user = $userClass::query()
            ->where('phone_number', '=', $phoneNumber)
            ->first();

        if (is_null($user)) {
            // todo: change to exception?
            abort(403, 'user not found');
        }

        Auth::login($user);

        return redirect()->to(App\Providers\RouteServiceProvider\RouteServiceProvider::HOME);
    }
}