title: $:/plugins/Gk0Wk/CPL-Repo/background-actions/auto-fetch-auth-state
tags: $:/tags/BackgroundAction
platforms: browser
track-filter: [[$:/temp/CPL-Server/auth-refresh-token]get[text]] [[$:/temp/CPL-Repo/server-type]get[text]] [[$:/temp/CPL-Server/api-base]get[text]]

<$list filter="[[$:/temp/CPL-Repo/server-type]get[text]match[server]]">
<$list filter="[[$:/temp/CPL-Server/api-base]get[text]!is[blank]]">
<$action-sendmessage $message="tm-http-request"
  url={{{ [[$:/temp/CPL-Server/api-base]get[text]addsuffix[/cpl/auth/config]] }}}
  method="GET"
  oncompletion="""<$action-setfield $tiddler='$:/temp/CPL-Server/auth-config-response' text=<<data>> type='application/json'/>
  <$list filter='[<status>compare:number:gteq[200]compare:number:lt[300]]'>
    <$let githubClientId={{{ [<data>jsonget[githubClientId]trim[]!match[null]!match[undefined]] }}}>
      <$list filter='[<githubClientId>!is[blank]]'>
        <$action-setfield $tiddler='$:/temp/CPL-Server/github-client-id' text=<<githubClientId>>/>
      </$list>
      <$list filter='[<githubClientId>is[blank]]'>
        <$action-deletetiddler $tiddler='$:/temp/CPL-Server/github-client-id'/>
      </$list>
    </$let>
  </$list>
  <$list filter='[<status>compare:number:lt[200]] [<status>compare:number:gteq[300]] +[limit[1]]'>
    <$action-deletetiddler $tiddler='$:/temp/CPL-Server/github-client-id'/>
  </$list>"""/>
<$action-sendmessage $message="tm-http-request"
  url={{{ [[$:/temp/CPL-Server/api-base]get[text]addsuffix[/cpl/auth/status]] }}}
  method="GET"
  oncompletion="""<$action-setfield $tiddler='$:/temp/CPL-Server/auth-status-response' text=<<data>> type='application/json'/>
  <$list filter='[<status>compare:number:gteq[200]compare:number:lt[300]] [<data>jsonget[authenticated]match[true]] +[count[]compare:number:eq[2]]'>
    <$action-setfield $tiddler='$:/temp/CPL-Server/user-status' text='authenticated'/>
    <$action-setfield $tiddler='$:/temp/CPL-Server/user' text={{{ [<data>jsonextract[user]else[{}]] }}} type='application/json'/>
    <$action-setfield $tiddler='$:/temp/CPL-Server/is-admin' text={{{ [<data>jsonget[isAdmin]match[true]then[yes]else[no]] }}}/>
  </$list>
  <$list filter='[<status>compare:number:lt[200]] [<status>compare:number:gteq[300]] [<data>jsonget[authenticated]!match[true]] +[limit[1]]'>
    <$action-setfield $tiddler='$:/temp/CPL-Server/user-status' text='anonymous'/>
    <$action-setfield $tiddler='$:/temp/CPL-Server/is-admin' text='no'/>
    <$action-deletetiddler $tiddler='$:/temp/CPL-Server/user'/>
  </$list>"""/>
</$list>
</$list>
