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

How to use authentication middleware with this plugin? #69

Closed
lucassith opened this issue Jun 13, 2016 · 8 comments
Closed

How to use authentication middleware with this plugin? #69

lucassith opened this issue Jun 13, 2016 · 8 comments
Assignees
Labels
question Further information is requested

Comments

@lucassith
Copy link

Hi.

I am looking for the solution on how to authenticate user by adding middleware to the config file. I have probably the same problem as #63 has. According to the laravel docs

if(Auth::check()){

}

should be true if the user is logged in. This is working for each route in my routes.php but not if I use it with the log-viewer config.

@arcanedev-maroc
Copy link
Member

Hi @lucassith,

You don't need to repeat yourself by adding the Auth::check() for each route in your routes.php.

Try middleware instead, check the documentation for more details.

So you can apply the auth middleware to the log-viewer routes with the config file like this:

    'route'         => [
        'enabled'    => true,
        'attributes' => [
            'prefix'     => 'log-viewer',
            'middleware' => ['auth'],
        ],
    ],

@arcanedev-maroc arcanedev-maroc added the question Further information is requested label Jun 13, 2016
@arcanedev-maroc arcanedev-maroc self-assigned this Jun 13, 2016
@lucassith
Copy link
Author

lucassith commented Jun 13, 2016

@arcanedev-maroc thanks for your response. However it does not resolve my problem.

log-viewer.php

'route' => [
    'enabled' => env('APP_DEBUG', false),

    'attributes' => [
        'prefix' => 'admin/log-viewer',

        'middleware' => ['globaladmin'],
    ],
],

globaladmin middleware:

use Illuminate\Contracts\Auth\Guard;
use Auth;

class GlobalAdminMiddleware
{
    protected $auth;

    public function __construct(Guard $auth)
    {
        $this->auth = $auth;
    }

    public function handle($request, Closure $next)
    {
        if ($this->auth->guest() || !Auth::user()->is_admin) {
            if ($request->ajax()) {
                return response('Unauthorized.', 401);
            } else {
                return redirect()->route('login');
            }
        }
        if (Auth::getUser()->is_suspended) {
            Auth::logout();

            return redirect('/suspended');
        }

        return $next($request);
    }
}

Every time I go to the http://my.app/admin/log-viewer I am being redirected to the login and then to the main page because I am already logged in. I am global admin.

Even if I use the default auth middleware I am being redirected to the login -> main page.

And as I said, if I use this middleware inside my own routes it is working well.

@arcanedev-maroc
Copy link
Member

So, if you go to http://my.app/admin/log-viewer and you've been redirected to the login page, that means the middleware is working for the log-viewer routes.

You need to check your routes and your middleware(s) because i don't have this issue with any of my projects (LogViewer in backend side => Administration with auth middleware(s) ).

@lucassith
Copy link
Author

I was able to make it work.

In L5.2 middleware groups were introduced. In order to make Auth work you need to use "web" group which contains session and csrf middlewares.

Thank you @arcanedev-maroc for help and this great plugin.

@arcanedev-maroc
Copy link
Member

You're welcome 👍

@angoz
Copy link

angoz commented Aug 13, 2016

@lucassith I have the same problem as you in L5.2, but I was fixed it when I found this issue.
Thank you very much.

config/log-viewer.php Like this:

'route'         => [
    //...
    'attributes' => [
        //...

        'middleware' => ['web', 'auth'],
    ],
    //...
],

@cwilby
Copy link

cwilby commented Jul 6, 2021

Fun fact for anybody reading this in the future - you can re-use the gate in Laravel Nova for convenience.

...
'middleware' => ['nova']
...

@patelmm
Copy link

patelmm commented Mar 1, 2023

not working, any one have solution ?

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

No branches or pull requests

5 participants