TuffRivers
6 months ago
1144
5
Laravel

Auth::user returning Generic user?

Posted 6 months ago by TuffRivers

For some reason my Auth::user is returning a generic user object, and not my user model which has my traits to createToken and i cant seem to figure out why?

Error Call to undefined method Illuminate\Auth\GenericUser::createToken()", \ my generic class obviously does not inherit the trait for HasApiToken

auth.php

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

        'users' => [
            'driver' => 'database',
            'model' => App\Models\User::class,
            'table' => 'users',
        ],
    ],

App\Models\User

<?PHP

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

class User extends Authenticatable  

{

    use HasApiTokens, Notifiable;

 /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'first_name','last_name', 'email', 'password','alerts','status','client_id'
    ];

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

    /**
     * The attributes that should be cast to native types.
     *
     * @var array
     */
    protected $casts = [
        'email_verified_at' => 'datetime',
    ];
     
    public $id;
    public $first_name;
    public $last_name;
    public $email;
    public $alerts;
    public $status;
    public $client_id;
    public $password;

}

and my auth controller

use App\Models\User;

public function login($credentials)
    {
         

            if(Auth::attempt($credentials)){

                 $user = Auth::user(); //returns generic user object with my table from database with no traits for createtoken, error 

		$user = new User; //returns an empty user object but a real model, and returns a token

                $token =  $user->createToken('token')->accessToken; 

            return $token;

        }
        
        else {
 
            $error = [

                'error' => 'Invalid Email or Password.'
                
            ];
            
            return $error;
        }

Please sign in or create an account to participate in this conversation.