Authentication in the Laravel accessing routes protected by auth middleware

Asked

Viewed 305 times

-1

Good morning, I’m making a little project with Laravel, I’m at the authentication part and I had a question. i protected a group of routes with middleware auth so the user will only be able to access the route home if it authenticates first, but here comes the problem I can’t access this route within the auth with a route from outside of this group so it’s not directing to it so how will I access it? below the code:

View login

<!doctype html>
<html lang="en">
  <head>
    <title>Title</title>
    <!-- Required meta tags -->
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">

    <!-- Bootstrap CSS -->

    <link rel="stylesheet" href="{{asset('css/style.css')}}">
    <link rel="stylesheet" href="{{asset('css/style-form.css')}}">
    <script src="{{asset('jquery/jquery.js')}}"></script>
    <script src="{{asset('bootstrap/js/bootstrap.js')}}"></script>
  </head>
  <body class="bg-dark">
    <div class="container col-lg-4">
                    <br>
                    <br>
                    <br>
                    <br>
                    <br>
                    <br>
                    <br>
                    <br>
        <div class="card ml-auto mr-auto">
            <div class="card-header">   
                    <h5>Login</h5>
            </div>  
            <div class="card-body"> 
                <form action="{{route('entrar')}}" class="form-horizontal" method="post" enctype="multipart/form-data" accept-charset="UTF-8">
                    @csrf
                    <div class="form-group">
                        <div class="input-group mb-3">
                            <div class="input-group-prepend">
                                <span class="input-group-text" id="basic-addon1"><img src="{{asset('img/user-solid.svg')}}" width="20px" height="20px"></span>
                            </div>
                                <input type="text" class="form-control form-control-lg" name="email" placeholder="[email protected]" aria-label="Usuário" aria-describedby="basic-addon1">
                        </div>
                    </div>  
                    <div class="form-group">
                        <div class="input-group mb-3">
                            <div class="input-group-prepend">
                                    <span class="input-group-text" id="basic-addon1"><img src="{{asset('img/key-solid.svg')}}" width="20px" height="20px"></span>
                            </div>
                                <input type="password" name="password" id="senha" class="form-control form-control-lg" placeholder="***********" aria-label="Usuário" aria-describedby="basic-addon1">
                        </div>
                        <select class="form-control" name="cargo">
                        <option>Gerente</option>
                        <option>Funcionario</option>
                    </select>
                    </div>
                    <button type="submit" class="btn btn-primary btn-block">Entrar</button>

                </form>
            </div>      
        </div>       
    </div>
  </body>
</html>

Routes

Route::get('/', function () {
    return view('logar.login');
})->name('login');


Route::post('/login/entrar',['uses'=>'LoginController@entrar'])->name('entrar');

//Route::get('/teste',['uses'=>'LoginController@teste'])->name('teste');
Route::group(['middleware'=>'auth'],function(){
    Route::get('/home',['uses'=>'LoginController@entrar'])->name('home');
    Route::get('/logout',['uses'=>'LoginController@logout'])->name('logout');
    Route::get('/funcionario',['uses'=>'FuncionarioController@visualizar'])->name('funcionarioVisualizar');

    Route::get('/funcionario/adicionar',['uses'=>'FuncionarioController@adicionarFuncionario'])->name('adicionarFuncionario');

    Route::get('/produto',['uses'=>'ProdutoController@index'])->name('produto');

    Route::get('/produto/adicionar',['uses'=>'ProdutoController@adicionarProduto'])->name('adicionarProduto');
});

Login controller

 <?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;

class LoginController extends Controller
{
    public function entrar(Request $req)
    {
        $dados = $req->all();
        if (Auth::attempt(['email'=>$dados['email'],'password'=>$dados['password']])) {
            return redirect()->route('home');
        } else {
            return redirect()->route('login');
        }
    }
    public function logout()
    {
        Auth::logout();
    }
}

1 answer

0


You have two routes pointed to a function in Logincontroller

Route::post('/login/login',['uses'=>'Logincontroller@login'])->name('login');

Route::get('/home',['uses'=>'Logincontroller@enter'])->name('home');

This second route has to point to your home view that would look like this:

//Rota:
Route::get('/home',['uses'=>'LoginController@home'])->name('home'); //home nome da function no seu Controller

and add this function to the Controller

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;

class LoginController extends Controller
{
    public function entrar(Request $req)
    {
        $dados = $req->all();
        if (Auth::attempt(['email'=>$dados['email'],'password'=>$dados['password']])) {
            return redirect()->route('home');
        } else {
            return redirect()->route('login');
        }
    }
    public function home()
    {
        return view('home');
    }
    public function logout()
    {
        Auth::logout();
    }
}

Browser other questions tagged

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