pub async fn login<T: Client + TokenClient + CacheClient>(
    options: &LoginOptions<'_, T>,
) -> Result<Token, Error> {
    let LoginOptions {
        api_client,
        color_config,
        login_url: login_url_configuration,
        login_server,
        existing_token,
        force,
        sso_team: _,
    } = *options; // Deref or we get double references for each of these

    // I created a closure that gives back a closure since the `is_valid` checks do
    // a call to get the user, so instead of doing that multiple times we have
    // `is_valid` give back the user email.
    //
    // In the future I want to make the Token have some non-skewable information and
    // be able to get rid of this, but it works for now.
    let valid_token_callback = |message: &str, color_config: &ColorConfig| {
        let message = message.to_string();
        let color_config = *color_config;
        move |user_email: &str| {
            println!("{}", color_config.apply(BOLD.apply_to(message)));
            ui::print_cli_authorized(user_email, &color_config);
        }
    };

    // Check if passed in token exists first.
    if !force {
        if let Some(token) = existing_token {
            debug!("found existing turbo token");
            let token = Token::existing(token.into());
            if token
                .is_valid(
                    api_client,
                    Some(valid_token_callback("Existing token found!", color_config)),
                )
                .await?
            {
                return Ok(token);
            }
        // If the user is logging into Vercel, check for an existing `vc` token.
        } else if login_url_configuration.contains("vercel.com") {
            // The extraction can return an error, but we don't want to fail the login if
            // the token is not found.
            if let Ok(Some(token)) = extract_vercel_token() {
                debug!("found existing Vercel token");
                let token = Token::existing(token);
                if token
                    .is_valid(
                        api_client,
                        Some(valid_token_callback(
                            "Existing Vercel token found!",
                            color_config,
                        )),
                    )
                    .await?
                {
                    return Ok(token);
                }
            }
        }
    }

    let redirect_url = format!("http://{DEFAULT_HOST_NAME}:{DEFAULT_PORT}");
    let mut login_url = Url::parse(login_url_configuration)?;
    let mut success_url = login_url.clone();
    success_url
        .path_segments_mut()
        .map_err(|_: ()| Error::LoginUrlCannotBeABase {
            value: login_url_configuration.to_string(),
        })?
        .extend(["turborepo", "success"]);

    // Create the full login URL.
    login_url
        .path_segments_mut()
        .map_err(|_: ()| Error::LoginUrlCannotBeABase {
            value: login_url_configuration.to_string(),
        })?
        .extend(["turborepo", "token"]);

    login_url
        .query_pairs_mut()
        .append_pair("redirect_uri", &redirect_url);

    println!(">>> Opening browser to {login_url}");
    let spinner = start_spinner("Waiting for your authorization...");

    // Try to open browser for auth confirmation.
    let url = login_url.as_str();
    if !cfg!(test) && webbrowser::open(url).is_err() {
        warn!("Failed to open browser. Please visit {url} in your browser.");
    }

    let token_cell = Arc::new(OnceCell::new());
    login_server
        .run(
            DEFAULT_PORT,
            crate::LoginType::Basic {
                success_redirect: success_url.to_string(),
            },
            token_cell.clone(),
        )
        .await?;

    spinner.finish_and_clear();

    let token = token_cell.get().ok_or(Error::FailedToGetToken)?;

    // TODO: make this a request to /teams endpoint instead?
    let user_response = api_client
        .get_user(token.as_str())
        .await
        .map_err(Error::FailedToFetchUser)?;

    ui::print_cli_authorized(&user_response.user.email, color_config);

    Ok(Token::new(token.into()))
}
Login returns a `Token` struct. If a token is already present,
we do not overwrite it and instead log that we found an existing token,
setting the `exists` field to `true`.

First checks if an existing option has been passed in, then if the login is
to Vercel, checks if the user has a Vercel CLI token on disk.
crates/turborepo-auth/src/auth/login.rs