Livewire actions get 302 response - laravel

I have a laravel project deployed on a domain subfolder
https://example.com => another project
https://example.com/dashboard => my laravel project
I have set my livewire config asset_url to config('app.url'), which my .env's APP_URL is https://example.com/dashboard
It is a simple project that has an auth middleware which protect https://example.com/dashboard and redirect it to https://example.com/dashboard/login. And when I try to fill the input (it has wire:model attribute), I get an error at my JS console
Uncaught (in promise) SyntaxError: Unexpected token < in JSON at position 0 at JSON.parse (<anonymous>) at index.js:58
When I check the Network tab, there were some process:
https://example.com/dashboard/livewire/message/login (post) => 302 (i think it is not expected)
https://example.com/dashboard/login (get) => 200 (i think it makes the error above)
It works fine if I deploy it to https://example.com, but at https://example.com/dashboard it keeps getting this issue
Could you guys help me?

Take a look at the livewire.php config folder, maybe you have to change the path for the Livewire assets

Related

How to stop the GET method is not supported for this route from showing?

I have a working Laravel project with loads of different routes.
I'm currently testing it and one of my tests was to check if a user were to use a delete or post route from the URL. I didn't know what the application would do honestly and it outputted the typical:
The GET method is not supported for this route. Supported methods: DELETE
which I have seen a million times. Is there a way to stop this error from coming up and instead output an error screen or simply redirect to a different view?
The error message:
The GET method is not supported for this route. Supported methods: DELETE.
should only appear when your Laravel site has APP_DEBUG set to true (in the .env file).
When APP_DEBUG is set to false as it should always be in on a live site, then the user will be shown a 404 error page instead.
You can easily test this by adding the following code to your routes file:
Route::delete('test', function() {
return 'You should never see this text when viewing url via a GET request';
});
May be u didn't noticed but ur form tag method attribute and route definition is different

Laravel api - 405 not allowed shared hosting

I'm new to Laravel and i have the next problem.
I have an API POST route that works properly in the localhost. I send POST requests from POSTMAN:
Route::group(['middleware' => 'api_import'], function () {
Route::get('/products', [ArticlesController::class, 'index']);
Route::post('/products', [ArticlesController::class, 'addOrUpdateArticles']);
Route::post('/categories', [CategoriesController::class, 'addOrUpdateCategories']);
});
However, it will not work on hosting. Hosting is shared, and I get the message:
When json contains a couple of products everything works properly. When json contains a complete table of 3500 products I get this error.
It is possible that there is a redirect on the page. You should confirm this through the network tab in your browser, because any POST request with a redirect is considered a GET request, so an error appears (405 Method Not Allowd)

404 Not Found on sanctum/csrf-cookie path

So I've been building a projet based on laravel. I'm building on a SPA foundation with sanctum as my authorization package. It works perfectly. Then I deploy the project to the server and everytime I try to login there is a 404 error on /sanctum/csrf-cookie.
How could this happen? Is it because the SanctumServiceProvider not working.
The problem is when you define sanctum prefix, the route become something else like this:
you can check your routes with : php artisan route:list
as you can see the /sanctum/ is removed and when you check the route /sanctum/csrf-cookie it will not be and throws 404 error. So you have two options:
add this prefix: 'prefix' => 'api/v1/sanctum'
or
change GET call to api/csrf-cookie
You need to check if you're setting correct axios defaults for your /sanctum/csrf-cookie api call.
You can set it as follows
axios.defaults.withCredentials = true;
axios.defaults.baseURL = "http://localhost"; //Before sending get request
axios.get("/sanctum/csrf-cookie").then(async () => {
//Logic to handle login
});
If defaults are not set properly url becomes http::localhost::8080/sanctum/crf-cookie which is where frontend is serving but instead it has to be http::localhost/sanctum/csrf-cookie
Note: localhost example is just for explanation. For production server make sure your url is correct and api call is on backend.
I solved this issue by adding:
AllowOverride All
to the apache directory config
add in last line inside config/sanctum.php
'routes' => false,
Add in config/cors.php
'paths' => ['*']

