Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Impersonate stopped working after Laravel update? #10

Closed
seabasss opened this issue Mar 29, 2022 · 10 comments
Closed

Impersonate stopped working after Laravel update? #10

seabasss opened this issue Mar 29, 2022 · 10 comments

Comments

@seabasss
Copy link

seabasss commented Mar 29, 2022

Impersonate stopped working after I updated Laravel. I was on Laravel 9.3.1 and it worked and after updating to 9.6.0 I just get logged out when I click on the impersonate button. Any ideas why? Impersonate, Filament and Jetstream are all on latest versions.

I found two things that seems to "fix" this. One is removing this line from App\Http\Kernel.php ... \Laravel\Jetstream\Http\Middleware\AuthenticateSession::class, and the other is changing Route::middleware(['auth:sanctum', 'verified']) to Route::middleware(['auth:web', 'verified']) in my routes file. Does anyone know the implications of doing any of those options?

Thank you!

@MACscr
Copy link

MACscr commented Mar 29, 2022 via email

@seabasss
Copy link
Author

No error. In Filament I go to users and then click the impersonate button and I just get logged out and end up on the Jetstream log in page. It used to work fine before I updated Laravel. It seems like the issues started around Laravel v9.5.0.

See possibly related issues:
404labfr/laravel-impersonate#154
laravel/framework#41447

@seabasss
Copy link
Author

seabasss commented Mar 30, 2022

Exact same thing happens in a fresh app. Steps to reproduce:

Install Laravel:
composer create-project laravel/laravel impersonate
Install Jetstream:
composer require laravel/jetstream
Install Livewire stack with Teams:
php artisan jetstream:install livewire --teams
Set up database and change env file accordingly.

Run:

npm install                                        
npm run dev
php artisan migrate

Install Filament:
composer require filament/filament
Install Impersonate:
composer require stechstudio/filament-impersonate
Run for the sake of it:
php artisan optimize:clear
Add two users to the database

Add ListUsers.php:

<?php

namespace App\Filament\Resources\UserResource\Pages;

use App\Filament\Resources\UserResource;
use Filament\Resources\Pages\ListRecords;

class ListUsers extends ListRecords
{
    protected static string $resource = UserResource::class;
}

Add UserResource.php:

<?php

namespace App\Filament\Resources;

use Filament\Forms;
use App\Models\User;
use Filament\Tables;
use Filament\Resources\Form;
use Filament\Resources\Table;
use Filament\Resources\Resource;
use STS\FilamentImpersonate\Impersonate;
use App\Filament\Resources\UserResource\Pages;

class UserResource extends Resource
{
    protected static ?string $model = User::class;

    protected static ?string $navigationIcon = 'heroicon-o-users';

    public static function form(Form $form): Form
    {
        return $form
            ->schema([
                Forms\Components\TextInput::make('name')->required(),
            ]);
    }

    public static function table(Table $table): Table
    {
        return $table
            ->columns([
                Tables\Columns\TextColumn::make('name'),
            ])
            ->prependActions([
                Impersonate::make('impersonate'),
            ]);
    }

    public static function getRelations(): array
    {
        return [
            //
        ];
    }

    public static function getPages(): array
    {
        return [
            'index' => Pages\ListUsers::route('/'),
            'edit' => Pages\EditUser::route('/{record}/edit'),
        ];
    }
}

Log in to Filament as one of the users and click on Users in the menu and then the impersonate symbol.
You're redirected to / and if you click on Dashboard at the top right you end up on the log in page and your user_id is nulled in the sessions table. Changing web.php from auth:sanctum to auth:web fixes this, but question is if there's any way to keep using sanctum and have it play with impersonate? Thank you!

@jszobody
Copy link
Member

Is this still an issue?

@wit3
Copy link

wit3 commented Aug 5, 2022

Is this still an issue?

yes!

@ziming
Copy link

ziming commented Oct 12, 2022

impersonate suddenly stopped working for me as well

@pschilly
Copy link

Fresh install - also not working after following all directions to a T.

@timrey
Copy link

timrey commented Dec 14, 2022

Any news on this?

I fixed it by adding
Event::listen(function(TakeImpersonation $event) {
session()->put('password_hash_sanctum', $event->impersonated->getAuthPassword());
});
in EventServiceProvider.php boot()
as mentioned here: 404labfr/laravel-impersonate#154

@jszobody
Copy link
Member

Is everyone using Sanctum in this thread?

I've been utterly unable to reproduce the issue myself. Fresh Laravel 10 app, fresh Filament install, and then installed this package 2.11 and it just works. I also went back and installed Jetstream with the livewire stack, and it also is working great.

I'm not seeing any issue with this package and latest versions of Laravel, at all.

Now there might be something we need to look at with Sanctum specifically, not sure. A few in this thread mention Sanctum. I'm not terribly familiar with that. Can someone provide more specific instructions on how to reproduce this, with a fresh Laravel app, using Sanctum?

If anyone else is still having problems with latest Laravel, please try to reproduce in a fresh app and provide details. If I can't reproduce, I can't see what the issue is and how to fix.

@jszobody
Copy link
Member

Alright, after a bunch of trial & error today I think I was able to reproduce what y'all are seeing with Sanctum. And 2.12 has been tagged with a fix.

Ultimately the issue needs to be resolved in the underlying 404labfr/laravel-impersonate package, they have multiple, lengthy tickets on this topic with currently no solution in place. I think my 2.12 update will work around the issue for now, please let me know if you still have problems logging out.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants