0

(laravel 5.4.36)

When I add 'middleware' => 'auth' to any of my routes, I get the three errors below. This only happens on my Production server. On my local it redirects to my login page as it should.

Example route:

Route::get('/credits', [
    'middleware' => 'auth',
    'as' => 'pages.credits',
    'uses' => 'PagesController@credits'
]);

Symfony\Component\Debug\Exception\FatalThrowableError Call to a member function setCookie() on null


Symfony\Component\Debug\Exception\FatalThrowableError Type error: Argument 1 passed to Illuminate\Session\Middleware\StartSession::addCookieToResponse() must be an instance of Symfony\Component\HttpFoundation\Response, instance of Illuminate\View\View given, called in ../vendor/laravel/framework/src/Illuminate/Session/Middleware/StartSession.php on line 72


Symfony\Component\Debug\Exception\FatalThrowableError Type error: Argument 1 passed to Illuminate\Cookie\Middleware\EncryptCookies::encrypt() must be an instance of Symfony\Component\HttpFoundation\Response, instance of Illuminate\View\View given, called in ../vendor/laravel/framework/src/Illuminate/Cookie/Middleware/EncryptCookies.php on line 59

/vendor/laravel/framework/illuminate/Auth/Middleware/Authenticate.php:

<?php

namespace Illuminate\Auth\Middleware;

use Closure;
use Illuminate\Auth\AuthenticationException;
use Illuminate\Contracts\Auth\Factory as Auth;

class Authenticate
{
    /**
     * The authentication factory instance.
     *
     * @var \Illuminate\Contracts\Auth\Factory
     */
    protected $auth;

    /**
     * Create a new middleware instance.
     *
     * @param  \Illuminate\Contracts\Auth\Factory  $auth
     * @return void
     */
    public function __construct(Auth $auth)
    {
        $this->auth = $auth;
    }

    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @param  string[]  ...$guards
     * @return mixed
     *
     * @throws \Illuminate\Auth\AuthenticationException
     */
    public function handle($request, Closure $next, ...$guards)
    {
        $this->authenticate($guards);

        return $next($request);
    }

    /**
     * Determine if the user is logged in to any of the given guards.
     *
     * @param  array  $guards
     * @return void
     *
     * @throws \Illuminate\Auth\AuthenticationException
     */
    protected function authenticate(array $guards)
    {
        if (empty($guards)) {
            return $this->auth->authenticate();
        }

        foreach ($guards as $guard) {
            if ($this->auth->guard($guard)->check()) {
                return $this->auth->shouldUse($guard);
            }
        }

        throw new AuthenticationException('Unauthenticated.', $guards);
    }
}
loquela
  • 37
  • 7
  • Can you post your code in `Middleware/Authenticate.php`? – Nathan Apr 19 '20 at 06:42
  • Did you modify the middleware? https://stackoverflow.com/questions/48970172/call-to-a-member-function-setcookie-on-null might help – Edwin Krause Apr 19 '20 at 07:19
  • Thanks @EdwinKrause, no. I've not modified the original Auth Middleware. Also, the thing that's really foxing me is that it works on my local install with now errors. – loquela Apr 20 '20 at 07:14
  • Did you check folder and file permissions? – Edwin Krause Apr 20 '20 at 08:39
  • Hi @EdwinKrause, this was what I was thinking but storage/* seem fine and cookies and sessions in all other contexts work ok too. Where else should I check? – loquela Apr 20 '20 at 21:45
  • If I change my .env APP_ENV=production to APP_ENV=local on my remote production server. The issue is resolved! Any ideas why this is and how I might get it to work with APP_ENV=production? – loquela May 02 '20 at 02:36
  • have you found any solution for this problem ? – Farshad Sep 03 '22 at 20:40

0 Answers0