Framework: assertAuthenticatedAs Fails when using uuid

Created on 4 Mar 2020  路  13Comments  路  Source: laravel/framework

  • Laravel Version: 6.17.1
  • PHP Version: 7.2.24
  • Database Driver & Version: Postgresql

Description:

I'm using an uuid for my User to authenticate. (I'm using ramsey/uuid-doctrine )
All works fine, but when i'm testing the authentification with the assertAuthenticatedAs methods, i got this error :
The currently authenticated user is not who was expected Failed asserting that 'd5ac944d-b245-4c95-8b4d-283579c1872b' is identical to an object of class "Ramsey\Uuid\Uuid".

I'm currently overriding 'assertAuthenticatedAs' to use assertEquals instead of assertSame to be able to pass my tests. I'm curious if it would be possible to change it directly in the framework here, or if i keep my overridden method :)

Thanks

bug

Most helpful comment

I'm gonna elevate this as a bug then because I'm out of ideas. Appreciating any help with this. Thanks for reporting.

All 13 comments

Can you share the test?

This is the test :

    /** @test */
    public function can_register_using_facebook_and_login()
    {
        $this
            ->json('POST', route('auth.login.facebook'))
            ->assertSuccessful();

        $customer = Customer::whereEmail('[email protected]')->first();
        $this->assertAuthenticatedAs($customer);
    }

And this is the controller

public function facebook(Request $request)
    {
        $facebookUser = $this->getFacebookUser();

        /** @var Customer $customer */
        $customer = Customer::whereEmail($facebookUser->email)->first();

        if (!$customer) {
            $customer = $this->createFromFacebook($facebookUser);
            event(new Registered($customer));
        }

        Auth::login($customer);
        return $this->respondWithToken($customer);
    }

It's probably best to also share the model. Thanks.

This is the model :

<?php

class Customer extends Authenticatable implements JWTSubject
{
    use Notifiable, SoftDeletes;

    protected $fillable = [
        'uuid',
        'full_name',
        'email',
        'password',
        'locale',
    ];

    protected $hidden = [
        'password',
        'remember_token',
    ];

    protected static function boot()
    {
        parent::boot();

        static::creating(function (Customer $customer) {
            $customer->uuid = Str::uuid();
        });
    }

    public function getJWTIdentifier()
    {
        return $this->uuid;
    }

    public function getAuthIdentifierName()
    {
        return 'uuid';
    }

    public function getJWTCustomClaims(): array
    {
        return [];
    }
}

And the migration to add the uuid:


class AddUuidToCustomers extends Migration
{
    public function up()
    {
        \Doctrine\DBAL\Types\Type::addType('uuid', 'Ramsey\Uuid\Doctrine\UuidType');

        Schema::table('customers', function (Blueprint $table) {
            $table->uuid('uuid')->unique()->nullable();
        });

        Customer::all()->each(function (Customer $customer) {
            $customer->uuid = Str::uuid();
            $customer->save();
        });

        Schema::table('customers', function (Blueprint $table) {
            $table->uuid('uuid')->nullable(false)->change();
        });
    }

    public function down()
    {
        Schema::table('customers', function (Blueprint $table) {
            $table->dropColumn('uuid');
        });
    }
}

Thanks a lot ;)

I see you haven't defined your keyType:

Screenshot 2020-03-05 at 14 14 07

Can you add that and try again?

Nop i'm still getting this error :
``The currently authenticated user is not who was expected Failed asserting that '2f1bc2c4-0a47-406c-8844-2c935bc07c98' is identical to an object of class "Ramsey\Uuid\Uuid".

@pa-bouly replace $customer->uuid = Str::uuid(); with $customer->uuid = Str::uuid()->toString();?

Neither :/

I'm gonna elevate this as a bug then because I'm out of ideas. Appreciating any help with this. Thanks for reporting.

Think I've found the problem here. Will send in a PR.

Any news about it @ryangjchandler ? :)

Neither :/

What error does it produces?

It works for me when using the regular uuid column type. We can't unfortunately give support for third party libraries so please post this on a support channel.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

gabriellimo picture gabriellimo  路  3Comments

YannPl picture YannPl  路  3Comments

fideloper picture fideloper  路  3Comments

PhiloNL picture PhiloNL  路  3Comments

lzp819739483 picture lzp819739483  路  3Comments