3

I have implemented Multi-Auth in my laravel 5.4 project, but whenever I try to login from another device I get this error.

ErrorException in Response.php line 386:Header may not contain more than a single header, new line detected

Now I have tried looking into other similiar questions in this website but none of them match what I'm doing in the login controller.

Here is my login controller :

   class LoginController extends Controller
{


    use AuthenticatesUsers;   

    public function __construct()
    {
        $this->middleware('guest', ['except' => 'logout']);
    }

    public function username()
    {
        return 'mobile_no';
    }

    protected function redirectTo( )
    {
        $notification = array(
            'message' => 'Welcome Admin!', 
            'alert_type' => 'info',
            'title' => Auth::user()->name
        );
        return redirect('/home')->with('notification', $notification);
    }
}

Whats wrong with my redirecTo() function?

1 Answer 1

4

This question is already answered here . Basically you method should return a String and not a Redirect responce. Here is an example:

namespace App\Http\Controllers\Auth;

use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\AuthenticatesUsers;
use Illuminate\Support\Facades\Auth;

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 = '/';

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

     public function redirectTo(){
          return '/admin';
     }


}

but because you actually need to redirect to a view AND include some data that wont work for you. What you need is to override the redirect functionality all together and just create your own . Here is what you need to do. You can just copy the whole class from here and it should work out of the box :) . Here is the code. Cheers.

namespace App\Http\Controllers\Auth;

use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\AuthenticatesUsers;
use Illuminate\Support\Facades\Auth;

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 = '/';

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

     //public function redirectTo(){
     //     return '/admin';
     //}
     protected function authenticated()
 {
      $notification = array(
          'message' => 'Welcome Admin!',
          'alert_type' => 'info',
          'title' => Auth::user()->name
      );
      return redirect('/home')->with('notification', $notification);
 }

}
Sign up to request clarification or add additional context in comments.

2 Comments

Can you please clarify why should I disable $this->middleware('guest')->except('logout');? Without It the logged user is able to visit login page which shouldn't be the desired behavior right?
Try enabling it. Its not a MUST. it was supposed to be disabled because that code also redirects and you want to override that behavior. You can always just edit that middle ware as well just to make sure that it redirects correctly based on condition and not just to ' /home '

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.