Private/Assert-DuneSession.ps1

<#
.SYNOPSIS
Validate that an active Dune session exists and is not expired.
 
.DESCRIPTION
Checks for an existing DuneSession. If none is found, attempts to load a cached session. Throws if no session exists or if the session has expired. For expired social-login sessions with a refresh token, attempts a token refresh.
 
.EXAMPLE
PS> Assert-DuneSession
Throws if no valid session is available.
#>

function Assert-DuneSession {
    [CmdletBinding()]
    param ()

    if (-not $DuneSession) { Load-DuneSession }
    if (-not $DuneSession) {
        throw "You are not authenticated. Please run Connect-Dune."
    }
    if ($DuneSession.ExpiryDate -and (Get-Date) -ge $DuneSession.ExpiryDate) {
        if ($DuneSession.Type -eq 'SocialLogin') {
            if ($DuneSession.Type -eq 'SocialLogin' -and $script.Session.RefreshToken) {
                Refresh-JwtToken
            }
        }
        else {
            throw "Session expired. Please use Connect-Dune to reauthenticate"
        }
    }
}