I was asked to check if the user is logged in or not I have been given a sample code
public function getAllNfts(GetAllNfts $request): JsonResponse
{
$data = $request->getData();
$data['uuid'] = ($request->hasHeader('Authorization') && $request->header('Authorization')) ? $this->token_service->getUserUUIDFromToken($request) : null;
$requested_uuid = $request->getRequestedUUID();
return $this->nft_service->getAllNfts(uuid: $requested_uuid, data: $data);
}
But I do not have uuid for my code when I work on the admin and json is not used because it is in the admin panel This is my code
public function updateCollection(EditCollection $request):RedirectResponse
{
$request->validated();
$data = $request->all();
$this->collection_repository->updateCollection($data);
return redirect('collections');
}
THis is FOrm Request Validate EditCollection.php
<?php
namespace App\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
class EditCollection extends FormRequest
{
public function authorize()
{
return true;
}
public function rules()
{
return [
'title' => ['required', 'string','max:255', 'min:10', 'between:10,255'],
'status' => ['required','Boolean','size:1'],
];
}
}
How to check all requests to see if the user is authenticated And not contrary to the sample code
CodePudding user response:
I think you don't need to check for each request as long as you're using Laravel's Middleware. Take a look at this doc.
CodePudding user response:
You can try something like this $data->user_id = Auth::id();
and then import this statement, use Illuminate\Support\Facades\Auth;
CodePudding user response:
I think for your case you can use form request class to check before process that request
<?php
namespace App\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
class EditCollection extends FormRequest
{
public function authorize()
{
return auth()->check();
}
public function rules()
{
return [
'title' => ['required', 'string','max:255', 'min:10', 'between:10,255'],
'status' => ['required','Boolean','size:1'],
];
}
}
Add this lines to the all request that you need to validate if the user is logged
public function authorize()
{
return auth()->check();
}
I hope its helps you