• Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Register
  • Login
Bagisto Forum

Bagisto

  • Register
  • Login
  • Search
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups

make api with basic Authentication

Bug Report
2
8
718
Loading More Posts
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M
    mohammad hamayel last edited by 12 May 2022, 08:19

    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

    1 Reply Last reply Reply Quote 0
    • M
      mohammad hamayel last edited by 12 May 2022, 10:55

      it returns the login page code , not the api response

      1 Reply Last reply Reply Quote 0
      • M
        mohammad hamayel last edited by 12 May 2022, 11:21

        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

        1 Reply Last reply Reply Quote 0
        • M
          mohammad hamayel last edited by 15 May 2022, 09:32

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

          but still not working

          any updates ?

          1 Reply Last reply Reply Quote 0
          • devansh-webkul
            devansh-webkul last edited by 16 May 2022, 04:32

            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.

            1 Reply Last reply Reply Quote 0
            • M
              mohammad hamayel last edited by 16 May 2022, 07:13

              @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']);
              });

              1 Reply Last reply Reply Quote 0
              • devansh-webkul
                devansh-webkul last edited by 20 May 2022, 05:05

                On which package are you hitting, can you share the controller and route file as well.

                1 Reply Last reply Reply Quote 0
                • M
                  mohammad hamayel last edited by 22 May 2022, 07:20

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

                  On which package are you hitting, can you share the controller and route file as well.

                  controller: bagisto\packages\Webkul\Shop\src\Http\Controllers\OrderController.php

                  <?php
                  namespace Webkul\Shop\Http\Controllers;

                  use Webkul\Sales\Repositories\OrderRepository;
                  use Webkul\Sales\Repositories\InvoiceRepository;
                  use PDF;
                  use Illuminate\Http\Request;

                  class OrderController extends Controller
                  {
                  /**
                  * OrderrRepository object
                  *
                  * @var \Webkul\Sales\Repositories\OrderRepository
                  */
                  protected $orderRepository;

                  /**
                   * InvoiceRepository object
                   *
                   * @var \Webkul\Sales\Repositories\InvoiceRepository
                   */
                  protected $invoiceRepository;
                  
                  /**
                   * Create a new controller instance.
                   *
                   * @param  \Webkul\Order\Repositories\OrderRepository  $orderRepository
                   * @param  \Webkul\Order\Repositories\InvoiceRepository  $invoiceRepository
                   * @return void
                   */
                  public function __construct(
                      OrderRepository $orderRepository,
                      InvoiceRepository $invoiceRepository
                  )
                  {
                      $this->middleware('customer');
                  
                      $this->orderRepository = $orderRepository;
                  
                      $this->invoiceRepository = $invoiceRepository;
                  
                      parent::__construct();
                  }
                  
                  /**
                   * Display a listing of the resource.
                   *
                   * @return \Illuminate\View\View
                  */
                  public function index()
                  {
                      return view($this->_config['view']);
                  }
                  
                  /**
                   * Show the view for the specified resource.
                   *
                   * @param  int  $id
                   * @return \Illuminate\View\View
                   */
                  public function view($id)
                  {
                      $order = $this->orderRepository->findOneWhere([
                          'customer_id' => auth()->guard('customer')->user()->id,
                          'id'          => $id,
                      ]);
                  
                      if (! $order) {
                          abort(404);
                      }
                  
                      return view($this->_config['view'], compact('order'));
                  }
                  
                  public function discountCode()
                  {
                      if (true) {
                          session()->flash('success', __('shop::app.customer.account.order.index.order'));
                      } else {
                          session()->flash('error', trans('admin::app.response.cancel-error', ['name' => 'Order']));
                      }
                  
                      return redirect()->back();
                  }
                  
                  public function saveAttachments(Request $request)
                  {
                      return "saveAttachments in OrderController";
                  
                  }
                  

                  }

                  route file : bagisto\routes\api.php

                  <?php

                  use Illuminate\Http\Request;

                  use Illuminate\Support\Facades\Route;

                  use Webkul\Shop\Http\Controllers\OrderController;

                  use Illuminate\Support\Facades\Http;

                  // protected $namespace = 'packages\Webkul\Shop\Http\Controllers';
                  /*

                  API Routes
                  --------------------------------------------------------------------------
                  Here is where you can register API routes for your application. These
                  routes are loaded by the RouteServiceProvider within a group which
                  is assigned the "api" middleware group. Enjoy building your API!

                  |
                  */

                  Route::middleware(['basic.auth:api'])->group(function () {
                  //All the routes are placed in here

                  Route::post('/attachments', [OrderController::class, 'saveAttachments']);
                  

                  });

                  1 Reply Last reply Reply Quote 0
                  1 out of 8
                  • First post
                    1/8
                    Last post