vendredi 7 juin 2019

Access Authenticated user from a package

I can not figure out how to make my package recognise the logged in user. All works fine when the controllers are in the main application but if try to move them in a package, authentication is not recognised.

The \app\Http\Middleware\RedirectIfAuthenticated.php keeps kicking me out.

I login in the main application with a guard "admin". I can navigate to all the pages of the application but as soon as I try to access a page from the package, I get kicked out.

I have added a few things in the controller construct to access the Auth::admin guard but nothing works... see the code below. I commented out some lines

\app\Http\Middleware\RedirectIfAuthenticated.php

public function handle($request, Closure $next, $guard = null)
    {

        if (Auth::guard($guard)->check())  //RETURNS NULL
        {

        if ($guard == "admin")
            {

                //user was authenticated with admin guard.
                return redirect()->route('admin.home');

            } else {

                //default guard.
                return redirect()->route('home');
            }

        }

        return $next($request);
    }

package routes

Route::prefix('/admin')->middleware('auth:admin')->name('admin.')->namespace('RF\RolesPermissions\Http\Controllers\Admin')->group(function(){

    Route::resource('admins', 'AdminController', ['except' => ['show'] ]);

});

Admin controller in the package

class AdminController extends Controller
{

//    protected $guard_name = 'admin';


    /**
     * Create a new controller instance.
     * 
     * @return void
     */
    public function __construct()
    {
        //Auth::shouldUse('admin');


        //$user = Auth::user();
        //dd($user);


        //dd(Config::get('auth'));

        //$this->middleware('auth');
    }

There are no errors. I just need

if (Auth::guard($guard)->check())  //RETURNS NULL

to recognise my authenticated user so I can access the policy files and detect permissions



from Newest questions tagged laravel-5 - Stack Overflow http://bit.ly/2I0bZe6
via IFTTT

Aucun commentaire:

Enregistrer un commentaire