refactor generate_token method, let it easier to be reused