Refactored the code quite a bit. Now authentication should work properly.