'How to implement Laravel Auth to redirect users?

I am working with vue 3 and laravel and want to make certain urls in the admin dashboard unaccessible for users that are not logged in. The user can visit domain/admin url to find the login form. Once the correct credentials are entered, the user gets redirected to the /dashboard and has access to all other backend urls.

I've read through this question and am trying to implement the accepted answer.

My web.php looks like this:

// HTTPS REQUEST
Route::post('/login', [UserController::class, 'login']);

//BACKEND REDIRECTS
Route::any('/admin', function () {
    return view('cms');
})->name('admin');

Route::group(['middleware' => 'auth'], function()
{
    Route::any('/dashboard', function () {
        return view('cms');
    })->name('dashboard');

    Route::any('/dashboard/artwork', function () {
        return view('cms');
    })->name('artwork');

    Route::any('/dashboard/artwork/upload', function () {
        return view('cms');
    })->name('artwork-upload');

    Route::any('/dashboard/blog', function () {
        return view('cms');
    })->name('blog');

    Route::any('/dashboard/blog/compose', function () {
        return view('cms');
    })->name('blog-compose');

    Route::any('/dashboard/newsletter', function () {
        return view('cms');
    })->name('newsletter-cms');

    Route::any('/dashboard/newsletter/compose', function () {
        return view('cms');
    })->name('newsletter-compose');
});

And I have added this function to Http/Middleware/Authenticate.php:

public function handle($request, Closure $next)
{
    //check here if the user is authenticated
    if ( ! $this->auth->user() )
    {
        // here you should redirect to login
    }

    return $next($request);
}

Unfortunately I do not know how to proceed. Inside Authenticate.php:

  1. How do I check if the user is authenticated?

  2. How do I redirect to login? That would be /admin url in my case.

  3. PhpStorm marks $next in return $next($request); as an error and shows:

    Function name must be callable - a string, Closure or class implementing '__invoke', currently '\App\Http\Middleware\Closure'

Right now, when I try to open /dashboard the log shows this:

[2022-03-10 17:10:00] local.ERROR: Declaration of App\Http\Middleware\Authenticate::handle($request, App\Http\Middleware\Closure $next) must be compatible with Illuminate\Auth\Middleware\Authenticate::handle($request, Closure $next, ...$guards) {"exception":"[object] (Symfony\\Component\\ErrorHandler\\Error\\FatalError(code: 0): Declaration of App\\Http\\Middleware\\Authenticate::handle($request, App\\Http\\Middleware\\Closure $next) must be compatible with Illuminate\\Auth\\Middleware\\Authenticate::handle($request, Closure $next, ...$guards) at /Users/artur/PhpstormProjects/safa-ameedee.com/app/Http/Middleware/Authenticate.php:22)
[stacktrace]
#0 {main}
"} 


Solution 1:[1]

Include the Auth facade

use Illuminate\Support\Facades\Auth;

Then you can change it to this

    public function handle($request, Closure $next, $guard = null)
    {
        if (Auth::check()) {
            return redirect('/admin');
        }

        return $next($request);
    }

Also, I usually prefer to add middleware using the CLI php artisan make:middleware RedirectIfAuthenticated

Then you can add

auth' => \App\Http\Middleware\RedirectIfAuthenticated::class

to the $routeMiddleware array in the App\Http\Kernel.php file

And finally, run composer dumpautoload

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 WesleyNZ