While some people are religious at least in part because they desperately want to believe in an afterlife that their religion promises them, not all religious people believe in an afterlife and not all those who believe in an afterlife are as blithely optimistic as the average contemporary American that of course they themselves will spend it in heaven. There are many people who not only adamantly persist in believing in fantastic, unsupportable, superstitious, supernaturalistic afterlife nonsense but actually do so despite having increased anxieties about death on account of precisely those beliefs. Not everyone believes the implausible because it makes them happier to do so.
And do you know what religion’s adherents have a particularly high anxiety about death and what the reasons for that anxiety are? Read Tom Rhees’s discussion of some research into this for some possible answers.
And then also check out some interesting research he summarizes that shows that believers in the afterlife are more prone to believe that things generally go fairly in this world. That is a really interesting conclusion which runs contrary to the view, espoused by many, that hopes for the afterlife are a form of psychological compensation for a sense that the present world is unjust. It is often thought that the afterlife is where people hope the general injustice of the world we live in will be finally rectified or where they hope that unjust burdens they endure in this life will finally pay off, etc.