1

In my Laravel 5.8/vuejs 2.6 app I use "tymon/jwt-auth": "^1.0.0", and my app/Http/Controllers/AuthController.php has method:

public function login(Request $request)
{
    $credentials = $request->only('email', 'password');
    if ($token = $this->guard('api')->attempt($credentials)) {
        return $this->respondWithToken($token);
    }

    return response()->json(['error' => 'Unauthorized'], 401);
}

and I keep token on client side. It works but I want to add more checks on the server's side, when I save data and to make in control's method :

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use Auth;
use DB;
use Illuminate\Support\Facades\File;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Facades\Validator;

use App\Settings;
use App\Http\Traits\funcsTrait;
use App\Forum;
use App\ForumCategory;
use App\ForumThread;
use App\ForumPost;
use Illuminate\Routing\Controller as BaseController;

use App\User;
use App\library\CheckValueType;

use App\Http\Requests\ForumThreadRequest;
use JavaScript;


class ForumController extends BaseController
{
    use funcsTrait;

    public function __construct()
    {
        $this->middleware('auth');
    }


    public function add_new_thread(ForumThreadRequest $request)
    {

        $loggedUser = Auth::user();

        if ( empty($loggedUser->id) ) {
            return response()->json(['error_code'=> 1, 'message'=> "You must be logged!", 'forumThreadRow'=>null],HTTP_RESPONSE_INTERNAL_SERVER_ERROR);
        }
        try {

But even if I have logged into the system it looks like that in add_new_thread method $loggedUser is empty. Have I to make some additive actions in login method of AuthController.php or in which way ?

mstdmstd
  • 2,195
  • 17
  • 63
  • 140

2 Answers2

2

As I use api guard decision is to use :

$user = Auth::guard('api')->user();
mstdmstd
  • 2,195
  • 17
  • 63
  • 140
2

This is a late answer, but maybe could help someone.

I had the same issue and it was fixed by adding $table property to the user model User.php

/**
 * Specify table name otherwise Auth::user() will return null
 *
 * @var string
 */
protected $table = 'users';

see here

Angel M.
  • 2,692
  • 2
  • 32
  • 43