简体   繁体   中英

How to get the csrf token “outside” laravel view?

i have a cordova app connected to a laravel api. I need to make a post from the mobile app to that laravel, but i need the csrf token. I cannot do the {{csrf_field}} because the view i´m using in the mobile is not provided by laravel so no blade or laravel helpers. I tried doing a previous ajax call only to get the token, but i don´t know if this is the best way to do it. Thank you!

In your case I wouldn't recommend to you to get the CSRF token.

Instead I'd suggest you to construct a personal authorization code (per user) or removing the corresponding URLs from the CSRF check (maybe even the whole API).

The token is linked to the currently authenticated user. What you're trying to do won't work.

Try using the API middleware to login and store the token on the mobile app, and use that to identity yourself.

While as the other answers have said, this isn't a recommended solution for your problem, Laravel has a helper function to give you the CSRF token, aptly named csrf_token() .

It's listed on the helpers page of Laravel's documentation: https://laravel.com/docs/5.4/helpers#method-csrf-token

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM