How to use Auth :: user () - id na route?

0
| id | user_id | url_to | controller |  isAuthorized |
| 1  |    1    |  page1 | DadosCtrl  |       1       |  
| 2  |    2    |  page3 | Dados3Ctrl |       1       |  
| 3  |    1    |  page2 | Dados2Ctrl |       1       |  

I have a middleware that is only allowed to enter who is allowed (% with%). But I have to make a permission for a isAuthorized = 1 not to have access to user of the other

Route::group(['middleware' => ['auth', 'activated', 'pages.auth']], function(){

    echo Auth::user()->id; // ERRO : Trying to get property of non-object
    Route::get('pages', 'Pages\PagesController@index');
    $pages = Pages::where('isAuthorized', true)->get();
    //$pages = Pages::where('isAuthorized', true)->where('user_id', Auth::user()->id)->get(); 
    // precisaria do Auth::user()->id, mas ocorre um erro
    foreach($pages AS $p){
        $s = $p->page;
        Route::get($s->url_to.'/', "Pages\$s->controller@index");
        Route::get($s->url_to.'/{id}', "Pages\$s->controller@show");
    }
});

The user can only access the Controller for his and if he is authorized. You will not be able to access Controller of other Controller . for example:

  • If user : can have access to user_id => 1 Controller and DadosCtrl ; if it accesses Dados2Ctrl , it will not have access.
  • If Dados3Ctrl : may have access to user_id => 2 Controller ; if it accesses Dados3Ctrl and DadosCtrl , it will not have access.

You are returning a Dados2Ctrl error while using Trying to get property of non-object on the route.

    
asked by anonymous 12.05.2017 / 15:38

1 answer

0

Do you instantiate the facade for Auth :: ?

  

Using the facade:

use Illuminate\Support\Facades\Auth;
  

// receives the current user   :

 $user = Auth::user();
  

// get the id of the current user:

$id = Auth::id();

Reference - Authentication

-

I suggest using a Middleware to "filter" the user to a route.

reference - laravel middleware

* I did not write the code because I'm a bit rusty in laravel;

    
12.05.2017 / 16:11