Laravel login redirects to blank page

Published 1 year ago by afonte15

Hello Everyone,

I have been stuck with Laravel redirecting after login. The connection works and after I login it redirects to a blank page but if I change the url path I can access the different web pages. Any assistance would be highly appreciated! I am using LDAP to connect and it is working. On my AuthController I have the protected $redirectTo paths set. See picture below. enter image description here

Please let me know if there is any other code I should provide.

Thank you!!!

Routes - Below


Route::group(['middleware' => ['web']], function () {

Route::auth();
Route::get('login', '[email protected]');
Route::post('login', '[email protected]_password');
Route::patch('computers/{inventories}', '[email protected]');
Route::get('computers/search', '[email protected]');
Route::resource('computers', 'InventoriesController');
}); 

Route::get('/home', '[email protected]');

ReidrectIfAuthenticted.php- Below


<?php

namespace App\Http\Middleware;

use Closure; use Illuminate\Support\Facades\Auth;

class RedirectIfAuthenticated {

protected $auth; /**

Handle an incoming request.
@param \Illuminate\Http\Request $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('/computers/create'); 
    }

    return $next($request); 
    }

} 

loginController.php - Below


<?php namespace App\Http\Controllers;

/**
 * @class Login
 */
use App\User;
use Illuminate\Http\Request;
class Login extends Controller
{
    /**
     * Show the application dashboard to the user.
     *
     * @return Response
     */
    public function index()
    {
        return view('auth.login');
    }
    public function check_password(Request $req)

    {
        //die('has to stop here');
        $user = User::check_password($req);
//var_dump($user); die;
        if ($user)
        {

            return redirect('/computers/create');
        }
        else
    {

            return redirect('login')->with('message', 'Login Failed');
        }
    }
}

AuthController.php - Below


<?php

namespace App\Http\Controllers\Auth;


use App\User;
use Validator;
use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\ThrottlesLogins;
use Illuminate\Foundation\Auth\AuthenticatesAndRegistersUsers;
class AuthController extends Controller
{
    /*
    |--------------------------------------------------------------------------
    | Registration & Login Controller
    |--------------------------------------------------------------------------
    |
    | This controller handles the registration of new users, as well as the
    | authentication of existing users. By default, this controller uses
    | a simple trait to add these behaviors. Why don't you explore it?
    |
    */
    use AuthenticatesAndRegistersUsers, ThrottlesLogins;
    /**
     * Where to redirect users after login / registration.
     *
     * @var string
     */

    protected $redirectTo = '/computers/create';
    protected $redirectAfterLogout = '/login';
    /**
     * Create a new authentication controller instance.
     *
     * @return void
     */


    public function __construct()
    {
        //$this->auth = $auth;
        //$this->registrar = $registrar;

        $this->middleware($this->guestMiddleware(), ['except' => 'logout']);
    }
   
 /**
     * Get a validator for an incoming registration request.
     *
     * @param  array  $data
     * @return \Illuminate\Contracts\Validation\Validator
     */
    //Code commented out because login is Using Ldap credentials
//    protected function validator(array $data)
//    {
//        return Validator::make($data, [
//            'name' => 'required|max:255',
//            'email' => 'required|email|max:255|unique:users',
//            'password' => 'required|min:6|confirmed',
//        ]);
    //}
    /**
     * Create a new user instance after a valid registration.
     *
     * @param  array  $data
     * @return User
     */
    //Removed b/c LDAP is being usedcd
//    protected function create(array $data)
//    {
//        return User::create([
//            'name' => $data['name'],
//            'email' => $data['email'],
//            'password' => bcrypt($data['password']),
//        ]);
//    }
}

InventoriesController.php - Below


