Refactor and extend authentication

- Return proper error on auth failures
- Extract common auth logic to function
- Add auth code functionality
- camelCase variable names