docs: def _login(server, creds_file=None, api_key=None, silent=False):
      """
      Login main function
      """
      # fetch_api_key and check_version expect server to end in /
      if server[-1] != "/":
          server = server + "/"
  
      # creds file path setup
      if creds_file is None:
          creds_file = os.path.expanduser("~/.onecodex")
  
      # check if the creds file exists and is readable
      if not os.path.exists(creds_file):
          if silent:
              return None
  
          creds = {}
      elif not os.access(creds_file, os.R_OK):
          click.echo("Please check the permissions on {}".format(collapse_user(creds_file)), err=True)
          sys.exit(1)
      else:
          # it is, so let's read it!
          with open(creds_file, "r") as fp:
              try:
                  creds = json.load(fp)
              except ValueError:
                  click.echo(
                      "Your ~/.onecodex credentials file appears to be corrupted. "  # noqa
                      "Please delete it and re-authorize.",
                      err=True,
                  )
                  sys.exit(1)
  
          # check for updates if logged in more than one day ago
          last_update = creds.get("updated_at") or creds.get("saved_at")
          last_update = last_update if last_update else datetime.datetime.now().strftime(DATE_FORMAT)
          diff = datetime.datetime.now() - datetime.datetime.strptime(last_update, DATE_FORMAT)
  
          if diff.days >= 1:
              # if creds_file is old, check for updates
              upgrade_required, msg = check_version(__version__, server)
              creds["updated_at"] = datetime.datetime.now().strftime(DATE_FORMAT)
  
              try:
                  json.dump(creds, open(creds_file, "w"))
              except Exception as e:
                  if e.errno == errno.EACCES:
                      click.echo(
                          "Please check the permissions on {}".format(collapse_user(creds_file)),
                          err=True,
                      )
                      sys.exit(1)
                  else:
                      raise
  
              if upgrade_required:
                  click.echo("\nWARNING: {}\n".format(msg), err=True)
  
          # finally, give the user back what they want (whether silent or not)
          if silent:
              return creds.get("api_key", None)
  
          click.echo(
              "Credentials file already exists ({}). Logout first.".format(collapse_user(creds_file)),
              err=True,
          )
          return creds.get("email", None)
  
      # creds_file was not found and we're not silent, so prompt user to login
      email, api_key = login_uname_pwd(server, api_key=api_key)
  
      if api_key is None:
          click.echo(
              "We could not verify your credentials. Either you mistyped your email "
              "or password, or your account does not currently have API access. "
              "Please contact help@onecodex.com if you continue to experience problems."
          )
          sys.exit(1)
  
      creds.update(
          {
              "api_key": api_key,
              "saved_at": datetime.datetime.now().strftime(DATE_FORMAT),
              "updated_at": None,
              "email": email,
          }
      )
  
      try:
          json.dump(creds, open(creds_file, "w"))
      except Exception as e:
          if e.errno == errno.EACCES:
              click.echo("Please check the permissions on {}".format(creds_file), err=True)
              sys.exit(1)
          else:
              raise
  
      click.echo("Your ~/.onecodex credentials file was successfully created.", err=True)
  
      return email