Skip to content

Auth check return false in middleware, but should return true with Log Viewer #428

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
zekuraz opened this issue Mar 12, 2025 · 5 comments

Comments

@zekuraz
Copy link

zekuraz commented Mar 12, 2025

Hi !

I installed Log Viewer in my laravel 11 protect, and works like a charm. But i want to limit who has access to the Log Viewer in production.

So,i create a middleware

<?php

namespace App\Http\Middleware;

use Closure;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;

class ViewLogs
{
    public function handle(Request $request, Closure $next)
    {
        if (Auth::check() && Auth::user()->hasRole('admin')) {
            return $next($request);
        }
        abort(401, 'Unauthorised');
    }
}

Add the following to $middlewareAliases array In app\Http\Kernel.php

        'view-logs' => \App\Http\Middleware\ViewLogs::class,

And add middleware to the the log viewer config

    /*
    |--------------------------------------------------------------------------
    | Log Viewer route middleware.
    |--------------------------------------------------------------------------
    | Optional middleware to use when loading the initial Log Viewer page.
    |
    */

    'middleware' => [
        'web',
        'view-logs',
        \Opcodes\LogViewer\Http\Middleware\AuthorizeLogViewer::class,
    ],

If i try to debug Auth::check(), it's always return false.

I also try Auth::guard('api'), it's doesn't work.

For information, i use Laravel Passport in my project.

Thanks in advance for your help !

@iwasherefirst2
Copy link

iwasherefirst2 commented Mar 13, 2025

I experienced the same issue. I just updated logviewer to 3.15. (I am using Laravel 9) and after the composer upgrade, I received a 403 error. I downgraded back to 3.1.5 and the issue was gone. For some strange reason, the permission error only happens in production, not locally.

I also tried to remove my custom middleware, but still got a 403.

@zekuraz
Copy link
Author

zekuraz commented Mar 14, 2025

i note that the "XSRF-TOKEN" mismatch when i check the api request. Some can give a clue ?

@arukompas
Copy link
Contributor

Hey @zekuraz , is there any reason you cannot utilise Log Viewer's callbacks as written in the documentation?

For example, you could just add this to your AppServiceProvider:

public function boot()
{
    LogViewer::auth(function ($request) {
        // Always allow access locally
        if (App::environment('local')) {
            return true;
        }

        // Otherwise, check for the "admin" role.
        return Auth::check() && Auth::user()->hasRole('admin');
    });
}

@zekuraz
Copy link
Author

zekuraz commented Mar 24, 2025

Hi @arukompas ,

I also try add this to my AppServiceProvider, but it always returns to me false.

@Zephni
Copy link

Zephni commented May 1, 2025

@zekuraz After installing this in a bunch of different applications the most common reason I've found for this is that your APP_URL (in .env) or config('app.url') needs to be the exact domain you are using. Eg:

APP_URL=http://local.my-domain.com

... or of course whatever you are serving your site locally as.

Note that it says in the docs/config that if the log viewer config key 'log-viewer.route_domain' is set to null it should work on all domains but I've found that this isn't true.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants