How to create a custom Auth controller in Laravel 5.5?

Asked

Viewed 1,753 times

0

I created a custom Auth controller in Laravel 5.5 with the "store" action inside it, then I authenticated using the method auth->attempt() returning true. So far so good, the problem starts when I try to use the "auth" middleware for my dashboard 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();
    }
}

1 answer

-2

This is my friend, good morning. To answer your question, in Aravel 5.5 we have several types of tutorials to do this type of multiple authentication. I recommend to give a read on this material that I used in a project my tbm (Independent authentications), it works with version 5.4, but if you adjust the structure a little, it will work well in 5.5.

Hugs, any questions come in contact:

contact: @brunogaignoux.com - http://brunogaignoux.com

  • While this link may answer the question, it is best to include the essential parts of the answer here and provide the link for reference. Replies per link only can be invalidated if the page with the link is changed. - Of Revision

  • @hugocsl Actually this answer is more like ad face.

  • @Leandroangelo can even be interpreted as spam even, but as it is within the subject of the tag, and Bruno is a member of STOF for more than 2 years I reviewed as "response per link" same....

  • @hugocsl Fact, my failure by contact data and website with resume.

  • Good friends, my intention was to really help, and as I work on two projects at the same time, I took a little time to help. Actually my site is a resume, but there are contact data beyond this email, in case you have doubts, since lately I have entered little here in STOF. I hope you understand that I am not trying to promote myself but help, here is a community open to doubts and not Linkedin, I mean, it would not make sense to try to promote myself in this environment. I hope you review your arguments, hugs! @Leandroangelo

Browser other questions tagged

You are not signed in. Login or sign up in order to post.