FrozenNode / Laravel-Administrator

An administrative interface package for Laravel
http://administrator.frozennode.com/
MIT License
1.94k stars 503 forks source link

Auth for admin doesn't seem to work #969

Open aligator28 opened 8 years ago

aligator28 commented 8 years ago

Trying to implement authentication for frozennode backend, but can't. Using Laravel 5.2. In my config/administrator.php Auth::check() always returns false, even after successful login. (Installed default authentication in php artisan make:auth) This is a part of config/administrator.php

'permission'=> function()
    {
       //dd(Auth::check());
       return Auth::check(); 
    },

My app/Http/Controllers/Auth/AuthController.php added only property $redirectTo

/**
     * Where to redirect users after login / registration.
     *
     * @var string
     */
    protected $redirectTo = '/admin';

    /**
     * Create a new authentication controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->middleware('guest', ['except' => 'logout']);
    }
    ...

In my route.php there is nothing special:

Route::group(['middleware' => 'web'], function () {
    Route::auth();

    Route::resource('/', 'IndexController');
    Route::get('/news', 'NewsController@index');
    Route::get('news/{slug}', 'NewsController@show');
    Route::get('/personnel', 'PersonnelController@index');
    Route::get('/services', 'ServicesController@index');
    Route::get('/adress', 'IndexController@adress');
    Route::post('/question', 'IndexController@question');

});

End when I login, middleware "Auth" check the user and redirects to main frontend page "/" it is working app/Http/Middleware/RedirectIfAuthenticated.php


<?php

namespace App\Http\Middleware;

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

class RedirectIfAuthenticated
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @param  string|null  $guard
     * @return mixed
     */
    public function handle($request, Closure $next, $guard = null)
    {
        if (Auth::guard($guard)->check()) {
            return redirect('/');
        }
        return $next($request);
    }

}

Of course, I can change return redirect('/'); to return redirect('/admin');, but then I got an endless redirect, because in config/administrator.php Auth::check() always return false. Why? Cookies are enabled in browser. By the way in Laravel 5.0 everything works just fine (tried several minutes ago). Am I misunderstood something or Frozennode administrator doesn't work with laravel 5.2?

kdeit commented 8 years ago

I have the same problem.

patrickcarlohickman commented 8 years ago

I don't use this package, but I investigated the issue for this question on SO: http://stackoverflow.com/questions/34856389/laravel-frozennode-authcheck-always-return-false

Basically, the issue is that the FrozenNode routes are not inside the web middleware group. Because of this, the session is not available, and therefore Auth::check() will always be false.

All you need to do is add web to the middleware key in your administrator.php configuration.

return array(
    // ...
    'middleware' => array('web'),
    // ...
);
namreg commented 8 years ago

@patrickcarlohickman +