He had a few reasons to believe you were up to no good.

You were parked in an underdeveloped area, at night, that is a first sign to cops to think, "why would he be there, parked?"

Second, when you got pulled over, your friend was roaming around in the glovebox. That is another sign that you may be hiding something.

Third, the smell of marijuana gave it all away, and it was obvious what you were up to.

So yes, he had a right to search you. You also confessed to having cannabis in the vehicle, which promps him to do a entire search, so you don't have more.

Next time, keep your mouth shut, and don't lie when you really have stuff in the car, or they'll really be pissed at you.