Refactor auth with ensureAuthenticated middleware