mardi 16 octobre 2018

Laravel 5.4 inconsistent no response and no error in logs

I have checked other similar questions but they do not resolve the issue.

I am using Angular JS with Laravel 5.4, sending a GET request and it is sometimes succeeding and sometimes failing. When I say failing I mean I am literally getting nothing back. I am getting no error and I can confirm laravel.log file is logging other errors but nothing for this.

In my console I have two examples that were done one after the other.

The request is called get-user-roles and the function is called at UserController@getUserRoles

Failed: enter image description here

Succeeded: enter image description here You can see in the second one I am getting blank data back as expected.

Here is my web.php in /routes

Route::group(
    [
        'middleware' => ['cors', 'switchdb'],

    ], function () {

    // auth

    Route::resource('authenticate', 'AuthenticateController', ['only' => ['index']]);
    Route::post('authenticate', 'AuthenticateController@authenticate');

    Route::group(
        [
            'middleware' => ['jwt.auth'],

        ], function () {
        Route::get('get-user-roles', 'UserController@getUserRoles');
    });

    Route::group(
        [
            'middleware' => ['jwt.auth', 'is-not-customer'],

        ], function () {
        // more functions
    });
});

Here is my controller UserController

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\User;
use App\Role;
use App\Http\Requests\PublishUserRequest;
use App\Http\Requests\ChangePasswordRequest;
use Auth;


class UserController extends Controller
{

    protected $user;

    public function __construct()
    {
        $this->user = new User();
    }

    public function getUserRoles()
    {
        try {
            $roles = $this->user->getPagesForRoles(Auth::user()->id);
            foreach ($roles as $key => $role) {
                if ($role['hasRole'] == 0) {
                    unset($roles[$key]);
                }
            }

            return response()->json([
                'data' => $roles
            ], 200
            );

        } catch (\Exception $e) {
            return response()->json([
                'message' => $e->getMessage(),
                'line' => $e->getLine()
            ], 400
            );
        }
    }
}

And my model code (App\User) for obtaining the roles...

public function getPagesForRoles($userID = false)
{
    if (!$userID) {
        $userID = Auth::user()->id;
    }

    $pagesForRoles = $this->role->getRoles();

    // check if user has role or not and add it to data
    if ($userID != 0) {

        foreach ($pagesForRoles as $key => &$role) {
            $hasRole = DB::table('user_roles')
                ->where('role_id', '=', $key)
                ->where('user_id', '=', $userID)
                ->get();

            if (isset($hasRole[0])) {
                if($key == 'admin') {
                    $hasAdminRole = 1;
                }
                $role['hasRole'] = 1;
            } else {
                $role['hasRole'] = 0;
            }
        }
    }

    if($userID == 1 || (isset($hasAdminRole) && $hasAdminRole == 1)) {
        $pagesForRoles = array(
            $pagesForRoles['admin']
        );
        $pagesForRoles[0]['hasRole'] = 1;
    }

    return $pagesForRoles;
}

getRoles() that is called in the model code simply returns a hardcoded array.

EDIT: Its worth mentioning that when I do die('test'); in my controller function I do get a response every time.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire