Skip to content

Instantly share code, notes, and snippets.

@xaoc-303
Created July 19, 2015 15:30
Show Gist options
  • Select an option

  • Save xaoc-303/2f02a99be456b12c89c3 to your computer and use it in GitHub Desktop.

Select an option

Save xaoc-303/2f02a99be456b12c89c3 to your computer and use it in GitHub Desktop.
API JSON CSRF
<?php
Route::filter('csrf', function() {
$token = Request::ajax() ? Request::header('X-CSRF-Token') : Input::get('_token');
if (Session::token() != $token)
throw new Illuminate\Session\TokenMismatchException;
});
<!DOCTYPE html>
<html>
<head>
<meta name="_token" content="{{ csrf_token() }}" />
</head>
<body>
<script>
$(function() {
$.ajaxSetup({
headers: {
'X-CSRF-Token': $('meta[name="_token"]').attr('content')
}
});
});
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment