akawam/authenticate-as-anyone

Package that allows you to authenticate as anyone, see readme for more informations

1.2.2 2023-04-03 12:18 UTC

This package is auto-updated.

Last update: 2025-05-31 00:24:33 UTC


README

The purpose of this package is to allow restricted users to imitate another user account.

It is usefull when you want to see what exactly is happening on a certain user account.

Installation

You can install the package via composer:

composer require akawam/authenticate-as-anyone

You can publish the view files with:

php artisan vendor:publish --provider="Akawam\AuthenticateAsAnyone\AuthenticateAsAnyoneServiceProvider" --tag="views"

You can publish the config file with:

php artisan vendor:publish --provider="Akawam\AuthenticateAsAnyone\AuthenticateAsAnyoneServiceProvider" --tag="config"

This is the contents of the published config file:

return [
    'route-prefix' => 'authenticate-as-anyone', //required
    'middlewares' =>
        [
            'auth', //optional
        ],
    'models' =>
        [
            //required name of the Model
            'User' =>
                [
                    'namespace' => 'App\Models',//optional (default is App\Models)
                    'columns' => [
                        'name' => 'name', //optional (default is name)
                        'firstname' => 'firstname', //optional (default is firstname)
                        'login' => 'email',//optional (default is email)
                    ],
                ],
        ],
];

Usage

  • Include the "logged as" ribbon
...
<body>
@aaaLogged

...
</body>
</html>

Adding your own event

When connecting as a user, an event is triggered (UserIsSwitching)

If you want to add some magic to your authentication (like adding some sessions data) you can do so by adding your own event listener like so :

// 'app/Providers/EventServiceProvider.php'
class EventServiceProvider extends ServiceProvider
{
    /**
     * The event listener mappings for the application.
     *
     * @var array
     */
    protected $listen = [
        UserIsSwitching::class => [
            TestListener::class
        ],
    ];

    /**
     * Register any events for your application.
     *
     * @return void
     */
    public function boot()
    {
        //
    }
}

Credits

License

The MIT License (MIT). Please see License File for more informations.