Refactoring of backend.py, abstracted the authentication logic.