Laravel Dusk auth issues with Laravel Passport

Can't test applications with Laravel Dusk if the app uses Laravel Passport for authentication. It's always 404 and if you stop the test with ->stop() it starts working.
The app is located: localhost:3000
and all requests are proxied to api.example.test
I've tried to authenticate the user inside the test:
$this->actingAs(User::find(1), 'api');
$browser->loginAs(User::find(1));
Passport::actingAs($this->user);
But still the same.
That's not the only problem, I think the main issue is also that I can't log in with Dusk by filling out the form and click "login"
$browser->visit('/login')
->type('#email', 'john#doe.com')
->type('#password', 'password')
->click('#login-button')
->waitForLocation('/');
But again, if you stop the test with ->stop() you can manually click the button and it will log you in.
Is Laravel Dusk useless if your app does not use basic auth?
Solved.
The problem was that my Dusk .env file had APP_URL=localhost:3000 where the front-end is located, but my APIs .env file had APP_URL=api.exmaple.com and that's why in my AuthController, I issued the token from env('APP_URL').'/oauth/token' <- which resulted in Dusk env to localhost:3000/oauth/token which is ofc 404
I changed that to env('API_URL') and added that value to my both .env files.

Trying to set up CAS with my Laravel project

I am using subfission/cas for my application. I have followed all installation steps. I am using windows, if that matters. More precisely, I have configured the following:
I ran the following in my terminal
composer require "subfission/cas" "dev-master"
I configured my Kernel.php accordingly, adding the following:
'cas.auth' => 'Subfission\Cas\Middleware\CASAuth',
'cas.guest' => 'Subfission\Cas\Middleware\RedirectCASAuthenticated',
I ran the following command:
php artisan vendor:publish
I also set up my cas server in my cas.php config file:
'cas_hostname' => env('CAS_HOSTNAME', 'cas.myserver.me'),
'cas_real_hosts' => env('CAS_REAL_HOSTS', 'cas.myserver.me'),
What I want is a middleware for all my routes, so I added the following route rule in my routes:
Route::middleware(['cas.auth'])->group(function ()
{
Route::get('/', function ()
{
return view('welcome');
});
});
Basically, I want to redirect everyone who is not logged in to the login page whenever I access the main page (for now, I will add more routes in the future). What happens is that the users are redirected to the login page when they are not logged in, but after the login I receive the following error:
ErrorException (E_WARNING)
DOMDocument::loadXML(): Opening and ending tag mismatch: hr line 1 and body in Entity, line: 1
No matter what view I'm redirecting the user to. I tried the default welcome page as well as an empty view, but I still get the same error.
EDIT: I have used the dev-master branch from subfission/cas for the above error and after switching to 2.1.1, I get a different error:
session_name(): Cannot change session name when headers already sent
EDIT 2: I did some more digging and I enabled internal errors in my cas client class with:
libxml_use_internal_errors(true);
And now I get the following:
Authentication failure: SA not validated Reason: bad response from the CAS server
And the cas response is:
The thing is that I use the same cas server for another 2 projects and it works well for those (but those aren't laravel projects.
I know it's been a while, but for anyone else having issues like this, the issue is the protocol selected for which your web service communicates with your CAS(Central Authentication Service) provider. There are two main protocols used for SSO/CAS in this package:
SAML(Security Assertion Markup Language) version 1.1 & 2
CAS Protocol v3.0
[Confusingly enough, CAS protocol shares the same namespace as the service.]
The idea is to match the protocol and version with your identity provider. It sounds like your provider is using CASv3.0, which is why disabling SAML worked.
Also, if you enable debug mode, you will see further error details in your log file to help your troubleshoot.
Best of luck!
I managed to solve the issue by disabling the SAML in the cas configure file:
'cas_enable_saml' => env('CAS_ENABLE_SAML', true),
change to
'cas_enable_saml' => env('CAS_ENABLE_SAML', false),

Resources