Problem when going back when logging in (Laravel 5.2 php)

1

I have created a login in Laravel 5.2 but it has a problem when logging in. It redirects me to the admin panel (until then it is ok) but when you press the back arrow of the browser I get the full email field (eg: henry @ gmail.com and the empty password field) and then I press the browser's next button and redirect me to the admin panel. I would like that does not happen .. but at the moment of going back to the login this empty or redirect me to the admin panel without first login. I'm using the Session class for the login.

routes.php

Route::get('/', 'AdminController@index');
Route::post('login','AdminController@ingresarSession');
Route::get('logout','AdminController@cerrarSession');

AdminController.php

namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use DB;
use Session;
use Illuminate\Support\Facades\Redirect;
use App\Http\Requests\UserFormRequest;

class AdminController extends Controller
{

    public function __construct()
    {

    }

    public function index()
    {
        return view('login.login');
    }

    public function ingresarSession(Request $request)
    {
        $email=$request->input('email');
        $password=$request->input('contrasena');

        $user=DB::table('users')->where("email","=",$email)->where("contrasena","=",$password)->get();

        if($user>0)
        {
            Session::put('email',$email);

            return Redirect::to("ventas/venta");
        }else{
            return Redirect::to("/");
        }
    }

    public function cerrarSession()
    {
        Session::forget('email');

        return Redirect::to("/");

    }
}

login.blade.php

<head>
    <title>Ventasoft</title>

    <link rel="stylesheet" href="{{ asset('css/bootstrap.min.css') }}">
    <link rel="shortcut icon" href="{{ asset('img/favicon.ico') }}">
</head>

<body background="{{ asset('imagenes/login/fondo.jpg') }}">

@if(count($errors)>0)
    <div class="alert alert-danger">
        <ul>
            @foreach($errors->all() as $error)
                <li>{{$error}}</li>
            @endforeach
        </ul>
    </div>
@endif

<div class="container" >
    <div class="row">
        <div class="col-sm-6 col-md-4 col-md-offset-4 col-xs-12" style="background: #fff;margin-top: 50px;">
            <h1 class="text-center">Acceso al Sistema</h1>
                <img class="center-block" src="{{ asset('imagenes/login/usuario.png') }}" width="180px" height="180px">
                <br/>
                {!! Form::open(array('method'=>'POST','action'=>'AdminController@ingresarSession')) !!}

                {{ Session::forget('email') }}
                <input type="text" name="email" class="form-control" placeholder="Email" required>
                <br/>
                <input type="password" name="contrasena" class="form-control" placeholder="Password" required>
                <br/>

                <button class="btn btn-lg btn-primary btn-block" type="submit">Ingresar</button>
                {!! Form::close() !!}
        </div>
    </div>
</div>
</body>

the Middleware Admin.php

namespace App\Http\Middleware;

use Closure;
use Session;
use Illuminate\Support\Facades\Redirect;
use Illuminate\Contracts\Auth\Guard;
class Admin
{
    protected $auth;
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */

    public function __construct(Guard $auth)
    {
        $this->auth=$auth;
    }

    public function handle($request, Closure $next)
    {
        if($request->session()->has('email'))
        {
            return $next($request);
        }else{
            return redirect()->to('/');
        }        
    }
}
    
asked by Alejandro 25.04.2018 в 04:10
source

1 answer

0

You can create a group of routes that have your Admin middleware as an attribute:

Route::middleware(['admin'])->group(function () {
    Route::get('/', 'AdminController@index');
});

Or just assign the middleware to a specific route:

Route::get('/', 'AdminController@index')->middleware('admin');

But remember that you must first register the middleware in the app's kernel, in the file app\Http\Kernel.php and in the property $routeMiddleware you add it, example:

'admin' => \App\Http\Middleware\Admin::class,

Also note that you say that by pressing the back arrow of the browser you go back to the login, and then you press the browser's next button and it redirects you to the admin panel, by doing so in no time you are closing the session of the user therefore this one continues logged.

    
answered by 25.04.2018 в 17:50