Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save vzool/def09c5976e48bbe9926c40a8b7f91c6 to your computer and use it in GitHub Desktop.
Save vzool/def09c5976e48bbe9926c40a8b7f91c6 to your computer and use it in GitHub Desktop.

Revisions

  1. @jrmadsen67 jrmadsen67 revised this gist Oct 13, 2015. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -4,7 +4,7 @@ expire & throw a strange error.

    Handling it is simple, and is a good lesson for dealing with other types of errors in a custom manner.

    In Middleware you will see a file VerifyCsrfToken.php and be tempted to handle thing there. DON'T!
    In Middleware you will see a file VerifyCsrfToken.php and be tempted to handle things there. DON'T!
    Instead, look at your app/Exceptions/Handler.php, at the render($request, Exception $e) function.

    All of your exceptions go through here, unless you have excluded them in the $dontReport array at the
    @@ -32,7 +32,7 @@ So back in the Handler class, let's specifically handle that type of exception:
    return parent::render($request, $e);
    }

    The code is simple - if the exception is a `TokenMismatchException` we will handling it just like
    The code is simple - if the exception is a `TokenMismatchException` we will handle it just like
    a validation error in a controller. In our forms(s), we need to be sure to use the
    $request->old('field_name') (or the old('field_name') helper function) to repopulate. Simply going
    "back" will refresh the form with a new token so they can re-submit.
  2. @jrmadsen67 jrmadsen67 revised this gist Oct 13, 2015. 1 changed file with 4 additions and 4 deletions.
    8 changes: 4 additions & 4 deletions gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -1,11 +1,11 @@
    Quick tip for handling CSRF Token Expiration - common issue is when you use csrf protection is that if
    a form sits there for a while (like a login form, but any the same) the csrf token in the form will expire
    & throw a strange error.
    a form sits there for a while (like a login form, but any the same) the csrf token in the form will
    expire & throw a strange error.

    Handling it is simple, and is a good lesson for dealing with other types of errors in a custom manner.

    In Middleware you will see a file VerifyCsrfToken.php and be tempted to handle thing there. DON'T! Instead,
    look at your app/Exceptions/Handler.php, at the render($request, Exception $e) function.
    In Middleware you will see a file VerifyCsrfToken.php and be tempted to handle thing there. DON'T!
    Instead, look at your app/Exceptions/Handler.php, at the render($request, Exception $e) function.

    All of your exceptions go through here, unless you have excluded them in the $dontReport array at the
    top of this file. You can see we have the $request and also the Exception that was thrown.
  3. @jrmadsen67 jrmadsen67 revised this gist Oct 13, 2015. 1 changed file with 16 additions and 13 deletions.
    29 changes: 16 additions & 13 deletions gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -1,17 +1,18 @@
    Quick tip for handling CSRF Token Expiration - common issue is when you use csrf protection is that if a form sits there
    for a while (like a login form, but any the same) the csrf token in the form will expire & throw a strange error.
    Quick tip for handling CSRF Token Expiration - common issue is when you use csrf protection is that if
    a form sits there for a while (like a login form, but any the same) the csrf token in the form will expire
    & throw a strange error.

    Handling it is simple, and is a good lesson for dealing with other types of errors in a custom manner.

    In Middleware you will see a file VerifyCsrfToken.php and be tempted to handle thing there. DON'T! Instead,
    look at your app/Exceptions/Handler.php, at the render($request, Exception $e) function.

    All of your exceptions go through here, unless you have excluded them in the $dontReport array at the top of this file.
    You can see we have the $request and also the Exception that was thrown.
    All of your exceptions go through here, unless you have excluded them in the $dontReport array at the
    top of this file. You can see we have the $request and also the Exception that was thrown.

    Take a quick look at the parent of VerifyCsrfToken - Illuminate\Foundation\Http\Middleware\VerifyCsrfToken.
    You can see from VerifyCsrfToken.php that handle() is the function called to do the token check. In the parent class,
    if the token fails, a `TokenMismatchException` is thrown.
    You can see from VerifyCsrfToken.php that handle() is the function called to do the token check. In the
    parent class, if the token fails, a `TokenMismatchException` is thrown.

    So back in the Handler class, let's specifically handle that type of exception:

    @@ -31,11 +32,13 @@ So back in the Handler class, let's specifically handle that type of exception:
    return parent::render($request, $e);
    }

    The code is simple - if the exception is a `TokenMismatchException` we will handling it just like a validation error
    in a controller. In our forms(s), we need to be sure to use the $request->old('field_name') (or the old('field_name')
    helper function) to repopulate. Simply going "back" will refresh the form with a new token so they can re-submit.

    CAREFUL! - I found that using the http://laravelcollective.com/ Form::open() tag seemed to be incompatible with the token -
    redirect()->back() was not refresh the token for me. This may just be something in my code, but when I used a regular
    html tag it was fine. If this is happening to you, try that.
    The code is simple - if the exception is a `TokenMismatchException` we will handling it just like
    a validation error in a controller. In our forms(s), we need to be sure to use the
    $request->old('field_name') (or the old('field_name') helper function) to repopulate. Simply going
    "back" will refresh the form with a new token so they can re-submit.

    CAREFUL! - I found that using the http://laravelcollective.com/ Form::open() tag seemed to be
    incompatible with the token - redirect()->back() was not refresh the token for me. This may just be
    something in my code, but when I used a regular html tag it was fine. If this is happening to you,
    try that.

  4. @jrmadsen67 jrmadsen67 revised this gist Oct 13, 2015. 1 changed file with 34 additions and 5 deletions.
    39 changes: 34 additions & 5 deletions gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -1,12 +1,41 @@
    Quick tip for handling CSRF Token Expiration - common issue is when you use csrf protection is that if a form sits there for a while (like a login form, but any the same) the csrf token in the form will expire & throw a strange error.
    Quick tip for handling CSRF Token Expiration - common issue is when you use csrf protection is that if a form sits there
    for a while (like a login form, but any the same) the csrf token in the form will expire & throw a strange error.

    Handling it is simple, and is a good lesson for dealing with other types of errors in a custom manner.

    In Middleware you will see a file VerifyCsrfToken.php and be tempted to handle thing there. DON'T! Instead, look at your app/Exceptions/Handler.php, at the render($request, Exception $e) function.
    In Middleware you will see a file VerifyCsrfToken.php and be tempted to handle thing there. DON'T! Instead,
    look at your app/Exceptions/Handler.php, at the render($request, Exception $e) function.

    All of your exceptions go through here, unless you have excluded them in the $dontReport array at the top of this file. You can see we have the $request and also the Exception that was thrown.
    All of your exceptions go through here, unless you have excluded them in the $dontReport array at the top of this file.
    You can see we have the $request and also the Exception that was thrown.

    Take a quick look at the parent of VerifyCsrfToken - Illuminate\Foundation\Http\Middleware\VerifyCsrfToken. You can see from VerifyCsrfToken.php that handle() is the function called to do the token check. In the parent class, if the token fails, a `TokenMismatchException` is thrown.
    Take a quick look at the parent of VerifyCsrfToken - Illuminate\Foundation\Http\Middleware\VerifyCsrfToken.
    You can see from VerifyCsrfToken.php that handle() is the function called to do the token check. In the parent class,
    if the token fails, a `TokenMismatchException` is thrown.

    So back in
    So back in the Handler class, let's specifically handle that type of exception:

    public function render($request, Exception $e)
    {

    if ($e instanceof \Illuminate\Session\TokenMismatchException)
    {
    return redirect()
    ->back()
    ->withInput($request->except('password'))
    ->with([
    'message' => 'Validation Token was expired. Please try again',
    'message-type' => 'danger']);
    }

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

    The code is simple - if the exception is a `TokenMismatchException` we will handling it just like a validation error
    in a controller. In our forms(s), we need to be sure to use the $request->old('field_name') (or the old('field_name')
    helper function) to repopulate. Simply going "back" will refresh the form with a new token so they can re-submit.

    CAREFUL! - I found that using the http://laravelcollective.com/ Form::open() tag seemed to be incompatible with the token -
    redirect()->back() was not refresh the token for me. This may just be something in my code, but when I used a regular
    html tag it was fine. If this is happening to you, try that.

  5. @jrmadsen67 jrmadsen67 created this gist Oct 13, 2015.
    12 changes: 12 additions & 0 deletions gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,12 @@
    Quick tip for handling CSRF Token Expiration - common issue is when you use csrf protection is that if a form sits there for a while (like a login form, but any the same) the csrf token in the form will expire & throw a strange error.

    Handling it is simple, and is a good lesson for dealing with other types of errors in a custom manner.

    In Middleware you will see a file VerifyCsrfToken.php and be tempted to handle thing there. DON'T! Instead, look at your app/Exceptions/Handler.php, at the render($request, Exception $e) function.

    All of your exceptions go through here, unless you have excluded them in the $dontReport array at the top of this file. You can see we have the $request and also the Exception that was thrown.

    Take a quick look at the parent of VerifyCsrfToken - Illuminate\Foundation\Http\Middleware\VerifyCsrfToken. You can see from VerifyCsrfToken.php that handle() is the function called to do the token check. In the parent class, if the token fails, a `TokenMismatchException` is thrown.

    So back in