php - Unable to decrypt CSRF/XSRF token in Laravel sent through Angular -
i using angularavel setup app. on local setup not need explicitly send xsrf-token
angular http request. , works fine laravel. uploaded same setup on server , tried login using form , laravel throws token mismatch
error. hence checked request payload in dev tools , found there no xsrf-token header in request,like see on local. believe angular generates 1 default , send every http request,but not sure why failing here.
i added csrf token in angular based on tutorial here using meta tag method. meta <meta name="csrf-token" content='<?php echo json_encode(csrf_token()); ?>'> // tried without encoding
in .config have
$httpprovider.defaults.headers.common['x-xsrf-token'] = $('meta[name=csrf-token]').attr('content');
if use x-xsrf-token
decryptexception in encrypter.php line 142:invalid data. if use x-csrf-token
token mismatch error.
request shows token in header(added below).
accept:application/json, text/plain, */* accept-encoding:gzip, deflate accept-language:en-us,en;q=0.8 connection:keep-alive content-length:68 content-type:application/json;charset=utf-8 cookie:phpsessid=yssuvcyycb-9ifzz29ksf1 host:demo.abc.com origin:http://demo.abc.com referer:http://demo.abc.com/hexa/public/ user-agent:mozilla/5.0 (windows nt 6.1; wow64) applewebkit/537.36 (khtml, gecko) chrome/42.0.2311.90 safari/537.36 firephp/4chrome x-firephp-version:0.0.6 x-wf-max-combined-size:261120 x-xsrf-token:inhrwmvjcuxznwvmrwlrwmfxr09vbgdzt1m2z284c0nqv2x2rwxan0mi -->token
where commiting mistake? defult token angular seems integrated , vry large string.whereas 1 genrated function small string.
this worked me, i'll show relevant snippets.
in template have following:
<script type="text/javascript">myapp.constant("csrf_token", '<?= csrf_token() ?>');</script>
and have factory such as:
.factory('product', function($http, csrf_token) { return { // store product save : function(productdata) { return $http({ method: 'post', url: 'http://localhost/angularjs/public/api/products', headers: {'x-csrf-token': csrf_token}, data: productdata }); }, } });
this needed stop receiving csrf token related errors.
Comments
Post a Comment