0

I have Added the @csrf token and in form and in the meta tag in header but still getting the same erro. When I register it works and redirects me to the home page. That's fine but it doesn't redirect me to home page when I login although the credentials are okay and fine.

Login.blade.php

@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>

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

WEB.PHP

Auth::routes();

Route::get('/home', 'HomeController@index')->name('home');

Route::get('/admin', function(){
    echo "Hello Admin";
})->middleware('auth','admin');

Route::get('/agent', function(){
    echo "Hello Agent";
})->middleware('auth','agent');

Route::get('/customer', function(){
    echo "Hello Customer";
})->middleware('auth','customer');

LoginController.php

<?php

namespace App\Http\Controllers\Auth;

use App\Http\Controllers\Controller;
use App\Providers\RouteServiceProvider;
use Illuminate\Foundation\Auth\AuthenticatesUsers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Redirect;

class LoginController extends Controller
{


    use AuthenticatesUsers;

    protected $redirectTo = RouteServiceProvider::HOME;


    public function __construct()
    {
        $this->middleware('guest')->except('logout');
//        $this->middleware('auth');
//        $this->middleware('admin');
    }


    protected function redirectTo( ) {
        if (Auth::check() && Auth::user()->role == 'customer') {
            return redirect('/customer');
        }
        elseif (Auth::check() && Auth::user()->role == 'agent') {
            return redirect('/agent');
        }
        else {
            return redirect('/admin');
        }
    }
}
Isaac Bennetch
  • 11,830
  • 2
  • 32
  • 43
Salman
  • 1
  • 4
  • Check `.env` file and the drivers set in it. See - https://stackoverflow.com/questions/46141705/the-page-has-expired-due-to-inactivity-laravel-5-5/59513122 – Sougata Bose Feb 18 '20 at 10:55
  • This exception is caused by application security protection support built-in Laravel. Error 419 or Page Expired may occur when we do not include CSRF_TOKEN form input in the page HTML body. See here https://www.phpclasses.org/blog/post/896-5-Common-Laravel-Request-Errors-Which-Aunt-New-PHP-Developers.html –  Feb 18 '20 at 13:41

2 Answers2

0

Try clearing your cache with php artisan cache:clear, that normally does the trick for me.

geertjanknapen
  • 1,159
  • 8
  • 23
0
## LoginController.php ##

There was version difference the above code in the Question is for laravel framework 5.6 and i have updated laravel and the code for laravel framework 6.15.1 or 6 is below this is the only issue.

 protected function redirectTo( ) {

//        dd(Auth::user()->role);

        if (Auth::check() && Auth::user()->role == 'customer') {
            $this->redirectTo = '/customer';
            return $this->redirectTo;
        }
        elseif (Auth::check() && Auth::user()->role == 'agent') {
            $this->redirectTo = '/agent';
            return $this->redirectTo;
        }
        else {
            $this->redirectTo = '/admin';
            return $this->redirectTo;
        }

    }
Salman
  • 1
  • 4