Factor out auth logic