title: Split OAuth php login in 2 functions
body: <p>I am using an OAuth plugin in my php application. All works well when I use all the code in one function. But I want split the OAuth call, and the OAuth token treatment. I try to create a global var for the provider object, but it doesn't work. I always have an error with the $provider object.
  This is the function I have for the moment :</p>
  
  <pre><code>function login(){
          $provider = new Stevenmaguire\OAuth2\Client\Provider\Microsoft([
          'clientId'          =&gt; 'myclientid',
          'clientSecret'      =&gt; 'mysecret',
          'redirectUri'       =&gt; 'https://mywebsite'
          //'redirectUri'       =&gt; 'https://mywebsite/loginMicrosoft' //The url where I want use the token
      ]);
  
      if (!isset($_GET['code'])) {
  
          $authUrl = $provider-&gt;getAuthorizationUrl();
          $this-&gt;Session-&gt;write('oauth2state', $provider-&gt;getState());
  
      // This is the part I want put in the function loginMicrosoft. All else if and else
      } elseif (empty($_GET['state']) || ($_GET['state'] !== $this-&gt;Session-&gt;read('oauth2state'))) {
  
          $this-&gt;Session-&gt;destroy();
          exit('Invalid state');
  
      } else {
          // Try to get an access token (using the authorization code grant)
          $token = $provider-&gt;getAccessToken('authorization_code', [
              'code' =&gt; $_GET['code']
          ]);
  
          // Use this to interact with an API on the users behalf
          $this-&gt;log( $token-&gt;getToken());
      }
  }
  //This is the function where I want put the elseif and else treatment
  function loginMicrosoft (){
  }
  </code></pre>
  
  <p>Thanks for your help.</p>
  
  <p><strong>EDIT</strong> : I also try to put my provider object in session, but I have the message "Call to a member function getAccessToken() on a non object"</p>
  
  <p><strong>EDIT 2</strong> : After unsuccessfully tried, I keep all the OAuth code in the same function</p>