private/Save-UnraidAuth.ps1

function Save-UnraidAuth {
    [CmdletBinding(SupportsShouldProcess, DefaultParameterSetName = 'Credential')]
    param(
        [Parameter(Mandatory)]
        [string]$ServerName,

        [Parameter(Mandatory, ParameterSetName = 'Credential')]
        [pscredential]$Credential,

        [Parameter(Mandatory, ParameterSetName = 'ApiKey')]
        [string]$ApiKey,

        [Parameter()]
        [switch]$Force
    )

    process {
        $appDataPath = if ($IsWindows -or $PSVersionTable.PSVersion.Major -lt 6) {
            Join-Path $env:LOCALAPPDATA "PSUnraid"
        }
        elseif ($IsMacOS) {
            Join-Path $env:HOME "Library/Application Support/PSUnraid"
        }
        else {
            Join-Path $env:HOME ".config/PSUnraid"
        }

        if (!(Test-Path $appDataPath)) {
            Write-Verbose "Creating PSUnraid directory at $appDataPath"
            New-Item -ItemType Directory -Path $appDataPath -Force | Out-Null
        }

        $authFilePath = Join-Path $appDataPath "auth.xml"

        # Load existing auth data or create new structure
        $authData = @{}
        if (Test-Path $authFilePath) {
            try {  $authData = Import-Clixml -Path $authFilePath  }
            catch {  Write-Verbose "Could not load existing auth file, creating new"  }
        }

        # Check if server already exists and ask for confirmation if not forced
        if ($authData.ContainsKey($ServerName) -and !$Force) {
            if (!$PSCmdlet.ShouldProcess("$ServerName authentication", "Overwrite existing authentication")) {
                return
            }
        }

        # Create auth object for this server
        $authObject = if ($PSCmdlet.ParameterSetName -eq 'ApiKey') {
            [PSCustomObject]@{
                AuthType   = 'ApiKey'
                ApiKey     = $ApiKey
            }
        }
        else {
            [PSCustomObject]@{
                AuthType   = 'Credential'
                Credential = $Credential
            }
        }

        # Add or update this server's auth
        $authData[$ServerName] = $authObject

        try {
            $authData | Export-Clixml -Path $authFilePath -Force
            Write-Information "Authentication for $ServerName saved to $authFilePath" -InformationAction Continue
        }
        catch {
            Write-Error "Failed to save authentication: $_"
        }
    }
}