Auth not working in Laravel Tinker
Asked Answered
B

1

27

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?

Bevel answered 24/1, 2018 at 16:23 Comment(0)
A
56

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'])
Alienee answered 24/1, 2018 at 16:25 Comment(4)
These are all good points. In fact this led me to find the answer. I have a custom UserProvider. I implemented retrieveById using the email as the identifier. I needed to call Auth::loginUsingId('[email protected]');Bevel
Auth::loginUsingId is not working on Laravel 6+, but Auth::attempt(['email', 'password' ]) is working fine.Brottman
Auth::loginUsingId() works for me in Laravel 8Alegre
@Pranta Saha Auth::loginUsingId(1) works for me in Laravel 7. It should have worked in Laravel 6 as well. Maybe you left out parenthesis after the 'loginUsingId' portion or left out a value inside of them?Broeder

© 2022 - 2024 — McMap. All rights reserved.