1

I have a controller with a store() method called from a Vue component. It works fine, as I can successfully pass data to my controller from the Vue component. But, I am trying to get the identity of the authenticated user, and for some reason, I can not do that inside of the controller method. What I am currently doing is checking if there is an authenticated user (which there should be, auth is set up, and when I check on the front end in a blade template auth()->check() returns true).

Controller

public function store(Request $request)
{
    $player = Player::create([
        'first_name' => $request['first_name'],
        'last_name' => $request['last_name'],
        'dob' => $request['dob'],
    ]);

    If (auth()->check()) {
        Mail::to(auth()->user())->send(new PlayerCreated($player));
    }
}

I am also importing Illuminate\Support\Facades\Auth, so it is not an issue of not having use of the Auth facade. But, for some reason, it is not working.

I am wondering if it has something to do with this being an API route? I can't seem to figure out why I still wouldn't be able to access the authenticated user in my controller. Any ideas on why this is happening and how to fix?

Karl Hill
  • 12,937
  • 5
  • 58
  • 95
develpr
  • 1,296
  • 4
  • 22
  • 42
  • 1
    Are you using the default auth configuration, or is a differently named configuration? – fubar May 08 '19 at 02:41
  • 2
    In your `config/auth.php` check your api guard. – Aditya Thakur May 08 '19 at 02:46
  • 2
    What is exactly the issue? `check()` returns `false` while you should still be logged in? Are you using api endpoint or not? Are you sending credentials (cookie/token) along your request? – Leonardo Rossi May 08 '19 at 04:44
  • which laravel version you are using ? – Ronak Chauhan May 08 '19 at 06:10
  • @AdityaThakur Ahhh I think I may see issue. API driver is set to token. – develpr May 08 '19 at 20:39
  • @LeonardoRossi I thought `Auth::check()` would return true in this circumstance. I am not sending any credentials, I figured that I didn't need to. But now seeing the API driver is set to token, it is probably expecting one and returning false because there is nothing. – develpr May 08 '19 at 20:41
  • If you are not sending any credentials within your request, it's expected that `auth()->check()` would return `false`. If you are inside the `api` routes, then yes, you have to send your token (in query parameter/post data/header...) – Leonardo Rossi May 09 '19 at 05:18

3 Answers3

0

Make sure you're loading "web" middleware on this route!

Route::group(['namespace' => 'Your namespace', 'middleware' => ['web']], function () {
    // your routes
});
Akm
  • 81
  • 3
0

In your app/config.php check the driver you are using for authentication.You seeem trying to call Auth facade to fetch user in an API controller method.

Instead, you'll have to parse the token that is sent with authorization header to fetch the authenticated user. This depends on your api authentication driver.

for example in JWT you can get user through:

$user = JWTAuth::parseToken()->authenticate()

or you can try

$user = Auth::guard('api')->user();
Aditya Thakur
  • 2,562
  • 2
  • 10
  • 21
-2

In your controller add at the top of controller file

use Auth;

then you can use auth use email like

$email = Auth::user()->email;

I am assuming you are using laravel5.5

Ronak Chauhan
  • 681
  • 1
  • 8
  • 22