From within Tinker I am trying to:
>>> Auth::loginUsingId(12);
=> false
>>> Auth::user();
=> null
>>> Auth::attempt(['email' => '[email protected]']);
=> false
I am guessing that since Auth typically uses session data, and maybe sessions don't work with tinker.
Is it possible to authenticate within tinker?
All Laravel applications include Tinker by default. However, you may install it manually if needed using Composer: Tinker allows you to interact with your entire Laravel application on the command line, including the Eloquent ORM, jobs, events, and more. To enter the Tinker environment, run the tinker Artisan command:
By default, the auth.basic middleware will assume the email column on your users database table is the user's "username". If you are using PHP FastCGI and Apache to serve your Laravel application, HTTP Basic authentication may not work correctly.
You can still use Laravel Auth and its Controllers. Install Laravel UI package and run this: It will generate only app/Http/Controllers/Auth contents, so you don’t need Blade/Vue files to use them. See the implementation of this Artisan command in Github repository. Tip 3.
In general, Sanctum should be preferred when possible since it is a simple, complete solution for API authentication, SPA authentication, and mobile authentication, including support for "scopes" or "abilities". If you are building a single-page application (SPA) that will be powered by a Laravel backend, you should use Laravel Sanctum.
It's possible to login in Tinker. For example:
auth()->loginUsingId(1)
auth()->id()
Normally, the output of the auth()->id()
will be 1
.
If it doesn't work for you, make sure the storage
directory is writable:
sudo chmod -R 755 storage
You're also doing it wrong when you're using the attempt()
method. The correct syntax is:
attempt(['email' => '[email protected]', 'password' => 'secret'])
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With