Skip to content
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

Basic Auth For Production #294

Open
ashokdevatwal opened this issue Dec 14, 2023 · 2 comments
Open

Basic Auth For Production #294

ashokdevatwal opened this issue Dec 14, 2023 · 2 comments

Comments

@ashokdevatwal
Copy link

To view logs in production there must be a option to protect log route

ashokdevatwal added a commit to ashokdevatwal/laravel-log-viewer that referenced this issue Jan 3, 2024
@RogierVC
Copy link

RogierVC commented Jan 4, 2024

You can protect the route with, for example, Laravel's default auth middleware. This is outside the scope of this package.

@ashokdevatwal
Copy link
Author

@RogierVC Yes, absolutely right we can protect our route with Laravel's default auth middleware.

but i am using default auth for my website users

i want to give log route access only authorized admin of website.

can we built like this

config/logviewer.php

'username'       => env('LOGVIEWER_USERNAME', 'username'),
'password'       => env('LOGVIEWER_PASSWORD', 'password')

middleware/LogViewerBasicAuthMiddleware.php

<?php

  namespace Rap2hpoutre\LaravelLogViewer;

  use Closure;

  use Config;

  class LogViewerBasicAuthMiddleware
  {
      /**
       * Handle an incoming request.
       *
       * @param  \Illuminate\Http\Request  $request
       * @param  \Closure  $next
       * @return mixed
       */
        public function handle($request, Closure $next)
        {
            $username = Config::get('logviewer.username');
            $password = Config::get('logviewer.password');
    
            $givenUsername = $request->getUser();
            $givenPassword = $request->getPassword();
    
            if ($givenUsername !== $username || $givenPassword !== $password) {
                return response('Unauthorized.', 401, ['WWW-Authenticate' => 'Basic']);
            }
    
            return $next($request);
        }
   }

/Rap2hpoutre/LaravelLogViewer/LaravelLogViewerServiceProvider.php

  $router = $this->app->make(Router::class);
  $router->aliasMiddleware('auth.logviewer', LogViewerBasicAuthMiddleware::class);

Then can use

  Route::middleware('auth.logviewer')->get('logs', [\Rap2hpoutre\LaravelLogViewer\LogViewerController::class, 'index']);

Screenshot 2024-01-04 at 4 41 57 PM

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

2 participants