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
<?phpnamespace 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
-
-
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.phporderController function :
public function saveAttachments()
{
return "test request";
}api.php route:
Route::middleware(['basic.auth:api'])->group(function () {
Route::post('/attachments', [OrderController::class, 'saveAttachments']);
}); -
On which package are you hitting, can you share the controller and route file as well.
-
@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 hereRoute::post('/attachments', [OrderController::class, 'saveAttachments']);
});