24

From within Tinker I am trying to:

>>> Auth::loginUsingId(12);
=> false
>>> Auth::user();
=> null
>>> Auth::attempt(['email' => 'my@email.com']);
=> 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?

ajon
  • 7,868
  • 11
  • 48
  • 86

1 Answers1

48

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' => 'my@email.com', 'password' => 'secret'])
Alexey Mezenin
  • 158,981
  • 26
  • 290
  • 279
  • 1
    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('my@email.com');` – ajon Jan 24 '18 at 16:32
  • `Auth::loginUsingId` is not working on Laravel 6+, but `Auth::attempt(['email', 'password' ])` is working fine. – Pranta Saha May 31 '20 at 10:28
  • 2
    `Auth::loginUsingId()` works for me in Laravel 8 – ahofmann Feb 19 '21 at 08:33
  • @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? – McAuley Aug 08 '21 at 19:30