implementation of authenticate, plus some resource refactoring