简体   繁体   中英

laravel 5.3 auth always redirect to login page

I want to create authentication on laravel, but i have a problem. when i want to access another page, the page has redirected to login page.

Here is my routes

Route::auth();
Route::get('/news/getid/{id_category}', 'NewsController@getid');
Route::group(['middleware' => ['web', 'auth']], function () {
    Route::resource('news', 'NewsController', ['except' => ['getid']]);
    Route::resource('category', 'CategoryController');
});

and here is my LoginController

protected $redirectTo = '/news';

public function username()
{
    return 'username';
}

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

You have to comment $this->middleware('auth'); line from constructor of your "NewsController". It should be

public function __construct()
{
// $this->middleware('auth');
}

Note: By commenting this line, Never check authentication for all view page that return from this controller.

Route::auth(); should be in web middleware group as auth routes use session to remember logged in user.

You need to remove this line in every page that you don't want to be protected by the authentication:

$this->middleware('auth');

so basically remove it from the "another page"s controller that you mentioned above.

In your Route file(Maybe web.php). Remove the auth middleware from the group Like this:

Route::group(['middleware' => ['web' , 'auth']], function () {
Route::resource('news', 'NewsController', ['except' => ['getid']]);
Route::resource('category', 'CategoryController');
});

to become like this:

Route::group('middleware' => 'web', function () {
Route::resource('news', 'NewsController', ['except' => ['getid']]);
Route::resource('category', 'CategoryController');
});

But be sure you really don't need to protect that route.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM