Refactor token authentication