API not returning updated data - laravel

I have this weird problem. It works fine on my local machine. But the problem happens when I upload this project on the live server (in a sub-domain for testing purposes). GitHub Repository
The project has one Model (other than User), "Post"
The project has one route and one API endpoint. Initially, I should get the same results from both the web route and API endpoint. The problem is when I delete a post from the web, the API result still shows that deleted post. The API returns updated results after 20-30 minutes.
Very strange issue.
If anyone knows anything about such an issue, please help me out.
I am using Livewire
App\Http\Livewire\Posts.php
public function render()
{
$posts = DB::table('posts')
->select(DB::raw("
posts.id AS id,
posts.title AS title,
posts.category AS category,
posts.author AS author
"))
->orderBy('posts.category')
->get();
$data = [
'posts' => $posts
];
return view('livewire.posts', $data);
}
api.php
Route::get('posts', function() {
$posts = DB::table('posts')
->select(DB::raw("
posts.id AS id,
posts.title AS title,
posts.category AS category,
posts.author AS author
"))
->orderBy('posts.category');
return response()->json($posts->get(), 200);
});

Actually, it was my fault. I did try
php artisan cache:clear
php artisan optimize:clear
And it did not work since there was a caching configuration in Nginx.
You know, when you are a noob at server management and end up using a nice and ready solution like myvestacp, you may end up making silly mistakes like this.
The way myvestacp is configured, it utilizes a combination of Apache and Nginx (as proxy). Without knowing the outcome, I had set the Nginx proxy as "caching" instead of "hosting-public". This is why the API result would get cached and only update after some time.
I did not face any issues so far because I did not use API before.
But thank you anyway for your time and effort.

Related

User authenticating but not staying logged in Laravel 8.x

When testing my application and trying to create a new user then on form submission I am getting error 419.
If I use a helper to log in using Auth::loginUsingId(x) for an existing user then dd(auth()->user()) is successful but when I redirect to another page the user is no longer logged in. eg running the following code, you end up getting redirected to /login:
Route::get('/logInAs/{id}', function ($id) {
Auth::loginUsingId((int) $id);
dd(\auth()->user());
return redirect()->route('dashboard');
});
I would share more code but I'm not sure what would be useful at this point?
I've probably spent about seven hours trying to fix this so far. It's driving me up the wall!
The app works fine on other environments, such as staging. I've even gone so far as to re-download it from github as a fresh install (in case I was messing about in vendor directory and changed something), but that hasn't fixed it.
I get the same issue on Chrome, using different signed in users on Chrome, using incognito mode on Chrome, using Firefox, and using valet share and accessing my local version on another device, like a phone, so I don't think it is to do with browser caching.
When other devs working on the project run it locally on their machines I haven't had reports of this issue, so it seems to just me my local environment.
I've variously tried changing CACHE_DRIVER and SESSION_DRIVER in .env to no joy, but I'm beginning to lose the will to live so don't really know if I'm doing the right thing there - please help if you can!!
Edit to add more detail:
Middleware on the dashboard route:
Route::middleware(['auth:sanctum', 'verified'])->group(function(){
Route::get('/dashboard', DashboardController::class)->name('dashboard');
});
If I remove the middleware then I just get an error that user is null on dashboard.blade.php, ie the user is still not staying logged in.
A custom middleware:
class VerifyUserIsMerchant
{
public function handle(Request $request, Closure $next)
{
if ($request->user()->isMerchant()) {
return $next($request);
}
throw new AccessDeniedHttpException('You must be a merchant to access this page');
}
}
If I dd($request->user()) in this middleware it returns null.
The issue got fixed with the help of Laracasts. The full answer is here.
In short, I needed to remove the http:// protocol from SESSION_DOMAIN in .env and set it to match APP_URL.
This was the correct answer for this issue. It was deleted by a moderator. Moderator, before you delete this answer as well please can you explain why?
It provides the full answer to the issue raised in the question. I did not find this answer to this issue anywhere else (hence why it took me several days of debug to fix).
If I can know why it was deleted, I can then understand how to avoid making answers in the future that also would be deleted. Thank you.

Problem with Laravel routes - all sub folder traffic ending up in route view

Sorry - was difficult to give this one a clear title! But I have an issue and a difference between how my local laravel install is dealing with some routes compared to my live server.
Locally, I have this working:
Route::get('/blog', 'BlogController#home');
Route::get('/blog/{post_slug}', 'BlogController#viewPost');
As you can probably guess, I want to serve up a list of posts via the home() function if /blog is hit. Then all other traffic with a "slug" after /blog/, I want to load the blog post.
This all works locally.
However on live,
/blog/my-blog-post
Is serving up the home() function every time.
Where would I start with debugging this. Laravel versions? Server caching?
Maybe you can do this in laravel 5.7+
Route::prefix('blog')->group(function () {
Route::get('/', 'BlogController#home');
Route::get('/{post_slug}', 'BlogController#viewPost');
});
before just use: php artisan optimize, to clear all cache route and config.
for more info see the docs

GET Parameters cannot be retrieved from the Laravel Controller, after making it live to Linux Server

I am having an issue after making my Laravel Project live on the Server. Any GET parameters cannot be retrieved from the controller in the entire project, whereas it is running fine on my local server and I also tried in different servers, but when I switching it to the Production Server it is not been able to read the URL Parameters.
For Example, If I consider the URL as below:
http://www.example.com/?type=moble&value=recharge
My Controller Function
/***** In the Starting I had declared use Illuminate\Http\Request; *****/
public function index(Request $request)
{
echo $request->type;
echo $request->value;
}
Suppose the above URL pointed to this index function, it is returning NULL in the server.
I sorted the issues out. It was a problem in the htaccess file.

Laravel Socialite InvalidStateException in AbstractProvider.php line 200

I'm building a web app in my local system (Ubuntu-14.04 64Bit) using laravel 5.3. I used Socialite to signin from social networks. I configured G+, Facebook, GitHug. I'm using Chromium as my default browser. Finally the problem is i'm getting
InvalidStateException in AbstractProvider.php line 200
frequently. i tried
php artisan cache:clear
php artisan config:clear
composer dump-autoload
these are helping to solve the issue temporarily, again the problem raising.
please help me in this issue..
I have the same issue and I've read a lot about this, that depend if the URL where you are at the moment of the login request has www. at the beginning or not.
Into config\services.php, if you have the redirect set as http://sitename.tld/callback/facebook the oauth works if you send the login request from sitename.tld, while if you try from www.sitename.tld you get the exception.
I haven't yet understood how to have it working with and without www at the beginning.
If the AbstractProvider.php line 200 fires the exception when the state of the user is not present means that the User cannot be created.
First check your code when you get the details from the provider(facebook, github) if you create a user and you return it.
If you have managed and logged in your app and you deleted the user from the user table remember to delete also the data from the socialite account table.
I was getting that exception because 'state' wasn't saved in session. But I was using asPopup method - Socialite::driver('facebook')->asPopup()->redirect(); so I saved session then - $request->session()->save();. So I solved this issue.
or try
session()->put('state', $request->input('state'));
$user = Socialite::driver('facebook')->user();
it works
I have same issue and solved in 3 steps;
add request on the top
use Illuminate\Http\Request;
Pass request object to function
public function handleProviderCallback(Request $request)
{
try {
$user = Socialite::driver('facebook')->user();
} catch (Exception $e) {
throw new Exception;
}
}
Clear cache.
php artisan cache:clear
I had the same error but my solution was a little different. I am posting here just in case someone else keeps hitting this post like I did for a possible answer.
I develop on Ubuntu 18.04 desktop since it is a server with a GUI. Socialite works great locally but as soon as I pushed/pulled the changes through git to the server, it quit.
I was running traces by recording what was sent to and from google. I "dd($_GET)" to get a raw dump before Socialite had a chance to get the info so I knew what was stored and ready for use. All info was there but Socialite didn't seem to "see" it. That is when I reasoned it was my apache2 header configuration interfering with the cookies/session data.
I had set header security in my apache2 configs. One of the settings was
Header always edit Set-Cookie ^(.*) "$1;HttpOnly;Secure;SameSite=Strict"
This setting was interfering with the cookie information that socialite needed. I removed that setting from my apache2 header config(by commenting out) and restarted Apache. Finally I removed all sessions in storage/framework/session/* and cleared them from my browser just to be sure. That worked for me.
After I got it working, one by one enabled and tested each of the following settings to have the framework secure what header info it can:
SESSION_SECURE_COOKIE=true
in my .env file
'http_only' => true, and
'same_site' => 'lax'(setting to "strict" did not seem to work)
in my config/session.php file.
Now it is back to testing security and tweaking things back if need be.

Laravel 5 CORS-issue vs. Ionic Angular

I am currently making an Ionic/Cordova-application with Laravel 5 as a Rest-server (my first time coding a PHP-server).
With Postman all my Get/Post/Update/Delete-functions work on Laravel, and on clientside (Ionic Cordova). I am able to send data to http://postcatcher.in using Chrome Allow-Control-Allow-Origin plugin.. Without the plugin, I get this error:
Since this problem apparently only happens in development-mode (when testing client-side in browser), I assume it's alright to develop with the plugin.
When I try to send data to Laravel through Ionic Cordova, I get this error (even with the Allow-Control-Allow-Origin plugin:
I have tried multiple things, such as https://github.com/barryvdh/laravel-cors, which just doesn't seem to work for me. Neither does suggestions in this forum, using CORS-middlewares. https://laracasts.com/discuss/channels/requests/laravel-5-cors-headers-with-filters
I assume that this is a Laravel-issue, but I am not 100% sure.
To publish the server, I use
php artisan serve, giving it localhost:8000.
For the application, I write: ionic serve, which gives it localhost:8100.
At last, this is the code I use to send data on Ionic-side:
.factory('userFactory', function($http, $q) {
return{
createuser : function(info) {
var deferred = $q.defer();
$http.post('localhost:8000/users', info)
.success(function(response, status){
deferred.resolve(response);
})
.error(function() {
console.log('SOMETHING WENT WRONG');
});
return deferred.promise;
}
}
})
Any help is really appreciated. Really stuck with this issue.
So it turns out that I'm an absolute idiot. I just needed to write "http://" in front on the URL. Rookie mistake.
EDIT: I'll keep the question open for just half an hour, in case anyone has advice for me.

Resources