I try to test the authentication with Laravel Passport and there is no way ... The error is in the capture of postman, at the moment I try to create a user with postman I miss the error of the capture.
User.php
class User extends Authenticatable
{
use HasApiTokens, Notifiable, SoftDeletes;
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'user',
'email',
'password',
'rol',
];
/**
* The attributes that should be hidden for arrays.
*
* @var array
*/
protected $hidden = [
'password',
'remember_token',
];
/**
* The attributes that should be mutated to dates.
*
* @var array
*/
protected $dates = [
'deleted_at'
];
}
api.php
use Illuminate\Http\Request;
/*
|--------------------------------------------------------------------------
| 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('auth:api')->get('/user', function (Request $request) {
return $request->user();
});
Route::namespace('Api')->group(function() {
Route::resource('users', 'UserController');
Route::resource('productos', 'ProductosController')
Route::resource('category', 'CategoriasController');
Route::resource('gender', 'GeneroController');
});
UserController.php
<?php
namespace App\Http\Controllers\Api;
use Illuminate\Http\Request;
use App\Http\Requests\UserStore;
use App\User;
use App\Http\Controllers\Controller;
use App\Http\Resources\UserCollection;
use App\Http\Resources\UserResource;
class UserController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \App\Http\Resources\UserCollection
*/
public function index()
{
return new UserCollection(
User::paginate()
);
}
/**
* Store a newly created resource in storage.
*
* @param \App\Http\Requests\UserStore $request
* @return \Illuminate\Http\Response
*/
public function store(UserStore $request)
{
$data = $request->all();
$data['password'] = bcrypt($data['password']);
$user = User::create($data);
return $this->show($user);
}
/**
* Display the specified resource.
*
* @param \App\User $user
* @return \App\Http\Resources\UserResource
*/
public function show(User $user)
{
return new UserResource($user);
}
/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param int $id
* @return \Illuminate\Http\Response
*/
public function update(Request $request, $id)
{
//
}
/**
* Remove the specified resource from storage.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function destroy($id)
{
//
}
}
UserStore.php
<?php
namespace App\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Rule;
class UserStore extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
return !auth()->guard('api')->guest();
}
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
return [
'user' => [
'required',
'string',
'unique:users,user',
'max:255',
],
'email' => [
'required',
'string',
'email',
'unique:users,email',
'max:255',
],
'password' => [
'required',
'string',
'max:255',
],
'rol' => [
'nullable',
Rule::in([
'user',
'admin'
]),
],
];
}
}
UserCollection.php
<?php
namespace App\Http\Resources;
use Illuminate\Http\Resources\Json\ResourceCollection;
class UserCollection extends ResourceCollection
{
/**
* Transform the resource collection into an array.
*
* @param \Illuminate\Http\Request $request
* @return array
*/
public function toArray($request)
{
return $this->collection->map(function ($user) {
return [
'id' => $user->id,
'user' => $user->user,
'email' => $user->email,
'rol' => $user->rol,
'created_at' => $user->created_at,
'updated_at' => $user->updated_at,
];
});
}
}
UserResource.php
<?php
namespace App\Http\Resources;
use Illuminate\Http\Resources\Json\JsonResource;
class UserResource extends JsonResource
{
/**
* Transform the resource into an array.
*
* @param \Illuminate\Http\Request $request
* @return array
*/
public function toArray($request)
{
return [
'id' => $this->id,
'user' => $this->user,
'email' => $this->email,
'rol' => $this->rol,
'created_at' => $this->created_at,
'updated_at' => $this->updated_at,
];
}
}
Postman tests