How to allow users of different stages of the NFT sale to mint tokens of different rarity?

I have simple smart contract with mint for public sale, presale and whitelist. And i have a collection of nft tokens grouped sequentially into folders according to rarity. I am interested in how, for example, users from the whitelist can be allowed to mint legendary tokens, and users from the public sale - rare tokens.
My idea is to load tokens according to their rarity on ipfs and get several CIDs, then during the mint use a function that, according to a side parameter in the function I wrote, will change the actual CID of the tokens. But I would like to find a ready-made solution, or can someone explain to me how to do this please?

This is very similar to what I need to do, did you find a good solution?