Private/Invoke-PatPinAuthentication.ps1

function Invoke-PatPinAuthentication {
    <#
    .SYNOPSIS
        Performs PIN-based authentication with Plex.
 
    .DESCRIPTION
        Orchestrates the complete PIN authentication flow:
        1. Generates or retrieves client identifier
        2. Requests a PIN from Plex
        3. Displays the PIN and URL to the user
        4. Waits for user to authorize the PIN
        5. Returns the authentication token
 
    .PARAMETER TimeoutSeconds
        Maximum time to wait for authorization in seconds (default: 300 / 5 minutes).
 
    .PARAMETER Force
        Suppresses the interactive prompt to open the browser. When specified,
        automatically opens the browser. Use for non-interactive automation.
 
    .OUTPUTS
        System.String
        Returns the authentication token if successful
    #>

    [CmdletBinding(SupportsShouldProcess)]
    param (
        [Parameter(Mandatory = $false)]
        [ValidateRange(1, 1800)]
        [int]
        $TimeoutSeconds = 300,

        [Parameter(Mandatory = $false)]
        [switch]
        $Force
    )

    try {
        # Get or generate client identifier
        $clientIdentifier = Get-PatClientIdentifier
        Write-Verbose "Using client identifier: $clientIdentifier"

        # Request PIN
        Write-Verbose "Requesting PIN from Plex"
        $pin = New-PatPin -ClientIdentifier $clientIdentifier

        # Display instructions to user
        $plexLinkUrl = 'https://plex.tv/link'

        Write-Information "`nPlex Authentication" -InformationAction Continue
        Write-Information "===================" -InformationAction Continue
        Write-Information "`nEnter this code at $plexLinkUrl`:" -InformationAction Continue
        Write-Information "`n $($pin.code) (case-insensitive, copied to clipboard)" -InformationAction Continue

        # Copy code to clipboard
        Set-Clipboard -Value $pin.code

        # Open browser if -Force or user confirms
        if ($Force -or $PSCmdlet.ShouldContinue(
            "Open $plexLinkUrl in your browser?",
            'Plex Authentication'
        )) {
            Start-Process $plexLinkUrl
        }

        Write-Information "`nWaiting for authorization..." -InformationAction Continue

        # Wait for authorization
        $token = Wait-PatPinAuthorization -PinId $pin.id `
            -ClientIdentifier $clientIdentifier `
            -TimeoutSeconds $TimeoutSeconds

        if ($token) {
            Write-Information "`nAuthentication successful!" -InformationAction Continue
            return $token
        }
        else {
            throw "Authentication timed out after $TimeoutSeconds seconds. Please try again."
        }
    }
    catch {
        throw "PIN authentication failed: $($_.Exception.Message)"
    }
}