0
votes

I have an api in laravel and im using passport to authenticate.

On my config/auth.php file i have changed this so the api guard using passport and refers to my user provider

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

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

'providers' => [
   'users' => [
       'driver' => 'passport',
       'model' => App\Users::class,
   ],

Here is my App/user.php

<?php

namespace App;

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

class User extends Authenticatable
{
    use HasApiTokens;
    use Notifiable;

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'name', 'email', 'password',
    ];

    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = [
        'password', 'remember_token',
    ];

    public static function lookup($login)
    {
        return Self::where('email', $login)->first();
    }
}

Now when I want to log someone in, I run this code:

$newUser = new User([
           'name' => $login,
           'email' => $login,
           'password' => $password
       ]);

       $authUser = User::lookup($login);

       $tokenResult = $authUser->createToken('Personal Access Token');
       $token = $tokenResult->token;

       if ($remember)
       {
          $token->expires_at = Carbon::now()->addWeeks(1);
          $token->save();
       }

       return response()->json([
          'access_token' => $tokenResult->accessToken,
          'token_type' => 'Bearer',
          'expires_at' => Carbon::parse($tokenResult->token->expires_at)->toDateTimeString()
        ]);

This sends me back an access token just fine. What im trying to do is check if the user is logged in when doing other functions on the website. I am doing this like below in the routes file:

Route::get('me', 'Account@me')->middleware('auth:api');

So i want to get the infomation of whoever is logged in but it crashes at the middleware by giving me an error of 'Authentication user provider [passport] is not defined'

Any help is appriciated

1

1 Answers

1
votes

You have used a wrong user provider driver of passport, Instead use eloquent as this:

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

Also you have an extra s in the model name.