 <?php

namespace  App\Http\Controllers;

use Illuminate\Http\Request;
use DB;
use App\Inventory;
use App\Http\Requests\InventoryRequest;
class InventoriesController extends Controller
{
    public function __construct()
    {
        //$this->middleware('auth');  //does not allow users to login, redirects back to login when using LDAP credentials

    }
    public function index(Request $request)
    {
        $location = $request->input("building");
        if ($location != null) {
            $inventories = Inventory::where('building', $location)->get();
        }  else {
            $inventories = Inventory::all();
        }
        return view('computers.index', compact('inventories'));
    }
    public function show($inventories)
    {
        $inventories = Inventory::findOrFail($inventories);
        return view::make('computers.show')
            ->with('inventory', $inventories);
    }
    public function create(){
        //flash('Hello World', 'This is the message');
        return view('computers.create');
    }
    /**
     * Store a newly created resource in storage.
     *
     * @param  inventory  $request
     * @return Response
     *
     */
    public function store(InventoryRequest $request)
    {
           Inventory::create($request->all());
        flash('Success!', 'Inventory Successfully Updated!');
//s
//        return redirect()->back();  //temporary
        return back();
    }
    public function edit($inventories)
    {
        $inventories = Inventory::findOrFail($inventories);
        return view('computers.edit', compact('inventories'));
    }
    public function update(InventoryRequest $request, Inventory $inventories){
        $inventories->update($request->all());
        flash('Success!', 'Inventory Successfully Updated!');
        return back();
    }
    public function search()
        {
            $search = \Request::get('q'); //<-- we use global request to get the param of URI
//            $search = Input::get('search');
            $inventories = Inventory::where('lastName','LIKE','%'.$search.'%')
               -> orwhere('firstName', 'LIKE','%'.$search.'%' )
                -> orwhere('department', 'LIKE','%'.$search.'%' )
                -> orwhere('building', 'LIKE','%'.$search.'%' )
                -> orwhere('room', 'LIKE','%'.$search.'%' )
                -> orwhere('manufacturer', 'LIKE','%'.$search.'%' )
                -> orwhere('device', 'LIKE','%'.$search.'%' )
                -> orwhere('model', 'LIKE','%'.$search.'%' )
                -> orwhere('tag', 'LIKE','%'.$search.'%' )
                -> orwhere('macAddress', 'LIKE','%'.$search.'%' )
                -> orwhere('status', 'LIKE','%'.$search.'%' )
                -> orwhere('comments', 'LIKE','%'.$search.'%' )
                ->get();
            return view('computers.search',compact('inventories'));
        }
}

login.blade.php - Below


@extends('layouts.app')
    {{--@include('partials.header')--}}
@section('content')
<div class="container">
    <div class="row">
        <div class="col-md-8 col-md-offset-2">
            <div class="panel panel-default">
                <div class="panel-heading">Login</div>
                <div class="panel-body">
                    <form class="form-horizontal" role="form" method="POST" action="{{ url('/login') }}">
                        {{ csrf_field() }}

                        <div class="form-group{{ $errors->has('email') ? ' has-error' : '' }}">
                            <label for="email" class="col-md-4 control-label">Ad username</label>

                            <div class="col-md-6">
                                <input id="userName" type="text" class="form-control" name="userName" value="{{ old('email') }}">

                                @if ($errors->has('email'))
                                    <span class="help-block">
                                        <strong>{{ $errors->first('email') }}</strong>
                                    </span>
                                @endif
                            </div>
                        </div>

                        <div class="form-group{{ $errors->has('password') ? ' has-error' : '' }}">
                            <label for="password" class="col-md-4 control-label">Password</label>

                            <div class="col-md-6">
                                <input id="password" type="password" class="form-control" name="password">

                                @if ($errors->has('password'))
                                    <span class="help-block">
                                        <strong>{{ $errors->first('password') }}</strong>
                                    </span>
                                @endif
                            </div>
                        </div>

                        <div class="form-group">
                            <div class="col-md-6 col-md-offset-4">
                                <div class="checkbox">
                                    <label>
                                        <input type="checkbox" name="remember"> Remember Me
                                    </label>
                                </div>
                            </div>
                        </div>

                        <div class="form-group">
                            <div class="col-md-6 col-md-offset-4">
                                <button type="submit" class="btn btn-primary">
                                    <i class="fa fa-btn fa-sign-in"></i> Login
                                </button>

                                <a class="btn btn-link" href="{{ url('/password/reset') }}">Forgot Your Password?</a>
                            </div>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
</div>
@endsection


@extends('layout')

@include('partials.mainheader')

@section('content')

    <div class="row">
        <div class="container">

            <h1>Update Inventory</h1>

    </div>
    </div>
  <hr>

<div class="row">
    {{--<form method="POST" action="/computers" enctype="multipart/form-data">--}}
  {!! Form::open(['url' =>'computers']) !!}
    @include('computers.form', ['submitButtonText' => 'Submit Inventory'])

