0
votes

I am using laravel/passport and I want to authorize my API endpoint Then The result I'm receiving declares that a route(user/login) is not defined with status code 500 in postman

Route

Route::get('all/users', 'UserController@index')->middleware('auth');

Controller

    {  
        return new UserResource(User::findOrFail($id));
    } 

User Model

use Laravel\Passport\HasApiTokens;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;


class User extends Authenticatable
{
    use Notifiable, HasApiTokens;
    
    protected $fillable = [
        'name', 'email', 'password','balance','phone'
    ];

    
    protected $hidden = [
        'password', 'remember_token',
    ]; 
}```
1

1 Answers

0
votes

Route user/login declares in your application when you call Auth::routes() in your web.php file.

That error is returned because you are using api route and you have called auth middleware on it.

If you want to return just 401 unauthorized response when unauthorized user tries to access api you have to use auth middleware with api guard.

See example and correct code below:

Route::get('all/users', 'UserController@index')->middleware('auth:api');