DarkGhostHunter/Laraguard

Class name must be a valid object or a string

Closed this issue · 10 comments

Installed as per instructions, getting the following error:

Error
Class name must be a valid object or a string

    $secret = $User->createTwoFactorAuth();

App/User.php

// 3rd Party Components

use \Spatie\Tags\HasTags;
use Spatie\Permission\Traits\HasRoles;
use DarkGhostHunter\Laraguard\TwoFactorAuthentication;
use DarkGhostHunter\Laraguard\Contracts\TwoFactorAuthenticatable;

class User extends Authenticatable implements TwoFactorAuthenticatable
{
use Notifiable;
use HasRoles;
use HasTags;
use TwoFactorAuthentication;

...

App/Http/Controllers/UserController.php

public function prepareTwoFactor(Request $request)
{
if( TRUE == ( $User = Auth::user() ) )
{
$secret = $User->createTwoFactorAuth();

    return view('user.enable2fa', [
        'as_qr_code' => $secret->toQr(),     // As QR Code
        'as_uri'     => $secret->toUri(),    // As "otpauth://" URI.
        'as_string'  => $secret->toString(), // As a string
    ]);
}
...

Put the stack trace

Found the error.

This is not a package error, you seems to have edited your config and the Model is not being found. You should be able to debug further by checking if the class in laraguard.model exists.

I didn't edit the config file - to test, I went back to scratch, and redid the package install using just the following commands. Still getting the same error, and /config/laraguard.php doesn't exist - it's pulling the config from the vendor directory:

Error
Class name must be a valid object or a string

class UserController extends Controller
{

public function prepareTwoFactor(Request $request)
{
> $secret = $request->user()->createTwoFactorAuth();

Command list

composer require darkghosthunter/laraguard
composer update
composer dumpautoload
php artisan migrate
vi app/User.php
vi app/Http/Controllers/UserController.php
mkdir resources/views/user/
vi resources/views/user/2fa.blade.php
vi routes/web.php

Sorry to keep blowing this up - if I dump out class_implements and test with interface_exists, both show that DarkGhostHunter\Laraguard\Contracts\TwoFactorAuthenticatable is there.

Array
(
[Illuminate\Contracts\Auth\CanResetPassword] => Illuminate\Contracts\Auth\CanResetPassword
[Illuminate\Contracts\Auth\Access\Authorizable] => Illuminate\Contracts\Auth\Access\Authorizable
[Illuminate\Contracts\Auth\Authenticatable] => Illuminate\Contracts\Auth\Authenticatable
[Illuminate\Contracts\Support\Arrayable] => Illuminate\Contracts\Support\Arrayable
[ArrayAccess] => ArrayAccess
[Illuminate\Contracts\Support\Jsonable] => Illuminate\Contracts\Support\Jsonable
[JsonSerializable] => JsonSerializable
[Illuminate\Contracts\Queue\QueueableEntity] => Illuminate\Contracts\Queue\QueueableEntity
[Illuminate\Contracts\Routing\UrlRoutable] => Illuminate\Contracts\Routing\UrlRoutable
[DarkGhostHunter\Laraguard\Contracts\TwoFactorAuthenticatable] => DarkGhostHunter\Laraguard\Contracts\TwoFactorAuthenticatable
)
interface exists

Edit the vendor files and dump what does it gets, and the whole laraguard config just to be sure.

It SHOULD get the name of the class. For what the error says, it's getting anything but the class name.

PS: I'm talking about ddd() on the config on this line.

Weirdly, the laraguard config didn't appear at all in a dump of the application's config object.

I started a fresh laravel project and installed laraguard and it seems to be working fine now.

I have this too. It happened when updating dependencies. I was using v1.0.1, after update it is now at v1.4.0. But in the meantime, this commit took place:
a6daa79

And this commit changes the config file. And the old config file does not have a 'model' property, and the 'listener' is still set to true.

php artisan vendor:publish --provider="DarkGhostHunter\Laraguard\LaraguardServiceProvider" --tag="config" --force

did the trick

Thanks, I'll put it on the readme for upgrading.