Laravel 5 new auth: Get current user and how to implement roles?

Laravel 5 new auth: Get current user and how to implement roles?

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Laravel 5 Auth: Retrieving Users and Implementing Roles

When migrating to or adopting Laravel 5, one of the first hurdles developers encounter is retrieving the authenticated user within a controller context. The pattern you identified—injecting the Guard interface directly into your controller actions—is technically valid and functional. However, in the broader ecosystem of modern PHP frameworks, this approach often signals a misunderstanding of how Laravel handles service providers and dependency injection.

In Laravel 5, the Auth facade provides direct access to the guard without requiring you to instantiate it manually or pass it as a constructor argument. While injecting Guard makes your code testable in isolation, it creates a tight coupling to the authentication system that is unnecessary for standard controller logic. The idiomatic Laravel way is to use the auth() helper function or the Auth facade. This keeps your controllers clean and leverages the framework's built-in resolution of the guard instance from the application context.

To retrieve the current user, you simply call auth()->user(). This returns a model instance representing the logged-in user, or null if no one is authenticated. If you need to check for authentication before proceeding, auth()->check() or $request->user() are equally effective alternatives. Using these helpers reduces boilerplate and aligns with the convention over configuration philosophy that Laravel champions.

Implementing roles and permissions is where things get interesting. Laravel 5 does not include a built-in role management system; the framework provides the authentication layer, but the authorization logic must be defined by you. The industry standard for this in the Laravel ecosystem is middleware. Middleware acts as a gatekeeper, intercepting requests before they reach your controller and deciding whether to proceed or redirect based on specific criteria.

To implement granular permissions like client.edit or client.add, you need a permission checking mechanism. A common strategy involves creating a custom middleware that resolves the required permission string from the route or controller action and verifies it against the user's roles or attributes.

Here is how you might structure a basic permission middleware:

use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Gate; // Or direct DB check depending on implementation

class PermissionMiddleware
{
    public function handle($request, Closure $next)
    {
        $user = Auth::user();
        
        if (!$user) {
            return redirect()->route('login');
        }

        // Define your permissions here. 
        // In a real app, this data usually comes from a database table linked to users.
        $allowedPermissions = ['client.edit', 'client.add'];
        
        if (!in_array($request->route()->action['name'], $allowedPermissions)) {
            return abort(403);
        }

        return $next($request);
    }
}

While the code above is functional, a more robust approach typically involves storing permissions in a database table and querying them. You might define a route rule like Route::middleware('permission:client.edit')->get('/edit', 'ClientController@index');. This pattern decouples the permission logic from the controller, allowing you to manage access control centrally via routing configuration rather than cluttering every method with conditional checks.

Laravel also offers the Gate class for defining authorization policies, which are ideal for business logic rules (e.g., "Only managers can edit clients"). For simple role-based restrictions applied to routes or controllers, a custom middleware that checks user attributes is often the most direct solution.

If you prefer a pre-built solution that handles this complexity out of the box, LaravelCompany offers robust packages designed to streamline these exact workflows. Their solutions integrate seamlessly with Laravel 5's architecture, providing ready-to-use models for roles and permissions that save development time and reduce the risk of implementation errors. Exploring their offerings can help you decide if a dedicated package fits your specific authorization needs better than building a custom middleware from scratch.

Ultimately, moving away from injecting Guard into controllers and utilizing Laravel's helpers combined with a well-designed middleware layer will result in a more maintainable and secure application.

Our network