Laravel Auth Middleware


#1

Hey guys,
I started implementing Sentry into my Laravel apps, and most of it worked fine - but I ran into a bug which forced me to remove it again.
The problem: If a user who is NOT logged in and calls a route which requires Auth he will not be redirected to /login (which is the expected behavior) but he will instead get a “Something went wrong” page with no Sentry interface.

Any way to fix this?


#2

Noone? Sentry is not working for Laravel 5?


#3

I did something like this to skip over Sentry completely for ones I don’t want to report. It then defaults to what Laravel would typically do.

/**
* Report or log an exception.
*
* This is a great spot to send exceptions to Sentry, Bugsnag, etc.
*
* @param \Exception $exception
* @return void
*/
public function report(Exception $exception)
{
if (app()->environment(‘production’) && $this->shouldReport($exception)) {
$this->sentryID = app(‘sentry’)->captureException($exception);
}

    parent::report($exception);
}

/**
 * Render an exception into an HTTP response.
 *
 * @param  \Illuminate\Http\Request  $request
 * @param  \Exception  $exception
 * @return \Illuminate\Http\Response
 */
public function render($request, Exception $exception)
{
    if(app()->environment('production') && $this->shouldReport($exception)) {
        return response()->view('errors.500', [
            'sentryID' => $this->sentryID,
        ], 500);
    }

    return parent::render($request, $exception);
}

#4

Hey @cat24max,

As @joecme stated it is best to add the shouldReport check since in newer Laravel versions the auth middleware causes a exception to be thrown which redirects the user to the login page, however looking at the examples in the repo we are not currently doing that assuming that is were you got the code for the render method.

private $sentryID;

/**
 * Render an exception into an HTTP response.
 *
 * @param  \Illuminate\Http\Request  $request
 * @param  \Exception  $exception
 * @return \Illuminate\Http\Response
 */
public function render($request, Exception $exception)
{
    if (app()->bound('sentry') && $this->shouldReport($exception)) {
        return response()->view('errors.500', [
            'sentryID' => $this->sentryID,
        ], 500);
    }

    return parent::render($request, $exception);
}

Or maybe even share the SentryID to the view and let the app decide what to render.

/**
 * Render an exception into an HTTP response.
 *
 * @param  \Illuminate\Http\Request  $request
 * @param  \Exception  $exception
 * @return \Illuminate\Http\Response
 */
public function render($request, Exception $exception)
{
    if(app()->bound('sentry') && $this->shouldReport($exception)) {
        \View::share('sentryID', $this->sentryID);
    }

    return parent::render($request, $exception);
}

This would ofcourse only work then the report function looks like this

private $sentryID;

/**
 * Report or log an exception.
 *
 * This is a great spot to send exceptions to Sentry, Bugsnag, etc.
 *
 * @param  \Exception  $e
 * @return void
 */
public function report(Exception $e)
{
    if (app()->bound('sentry') && $this->shouldReport($e)) {
        $this->sentryID = app('sentry')->captureException($e);
    }

    parent::report($e);
}

A last option would be to simple not change the render method at all and use the Sentry facade to get the last event id directly in the view like this:

<p>
    If you see people running around trying to find out what happend
    present them with this ID: {{ Sentry::getLastEventID() }}. 
    They will know what to do with it!
</p>

Let me know if this helps :slight_smile: If so I will try to make this more clear in the examples and readme of sentry-laravel.


#5

@joecme @stayallive

Thanks for your answers.
This was indeed the solution for this problem and Sentry is now up and running on 2 of my applications without failing to redirect Auth-routes.

You should probably include this within the tutorial, as it this Auth-redirect is one of the most important things in frontend applications built with Laravel.


#6

Yes, Laravel user here too, the examples are great for exampeling but not for production usage, this will be fixed soon!