  {!! Form::close()!!}
</div>
    @include('errors.list')

@stop 

Snapey
Snapey
1 year ago (959,275 XP)

web middleware has not been needed for about 18 months or more so as a first step, remove that route middleware group

if that does not sort it, when you get redirected to 'a blank page' what is the url?

beetuco
beetuco
1 year ago (32,710 XP)

Throw this in your app and trace whats happening.

https://github.com/barryvdh/laravel-debugbar

beetuco
beetuco
1 year ago (32,710 XP)

Also what ldap package are you using?

afonte15

I am using laravel 5.2.

I have removed the web middleware from my routes.php file and the same thing still happens. I get redirected to a blank page and the url is my login page. But I can still access my other pages when I change the url after the login redirect to the blank page.

My logout redirect works and it redirects me to the login page.

In my InventoriesController.php if I uncomment "this->middleware('auth'); then I cannot get passed the login page. After I try logging in it redirects back to the login page and actually displays the page. But I cannot access any other pages even when changing the urls manually.


<?php

namespace  App\Http\Controllers;

use Illuminate\Http\Request;
use DB;
use App\Inventory;
use App\Http\Requests\InventoryRequest;
class InventoriesController extends Controller
{
   public function __construct()
    {
        //$this->middleware('auth');  //does not allow users to login, redirects back to login when using LDAP credentials

    }

I am not sure if this makes a difference but when I list my routes in the terminal under name nothing is listed. the URI is listed as computers/create but for under name it is blank.

afonte15

I am not using an LDAP package.

Snapey
Snapey
1 year ago (959,275 XP)

open network tools in your browser and submit the login form, see if you get a 302 response?

afonte15

After I login I am getting a 500 Internal Server Error

Snapey
Snapey
1 year ago (959,275 XP)

so, if not on the screen, the laravel logs should show it?

afonte15

laravel.log file located in app/storage/app/logs

[2017-04-03 13:10:38] local.ERROR: ErrorException: file_put_contents(/home/vagrant/sites/inventory/storage/framework/sessions/8f709f9d4f445d2772ed7f48bbb9c4bff8a63819): failed to open stream: Permission denied in /home/vagrant/sites/inventory/vendor/laravel/framework/src/Illuminate/Filesystem/Filesystem.php:111
Stack trace:
#0 [internal function]: Illuminate\Foundation\Bootstrap\HandleExceptions->handleError(2, 'file_put_conten...', '/home/vagrant/s...', 111, Array)
#1 /home/vagrant/sites/inventory/vendor/laravel/framework/src/Illuminate/Filesystem/Filesystem.php(111): file_put_contents('/home/vagrant/s...', 'a:4:{s:6:"_toke...', 2)
#2 /home/vagrant/sites/inventory/vendor/laravel/framework/src/Illuminate/Session/FileSessionHandler.php(83): Illuminate\Filesystem\Filesystem->put('/home/vagrant/s...', 'a:4:{s:6:"_toke...', true)
#3 /home/vagrant/sites/inventory/vendor/laravel/framework/src/Illuminate/Session/Store.php(262): Illuminate\Session\FileSessionHandler->write('8f709f9d4f445d2...', 'a:4:{s:6:"_toke...')
#4 /home/vagrant/sites/inventory/vendor/laravel/framework/src/Illuminate/Session/Middleware/StartSession.php(88): Illuminate\Session\Store->save()
#5 /home/vagrant/sites/inventory/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php(155): Illuminate\Session\Middleware\StartSession->terminate(Object(Illuminate\Http\Request), Object(Illuminate\Http\Response))
#6 /home/vagrant/sites/inventory/public/index.php(48): Illuminate\Foundation\Http\Kernel->terminate(Object(Illuminate\Http\Request), Object(Illuminate\Http\Response))
#7 {main}  
beetuco
beetuco
1 year ago (32,710 XP)

Try clearing cache (php artisan cache:clear) and check that your storage folder has appropriate write access for the system user that is running your web process (apache or nginx).

Are you using Homestead?

afonte15

Yes, I am using Homestead.

beetuco
beetuco
1 year ago (32,710 XP)

Yeh from that log it looks like the session is failing to write to file. (file_put_contents)

see this post: https://laracasts.com/discuss/channels/servers/homesteadvagrant-cant-change-storage-file-permissions-issue

Not sure why permissions would be askew in Homestead. Usually really rock solid unless played with (I have been there.. so annoying).

You could always use a database for sessions to get away from the filesystem for sessions.

in .env

SESSION_DRIVER=database

then create the table and run the migration. Then clear for good measure

php artisan session:table && php artisan migrate
php artisan cache:clear
composer dump-autoload 
afonte15

I will give this a try. Yea, Homestead usually never gives me trouble. I hadn't coded for awhile and getting back into things but I already thought I adjusted my folder permissions because before this issue I was getting blank page even at start up. I will take a look at the link and most likely try your suggestion.

afonte15

Ok so, I thought Chmod -R 777 permission is never good to do.

I ran sudo chmod -R o+w storage/

and now after I hit login I am getting this error

FatalThrowableError in LoginController.php line 25: Class 'App\Http\Controllers\User' not found

This is progress, right? Now I am seeing an error and it seems to be an issue with my LoginController.php file.

afonte15

I added use App\User; and that cleared up the error with the LoginController but now after I login it just redirects back to the login page.

When I check the network tab in my browser I get a

302 Found message on Post

And on Get I get a

200 OK

any thoughts. Possibly something with my middleware now?

Please sign in or create an account to participate in this conversation.