Recent Topics

make api with basic Authentication



  • Hello Bagisto

    may you tell me if I can make an api with basic authentication in bagito?

    I tried to make one using laravel documentation and without the authentication it worked fine but when I added the authentication it fails.

    it returns html code for the login page!!

    command : php artisan make:middleware AuthBasic

    AuthBasic class
    <?php

    namespace App\Http\Middleware;

    use Closure;
    use Illuminate\Support\Facades\Auth;

    class AuthBasic
    {
    /**
    * Handle an incoming request.
    *
    * @param \Illuminate\Http\Request $request
    * @param \Closure $next
    * @return mixed
    */
    public function handle($request, Closure $next)
    {
    if(Auth::onceBasic()){
    return response()->json(['message' => 'Auth Faild'],401);
    }else{
    return response()->json(['message' => 'Auth Faild'],401);
    // return $next($request);
    }
    }
    }

    kernal.php
    'api' => [
    'throttle:60,1',
    'bindings',
    // \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class,
    \App\Http\Middleware\AuthBasic::class,
    ],

    api.php

    Route::middleware('auth:api')->post('attachments', function(Request $request) {
    return response()->json("test test");
    });

    and i run these commands
    php artisan optimize
    php artisan config:cache

    ==> it looks like it doesn't reach the created middleware

    please your help

    thanks in advance



  • it returns the login page code , not the api response



  • when add - Accept: application/json - in the header it keep returns "error": "Unauthenticated"

    i tried customer and admin accounts for basic authentication but still not working



  • @mohammad-hamayel said in make api with basic Authentication:

    but still not working

    any updates ?



  • I am not getting what you are trying to do. As we are using the JWT package for authentication. If you are using it for authentication then maybe the other middleware is blocking it because the user is already unauthenticated.



  • @devansh-webkul said in make api with basic Authentication:

    JWT

    I made a middleware to handle the basic authentication and it worked fine, the authentication went very well, but now when I try to call a function inside bagisto's packages it returns the login page instead of the output of the function, looks like it need another authentication to reach the package's function!!

    what i need to do is to
    ==> call a function in Webkul\Shop\Http\Controllers\orderController from route\api.php

    orderController function :
    public function saveAttachments()
    {
    return "test request";
    }

    api.php route:

    Route::middleware(['basic.auth:api'])->group(function () {
    Route::post('/attachments', [OrderController::class, 'saveAttachments']);
    });


Log in to reply