Search code examples
laravellaravel-5laravel-5.3tinker

Auth not working in Laravel Tinker


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?


Solution

  • 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'])