40 euros a night in Barcelona is pretty tight if you're looking for something decent, but look, it's not impossible if you know where to look. There are plenty of hostels around, though sure, at that price you'll often have to share a room. What I'd say is don't just stick to Facebook like was mentioned before, because even though it works, it really depends on whether the owners feel like responding or if they just straight up ignore you.
Also try websites like booking.com or hostelworld filtering by low price - surprisingly, some hostels actually have more competitive prices there than on Airbnb. Then you've got options like BlaBlaCar which has accommodation listings besides travel, or just straight up Google "cheap hostels Barcelona" and contact them directly by phone or email. A lot of cheap hostels aren't even on platforms because they fill up on their own from word of mouth.
But yeah, be careful when you contact places directly without a middleman: always ask for current photos, talk to the owner first, and if something feels off (they want you to pay everything upfront, there's no way to reach them after...), don't risk it. Barcelona's pretty safe in that respect but there's all kinds of people on the internet, you know.