syntax error, unexpected '}' in Anchor Laravel - laravel-5

My Anchor Tag is like below
Login
I tried to change it to Laravel like below
{{ HTML::link("{{ route('login') }}", 'log in')}}
It says syntax error, unexpected '}'
Am I missing something?

You can use the result of the function route directly as a parameter of link:
{{ HTML::link(route('login'), 'log in') }}

Related

CSRF token mismatch when making a POST request with Axios

I have a route in api.php which I need to use 'web' middleware on to check for csrf verification because I don't want that route to be accessible from outside.
I have read the following solutions:
Laravel “CSRF token mismatch” for POST with laravel-cors and axios
CSRF Token mismatch, laravel and axios
Laravel + Vue.js (axios) - CSRF token mismatch
https://laracasts.com/discuss/channels/laravel/csrf-token-mismatch-6
All of the say the same - have <meta name="csrf-token" content="{{ csrf_token() }}"> in the document header and be sure to add this token to axios header in bootstrap.js after including axios. Here's my bootstrap.js fragment
window.axios = require('axios');
window.axios.defaults.headers.common = {
'X-CSRF-TOKEN': document.querySelector('meta[name="csrf-token"]').getAttribute('content'),
'X-Requested-With': 'XMLHttpRequest'
};
As you can see, everything seems to be setup correctly. I even tried assigning the token to axios header right before making the POST call but still the same issue persists - I keep getting "CSRF token mismatch". Any ideas what could be wrong?
EDIT: I have also tried appending "_token" with csrf token value in form body, still same issue.

Laravel + Vue.js - CORS issue with specific address?

