Private/Load-DuneSession.ps1

<#
.SYNOPSIS
Load a cached Dune session from disk.
 
.DESCRIPTION
Imports the session from `~/.dune/session.clixml` if the file exists. For credential-based sessions, validates the session by calling Get-DuneTenant. For expired social-login sessions, attempts a token refresh.
 
.EXAMPLE
PS> Load-DuneSession
Loads and validates the cached session if available.
#>

function Load-DuneSession {
    $path = "$HOME\.dune\session.clixml"
    if (Test-Path $path) {
        $script:DuneSession = Import-CliXml -Path $path
        Write-Verbose -Verbose ("Loading cached DuneSession" -f (Get-Item $path).CreationTime.ToShortDateString())
        if ($DuneSession.Type -eq 'Credential') {
            try {
                $null = Get-DuneTenant
            }
            catch {
                Remove-Variable -Name DuneSession -Force
                Write-Warning "Cached session is expired. Please use Connect-Dune to reauthenticate"
            }
        }
        else {
            if ($DuneSession.ExpiryDate -and (Get-Date) -ge $DuneSession.ExpiryDate) {
                if ($DuneSession.Type -eq 'SocialLogin') { Refresh-JwtToken }
            }
        }
    }
}