1
votes

I have error when do login in api by jwt ..when i do register its good will but in login display error

InvalidArgumentException: Auth guard [customers] is not defined. in file C:\Users\Ahmed\Desktop\project web\laravel_pro\vendor\laravel\framework\src\Illuminate\Auth\AuthManager.php on line 84

this is the controller

public function login(Request $request){
    $credentials = $request->only('email', 'password');

    if (Auth::guard('customer')->attempt($credentials))
    {
       $user= JWTAuth::guard('customer')->user(); 
       $jwt = JwtAuth::generateToken($user);

       return response()->json(compact('jwt'));

    }else{

        return response()->json(['error' => 'invalid_credentials'], 400);
    }

this is the api route

Route::post('login','UserController@login');

this is the auth.php

 'guards' => [
        'web' => [
            'driver' => 'session',
            'provider' => 'users',
        ],

        'api' => [
            'driver' => 'token',
            'provider' => 'users',
        ],

        'customer'=>[
          'driver' => 'token',
          'provider'=> 'customers',
        ]

    ],

'providers' => [
        'users' => [
            'driver' => 'eloquent',
            'model' => App\User::class,
        ],
        'customers' => [
            'driver' => 'eloquent',
            'model' => App\Customer::class,
        ],

please any body help my to Solution..Thank you

2
Try to run php artisan config:clear or php artisan config:cacheSehdev
i tried before aloes it's not working do you have another Solutionahmed aljabli
but after run php artisan config :cache ....the error change to Call to undefined method Illuminate\Auth\TokenGuard::attempt()ahmed aljabli
Change your driver from token to session 'driver' => 'session', After that run again php artisan cache:clear php artisan config:cacheSehdev
thank you i searched and i found the solution from token and change to session ..but now display new error invalid_credentials ..... does my code true or false ???ahmed aljabli

2 Answers

0
votes

First Change your guard's driver from token to session and then run the below commands

php artisan cache:clear
php artisan config:cache
0
votes

First guess: in config/auth.php the driver for your desired guard should be 'jwt' instead of 'token', because 'token' driver works against a fixed hash in the users table, as opposed to jwt that is volatile and doesn't query the DB to figure out the user.

Second guess : you're using the same controller for "internal users" ('api' guard), and "customer users" (should use 'customer' guard). This is confusing. Where are you discriminating the user type?

Regarding the error

Auth guard [customers] is not defined.

Check that it's not a typo and you always address it as 'customer', since 'customers' is not a guard but a provider. For example, check if the route middleware (e.g. Route::['middleware'=>'auth:customer']


Having said that...

I guess you installed tymondesigns/jwt-auth, whose configuration guide points to a scenario where you replace the api guard to use so, under that assumpion, make sure you've implemented the following modifications:

  1. If using Laravel 5.4 or below, you would need to follow these steps. On newer version they are handled for you so you can skip this step

  2. As I said above, in config/auth.php the driver for your desired guard should be 'jwt' instead of 'token', because 'token' driver works against a fixed hash in the users table, as opposed to jwt that is volatile and doesn't query the DB to figure out the user.

  3. You need to implement the JWTSubject interface on your Customer model (just add things, don't need to touch the other methods)

    use Tymon\JWTAuth\Contracts\JWTSubject;

    class Customer extends Authenticatable implements JWTSubject {

    /**
     * Get the identifier that will be stored in the subject claim of the JWT.
     *
     * @return mixed
     */
    public function getJWTIdentifier()
    {
        return $this->getKey();
    }
    
    /**
     * Return a key value array, containing any custom claims 
     * to be added to the JWT.
     *
     * @return array
     */
    public function getJWTCustomClaims()
    { 
        // just an example here, change it to whatever you want
        return [
           'picture' => 'picture', // array values are the fieldname in the DB
           'twitter' => 'twitter_url' 
        ];
    }
    ...
    
  4. Your controller should implement the corresponding guard in the constructor, and whitelist the login method since there is no JWT in the incoming headers.

    // UserController public function __construct() { $this->middleware('auth:users', ['except' => ['login']]); }

    public function login() { $credentials = request(['email', 'password']); // token is returned from attempt if (! $token = auth()->attempt($credentials)) { return response()->json(['error' => 'Unauthorized'], 401); } return response()->json([ 'access_token' => $token, 'token_type' => 'bearer', 'expires_in' => auth()->factory()->getTTL() * 60 ]); } ... other methods ...

I'd start by discarding the obvious:

  • are you still able to login as user (api guard)? if yo do, try to change the driver to 'jwt'