Laravel 5.6
Vue 2.5.7
Google Chrome
Hi, I am trying to understand this CORS issue, i'm still trying to find a way to consume this list: https://api.coinmarketcap.com/v2/listings/, and I receive the following error:
(index):1 Failed to load https://api.coinmarketcap.com/v2/listings/: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://tours.mine' is therefore not allowed access.
yet if I goto this address: https://jsonplaceholder.typicode.com/posts/ everything works fine.
After using this Moesif Chrome CORS extension, and thus disabling CORS for chrome, I received a new error:
Request header field X-CSRF-TOKEN is not allowed by Access-Control-Allow-Headers in preflight response. received only on this address: https://api.coinmarketcap.com/v2/listings/
http://tours.mine - is a local name I set in httpd/vhosts.conf.
I've tried BarryVdh cors lib, I also created my own CORS middleware, nada.
Flow:
in web.php routes:
Route::get('/', function () {
return view('welcome');
});
in welcome.blade I pass the csrf in both meta:
<meta name="csrf-token" content="{{ csrf_token() }}">
and script:
<script>
window.Laravel = <?php echo json_encode([
'csrfToken' => csrf_token(),
]); ?>
</script>
My Vue instance:
<div class="container" id="app">
<coin-add-component></coin-add-component>
</div>
and in my component I have the following hook:
mounted(){
this.axios.get('https://api.coinmarketcap.com/v2/listings/')
.then(response => {
console.log(response.data);
})
.catch(e => {
this.errors.push(e)
})
}
Your help is appreciated,
Bud
The url you are trying to consume can't be used in crossdomain with javascript because it doesn't provide a response header of type "Access-Control-Allow-Origin".
In this case, if you have no control on the API server you are forced to use other unconventional ways because all modern browsers will block any requests to that site if the domain doesn't match with yours.
You have 2 alternative to solve this problem:
Use a proxy with your same domain of yours to redirect all calls to that server
Make ajax calls to your server and then make your server communicate directly with the api server using for example curl
if you are using this(https://github.com/barryvdh/laravel-cors) make sure you turn off csrf token in your mid

how to fix CSRF token not found on console

How to fix CSRF token not found on laravel 5.4, i try to learn vue js in laravel but i have error in my console "CSRF token not found", help me how to fix this error.
You can add the following meta tag
<meta name="csrf-token" content="{{ csrf_token() }}">
If you are using Vue, here's the way to go:
Vue.http.interceptors.push(function (request, next) {
request.headers['X-CSRF-TOKEN'] = Laravel.csrfToken;
next();
});
or
<script>
window.Laravel = <?php echo json_encode([
'csrfToken' => csrf_token(),
]); ?>
</script>
1) Where this error come from ?
This error come from resources/js/bootstrap.js
2) Why this error occured ?
see below snippet , it is try to find out meta tag of name csrf-token ,
if token found then add as headers to axios http library.else show error
let token = document.head.querySelector('meta[name="csrf-token"]');
if (token) {
window.axios.defaults.headers.common['X-CSRF-TOKEN'] = token.content;
} else {
console.error('CSRF token not found: https://laravel.com/docs/csrf#csrf-x-csrf-token');
}
3) What is the Solution ?
VerifyCsrfToken middleware will check for the X-CSRF-TOKEN request header.
You could store the token in an HTML meta tag:
<meta name="csrf-token" content="{{ csrf_token() }}">
It will generate token as shown in Image :
For Ajax Request :
$.ajaxSetup({
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
}
});
For VueJS 2.0 :
Vue.http.headers.common['X-CSRF-TOKEN'] = document.head.querySelector('meta[name="csrf-token"]').content;
read more about CSRF token : https://laravel.com/docs/5.8/csrf
If you are using
let token = document.head.querySelector('meta[name="csrf-token"]');
Try using
let token = document.querySelector('meta[name="csrf-token"]');
Basically your script is unable to read meta tag with csrf-token, in that case, this should work.
What could be a problem as well is if you're ending a #section with a semicolumn. Like what happened with me I was doing
#endsection;
Which caused the error. When I changed it to
#endsection
The error was gone.
Or Else You can simply pass the URl in $except array, to exclude that URL from CSRF verification.
File
app/Http/Middleware/VerifyCsrfToken.php
Like this
protected $except = [
"readPDF/*",
];
This solution only applies when particular URL is need to be excluded from csrf verification so use it carefully guys.
In your bootstrap.js file replace this line document.head.querySelector('meta[name="csrf-token"]'); by $('meta[name="csrf-token"]').attr('content');
It would be
let token = $('meta[name="csrf-token"]').attr('content');
if (token) {
window.axios.defaults.headers.common['X-CSRF-TOKEN'] = token;
} else {
console.error('CSRF token not found: https://laravel.com/docs/csrf#csrf-x-
csrf-token');
}
This also solves the post-axios requests when you have forms in a vue component.

Calling gapi.auth.authorize() returns empty access_token

After calling gapi.auth.authorize() and getting the access token on the server side by this command:
gapi.auth.authorize({ response_type: 'code', client_id: "client_id", immediate : false, access_type : 'offline', scope: "scopes", redirect_uri : "redirect_uri"} , handleAuth1);
I call it again to receive a token in the client side:
gapi.auth.authorize({client_id: "client_id", immediate : true, scope: "scopes"}, handleAuth2);
for some reason handleAuth2 gets a result with no access_token. In this case my app asks from the user to allow access for the second time. In this case Google show the "Have offline access" because of the new policy.
Can I get the offline and the online permissions at once?
EDIT: I fixed the issue by using the gapi.auth.signIn() as google added this new API. The trick is to first call the api with these parameters:
<span id="signinButton">
<span
class="g-signin"
data-callback="loginFinished"
data-clientid="808248997275-ol6kol8h23j018iug3d5odi9vhrja9j5.apps.googleusercontent.com"
data-accesstype="offline"
data-cookiepolicy="http://brif.us"
data-scope="profile https://mail.google.com https://www.google.com/m8/feeds https://www.googleapis.com/auth/drive">
</span>
Using the accepted code generate the refresh token on the server side.
The next call should omit the offline line so the code should be:
<span id="signinButton">
<span
class="g-signin"
data-callback="loginFinished"
data-clientid="808248997275-ol6kol8h23j018iug3d5odi9vhrja9j5.apps.googleusercontent.com"
data-cookiepolicy="http://brif.us"
data-scope="profile https://mail.google.com https://www.google.com/m8/feeds https://www.googleapis.com/auth/drive">
</span>
Hope this helps someone...

jquery .ajax() unexpected ;

trying to do some low-level ajax that hits url in the background but does nothing on the client side. Here is the code:
can_id = $(this).attr('id');
$.ajax({
url:"savethis.php",
data:"q=0&can_id="+can_id;
});
but I keep getting this error:
Uncaught SyntaxError: Unexpected token ;
When I comment out the ajax() portion the error goes away. Why am I getting this error?
You have a semicolon at the end of can_id. Remove it
data:"q=0&can_id="+can_id**;**

Resources