phplaravelauthenticationtokenlaravel-10

Laravel 10 problem Authentication Error: Call to undefined method Illuminate\Auth\GenericUser::createToken()


**I want to make authentication in laravel 10 but gives me error : Error: Call to undefined method Illuminate\Auth\GenericUser::createToken() **

my model for users is UserRoot :

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class UserRoot extends Model {
  use HasFactory;
}

Contoller:

<?php

namespace App\Http\Controllers\Api;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Http\Requests\UserRequest;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Validator;

class UserRoot extends Controller {
  public function login(UserRequest $request){
    $credentials = $request->validated();
    
    if (!Auth::attempt($credentials)) {
      return response([
        "msg"=>"Not connected"
      ]);
    } else {
      $user = Auth::user();
      $token = $user->createToken("mytoken")->plainTextToken;
      return response([
        "msg"=>"connected",
        "user"=>Auth()->user(),
        "status"=>"ok",
        "token"=>$token
      ]);
    };
  }
}

auth.php:

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

Please help me resolve this.


Solution

  • The error you're experiencing is because your UserRoot model is not using the correct authentication traits and doesn't inherit from the right base class. For Laravel's authentication and token generation to work properly, you need to use the Authenticatable base class and the HasApiTokens trait.

    Here's how you should modify your UserRoot model:

    <?php
    namespace App\Models;
    
    use Illuminate\Foundation\Auth\User as Authenticatable;
    use Illuminate\Database\Eloquent\Factories\HasFactory;
    use Laravel\Sanctum\HasApiTokens;
    
    class UserRoot extends Authenticatable 
    {
        use HasFactory, HasApiTokens;
    
        protected $fillable = [
            'name', 
            'email', 
            'password'
        ];
    
        protected $hidden = [
            'password',
            'remember_token'
        ];
    }
    

    Make sure that your migration follows the correct naming convention as your model name;

    Also, in your config/auth.php, you should update the users provider to point to your new UserRoot model:

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

    Next make sure you have Laravel Sanctum installed. If not, install it via Composer:

    composer require laravel/sanctum
    

    And publish its configuration:

    php artisan vendor:publish --provider="Laravel\Sanctum\SanctumServiceProvider"
    

    This should resolve the createToken() method not found error and allow you to generate tokens for authentication.