Laravel 5 - web and API Route conflict - api

In one of my website in laravel. I facing error 419 when i called login api below my search web route
Route::get('/{address_1}/{address_2?}/{address_3?}/{address_4?}');
Route::post('/{address_1}/{address_2?}/{address_3?}/{address_4?}');
And Below Api Route
Route::post('login', 'Auth\PassportController#login');
Route::post('register', 'Auth\PassportController#register');

In Laravel, a 419 HTTP error code is usually a sign that you are missing a CSRF token.
If the 'API' routes:
Route::post('login', 'Auth\PassportController#login');
Route::post('register', 'Auth\PassportController#register');
Are in your web.php file then this is likely because the RouteServiceProvider wraps the routes in your web.php file inside the web middleware which has the CSRF middleware enabled.
If this is the case, you will either need to include a CSRF token in your POST request, move these routes to the api.php routes file, or add the register endpoint to the $excludes array of the VerifyCsrf middleware.
update
Actually, your search routes are going to cause a lot of grief.
Given the fact that they can match any URL with one to four segments, your API routes are getting swallowed by these.
What you will need to do is alter your RouteServiceProvider to register web routes last and then put your search routes last.
This way, your pre defined routes will be registered before your “catch-all” search routes.

Related

NextJS API Routes Callback

In my application I want to offer users to connect their Google account in order to receive data from the Google Business API.
I pass the API Route as a redirect URL, as I want to catch the code passed and write the token to the database.
Unfortunately when redirecting to the redirect URL and rendering this API Route returns in ERR_SSL_PROTOCOL_ERROR.
How can I correctly work with the API Routes to not render them in the UI, but use them to perform the action.
I solved the problem. I was redirecting to https://localhost:XXXX
Unfortunately there is no https on localhost and hence http://localhost has to be used

Cannot access to Laravel register and login views

Attempting to learn and domain Laravel I developed a website with it 5.6 version and vuejs 2.
In it's admin section I want to implement an authorization system. I tryed with Passport API and axios but I can't achived it. So I undid all I was made and now, I'm trying to implement the Laravel Authorization but I can't access to /login and /register routes. When you go to those urls the page automactically redirects you to home page. I deleted cache and cookies, and the issue persists. I reviewed the auth controllers and services and they are identically to the factory version.
I removed the vue routes to avoid conflicts.
It doesn't matter what path is written in LoginController.php it always redirects to home page. So the problem is not there... any way, I'm lost
Please check php artisan route:list to make sure you have the desired routes defined.

Proper 404 handling by server with vue-router in history mode

When using history mode in vue-router the documentation is suggesting a pretty dodgy way to get around some of the limitations it has.It suggests a server-side configuration that catches all URLs that could be a client-side route and rewriting to root (/) so the client-side app is delivered. And then another catch-all route to a 404 component in the client-side router if no routes match.
Problem is, this will mean your server is returning 200 OK status codes to crawlers/indexers for basically every URL, specifically ones that don’t technically exist.
My thoughts so far:
Use IIS <rewriteMap> to list the valid client-side route patterns I have and use that for matches instead of a catch-all on everything not a file/dir.
Problem: pain to manage in tandem with client-side routes.
Routes defined in server config and handed to client-side router via an api endpoint for registration
Problem: setting up an API when you just want to host a static app is a pain.
Any other suggestions?

Laravel's intended() with vue router

I am working on a project and am struggling with redirecting to intended location after login.
The problem is that Laravel does not include Vues route (anything after #/...) so it always redirects me only to 'domain.com/'
I am using laravel routing only for 'login' 'logout' and '/' and rest of the app is single page utilizing vue routing.
Users of the app are receiving notification emails when they need to take action. Those email contain links to requests where their action is required (e.g. domain.com/#/request/3413). Of course they need to login to be able to access that so they are redirected to login page by laravel (domain.com/login#/request/3413)
After successful login I am trying to redirect them with
return redirect()->intended('/');
But it redirects them to 'domain.com/' instead of 'domain.com/#/request/3413'
Is there any way to make laravel include vues route in that redirect?
Thanks a lot!
So after some excruciating research I have found out that anything after # is handled by browser meaning that server does not see it so you cant access it in your PHP code. I found out thanks to this thread: Getting FULL URL with #tag
I am unsure if that part of the request is not even sent to server but it seems like that and is even pretty logical to me that it would be so.
So to solve this I changed the link sent to user via email to something like this
domain.com/?request=3413
This way I can access it in my PHP code and put together the redirect link after successfull login.
You can remove the # in the URL in Vue Router by enabling History Mode like so:
const router = new VueRouter({
mode: 'history',
routes: [...]
})
You will need to direct all your requests to where you app lives. If your backend is Laravel, you can define a catch-all route in your routes/web.php file
Route::get('/{any}', 'SpaController#index')->where('any', '.*');
The caveat to this is that the server will no longer return 404s for paths that are not found so you will have to create a NotFoundComponent to display on your Vue app if a path is not found.
const router = new VueRouter({
mode: 'history',
routes: [
{ path: '*', component: NotFoundComponent }
]
})
Read the Vue Router History Mode Documentation for more info

MVC 4 Web Api Routing - Use relative path for routing URL

Problem:
I am trying to call the ApiController from localhost:xxxx/Home/TestPage and get a 404 that the Requested URL: /Home/api/Test cannot be found.
If I make this call from the Index.cshtml page it works fine and I can navigate to localhost:xxxx/api/Test and see the desired JSON. I've tried adding ~/ to my MapHttpRoute routeTemplate but that throws an exception.
Question:
How do I remove /Home from the URL request?
Another example would be if I am on the page localhost:xxxx/People/TestPage and I want to hit multiple URIs such as localhost:xxxx/api/Hobbies, localhost:xxx/api/Pets and locahost:xxx/api/Vehicles. How would I set up the custom routing to handle this? As of right now with the default routes, I get the error that it can't find /People/api/Hobbies and so forth.
SOLUTION (Possibly):
I created a custom MapHttpRoute with the routeTemplate: "{page}/api/{controller}/{id}" and it works but I don't completely understand WHY and if this is good practice or not.
Home is your default HTTP controller. Home/api/Test is not a valid Web API controller route unless you have created custom routes. The out of the box default web api route is api/{controller}/{id}. So, if you had an API controller called home, it's URL would be localhost/api/home/id