Refactored authentication to do less database queries