I get it—those newer LED headlights can be blinding sometimes. No matter the circumstance, it is not illegal to wear sunglasses while driving at night. That doesn’t mean it’s recommended, though.
Other than migraines, you may have other reasons for needing sunglasses at night—if you normally wear glasses but only have access to prescription sunglasses, for example. But it’s important to avoid wearing polarized lenses, as they can impair your vision.
If headlights are just too bright or causing a glare, you may benefit from a thorough windshield cleaning. Try getting a car wash and paying attention to if it helps.