I think the answer is trauma bonding. Like when you and someone have been through the same situation or similar situations that were traumatic y of bond because of the shared experience. You can also bond with someone who has shared a positive experience