Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Auth not working in Laravel Tinker

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?

like image 927
ajon Avatar asked Jan 24 '18 16:01

ajon


People also ask

How do I use Tinker in Laravel?

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:

Why is HTTP Basic authentication not working in my Laravel application?

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.

Can I use Laravel Auth without blade/Vue?

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.

When should I use Laravel sanctum for authentication?

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.


1 Answers

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'])
like image 76
Alexey Mezenin Avatar answered Oct 10 '22 17:10

Alexey Mezenin