Laravel 5.8 authentication

  authentication, laravel

Hi i’ve a problem with laravel authentication system (5.8) The problem is that I need to login two times to enter my site

My routes file is

Route::get('/', '[email protected]');
Route::get('/polizza', '[email protected]')->name('polizza');
Auth::routes();

--------------other routes------------------

RefundsController

public function __construct()
    {
        $this->middleware('auth');
    }
    
    public function index(){
        return view('auth.login');
    }

    public function indexRefunds(Request $request){
        DB::enableQueryLog(); 
        $grafici = 1;

        $getAverageLiquidati = DB::table('refunds')
            ->select(DB::raw("AVG(DATEDIFF(date_liq, date_ref)) AS avgliq"))
            ->where([
                ['disactive','=', 1],
                ['date_liq','<>','0000-00-00'],
                ['status_ref','>', 5]
                ])
            ->get();

        $getAverageRifiutati = DB::table('refunds')
        ->select(DB::raw("AVG(DATEDIFF(date_status, date_ref)) AS avgrif"))
        ->where(function($q) {
            $q->where('status_ref','=', 2)
              ->orWhere('status_ref','=', 3)
              ->orWhere('status_ref','=', 4);
        })
        ->where([
            ['disactive','=', 1],
            ['date_liq','<>','0000-00-00'],
            ])
        ->get();
        //dd(DB::getQueryLog());
        //dd($getAverageRifiutati);

        return view('pages.modify', compact('grafici','getAverageLiquidati','getAverageRifiutati'));
    }

login blade

@extends('layouts.app')

@section('content')
<div class="container">
    <div class="row justify-content-center">
        <div class="col-md-8">
            <div class="card">
                <div class="card-header">{{ __('Login') }}</div>

                <div class="card-body">
                    <form method="POST" action="{{ route('login') }}">
                        @csrf

                        <div class="form-group row">
                            <label for="email" class="col-md-4 col-form-label text-md-right">{{ __('E-Mail Address') }}</label>

                            <div class="col-md-6">
                                <input id="email" type="email" class="form-control @error('email') is-invalid @enderror" name="email" value="{{ old('email') }}" required autocomplete="email" autofocus>

                                @error('email')
                                    <span class="invalid-feedback" role="alert">
                                        <strong>{{ $message }}</strong>
                                    </span>
                                @enderror
                            </div>
                        </div>

                        <div class="form-group row">
                            <label for="password" class="col-md-4 col-form-label text-md-right">{{ __('Password') }}</label>

                            <div class="col-md-6">
                                <input id="password" type="password" class="form-control @error('password') is-invalid @enderror" name="password" required autocomplete="current-password">

                                @error('password')
                                    <span class="invalid-feedback" role="alert">
                                        <strong>{{ $message }}</strong>
                                    </span>
                                @enderror
                            </div>
                        </div>

                        <!--<div class="form-group row">
                            <div class="col-md-6 offset-md-4">
                                <div class="form-check">
                                    <input class="form-check-input" type="checkbox" name="remember" id="remember" {{-- old('remember') ? 'checked' : '' --}}>

                                    <label class="form-check-label" for="remember">
                                        {{-- __('Remember Me') --}}
                                    </label>
                                </div>
                            </div>
                        </div>-->

                        <div class="form-group row mb-0">
                            <div class="col-md-8 offset-md-4">
                                <button type="submit" class="btn btn-primary">
                                    {{ __('Login') }}
                                </button>

                                {{[email protected] (Route::has('password.request'))--}}
                                    <!--<a class="btn btn-link" href="{{-- route('password.request') --}}">
                                        {{-- __('Forgot Your Password?') --}}
                                    </a>-->
                                {{[email protected]}}
                            </div>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
</div>
@endsection

my LoginController

class LoginController extends Controller
{
    /*
    |--------------------------------------------------------------------------
    | Login Controller
    |--------------------------------------------------------------------------
    |
    | This controller handles authenticating users for the application and
    | redirecting them to your home screen. The controller uses a trait
    | to conveniently provide its functionality to your applications.
    |
    */

    use AuthenticatesUsers;

    /**
     * Where to redirect users after login.
     *
     * @var string
     */
    protected $redirectTo = '/polizza';

    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->middleware('guest')->except('logout');
    }
}

In my Middleware

class RedirectIfAuthenticated
{
    /**
     * Handle an incoming request.
     *
     * @param  IlluminateHttpRequest  $request
     * @param  Closure  $next
     * @param  string|null  $guard
     * @return mixed
     */
    public function handle($request, Closure $next, $guard = null)
    {
        if (Auth::guard($guard)->check()) {
            return redirect('/polizza');
        }

        return $next($request);
    }
}

When i connect to https://www.example.com/refunds/ because of definition in RefundsController it takes me to https://www.example.com/refunds/login but when i insert credentials it takes me to https://www.example.com/refunds/ with again login form then when i insert credentials it finally takes me to https://www.example.com/refunds/polizza

I dont understand why 🙁

Source: Laravel

Leave a Reply