I created a custom Auth controller in Laravel 5.5 with the action "store" inside it, then I authenticated using the auth->attempt()
method that returns true
. So far so good, the problem starts when I try to use the "auth" middleware for my panel routes, the authentication middleware always redirects to the login action.
Routes:
Route::group(['middleware' => ['auth', 'web']], function () {
Route::get('/painel/', ['as' => 'painel.dashboard', 'uses' => 'DashboardController@index']);
});
Route::get('/login', ['middleware' => 'web', 'as' => 'login', 'uses' => 'AuthController@index']);
Route::group(['middleware' => ['web']], function () {
Route::get('/painel/auth', ['as' => 'painel.auth.index', 'uses' => 'AuthController@index']);
Route::post('/painel/auth/store', ['as' => 'painel.auth.store', 'uses' => 'AuthController@store']);
});
Controller:
<?php
namespace App\Applications\Painel\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Contracts\Auth\Guard;
use Illuminate\Auth\AuthManager as Auth;
class AuthController extends BaseController
{
/**
* @var Guard
*/
private $auth;
public function __construct(Guard $auth)
{
$this->auth = $auth;
}
public function index()
{
return view('painel::auth.index');
}
public function store(Request $request)
{
$data = $request->only(['email', 'password']);
// This condition always return true, but after Laravel return to action index...
if ($this->auth->attempt($data)) {
return redirect()->route('painel.dashboard');
}
return redirect()->back();
}
}