How to create a custom Auth controller in Laravel 5.5?

0

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();
    }
}
    
asked by anonymous 17.11.2017 / 02:31

1 answer

-2

Talk to my friend, good morning. Answering your question, in laravel 5.5 we have several types of tutorials to do this type of multiple authentication. I recommend you read this material that I used in a project my own (

14.03.2018 / 13:29