We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 36763
    • 70 Posts
    Hello,

    I've run in to a bit of a hurdle. I can get this snippet running when using the standard path, document and set up, eg, website.com/index.php?id=19

    However, I am trying to run this from a different location, using custom files from a different path so I can access the ModX db - and do cool stuff. eg, website.com/test/index.php

    I've done minimalist test file example, and I'm a little stuck.
    This is the code I've put in my /test/index.php: (This is not to be confused with the main modX /index.php file. )


    <?php
    session_start();
    $sess_id = session_id();
    
    require_once('../config.core.php');
    require_once MODX_CORE_PATH.'model/modx/modx.class.php';
    $modx = new modX();
    $modx->initialize('web');
    $modx->getService('error', 'error.modError');
    
    // run HybridAuth (add your config params to the array)
    $HybridAuth = $modx->runSnippet('HybridAuth', array('providers' => "Facebook")); 
     
    echo $HybridAuth;
    
    ?> 
     


    So what I'm getting loaded is just a page with "You can authorize on site with: " and a link that says "Facebook" (no pretty icon like last time), and this result in the source code:

    You can authorize on site with: 
    <br/>
    <a href="http://www.website.com/test/index.php?hauth_action=login&provider=Facebook" class="ha-icon facebook" rel="nofollow" title="Facebook">Facebook</a>
    


    When I click on the link, I just get the url linking back to itself, with the parameters growing with more "&hauth_action=login&provider=Facebook" appended to the page url and the link url.

    I'd also updated my FB dev account to test this out too, and set the URL to the /test/ path, but it doesn't want to work either.

    Any thoughts?

    I noticed that in "normal" document that the css code gets put in automatically in the head code.
    Am I missing something else that needs to be triggered, such as the plugin?

    Cheers,
    Steve
      • 36763
      • 70 Posts
      Righto. smiley

      I'm a little less frustrated now. This is for others in a similar boat:



      <?php
      
      error_reporting(E_ALL);
      ini_set('display_errors', '1');
      session_start();
      $sess_id = session_id();
      
      require_once('../config.core.php');
      require_once MODX_CORE_PATH.'model/modx/modx.class.php';
      $modx = new modX();
      $modx->initialize('web');
      $modx->getService('error', 'error.modError'); 
      
      // Shows all the stuff about who you are logged in as etc
      if ($modx->context->key != 'web' && !$modx->user->id) {
              if ($user = $modx->getAuthenticatedUser($modx->context->key)) {
                      $modx->user = $user;
                      $modx->getUser($modx->context->key);
              }
      }
      
      if ($modx->user->isAuthenticated($modx->context->key)) {
              if (!$modx->user->active || $modx->user->Profile->blocked) {
                      $modx->runProcessor('security/logout');
                      //$modx->sendRedirect($modx->makeUrl($modx->getOption('site_start'),'','','full'));
                      $modx->sendRedirect($modx->getOption('ha.startURL'));
                      
              }
      }
      
      // end showing logged in stuff 
      
      if (!empty($_REQUEST['hauth_action']) || !empty($_REQUEST['hauth_start']) || !empty($_REQUEST['hauth_done'])) {
              
              if (!$modx->loadClass('hybridauth', MODX_CORE_PATH . 'components/hybridauth/model/hybridauth/', false, true)) {return;}
              $config = !empty($_SESSION['HybridAuth'][$modx->context->key])
                      ? $_SESSION['HybridAuth'][$modx->context->key]
                      : array();
              $HybridAuth = new HybridAuth($modx, $config);
      
              if (!empty($_REQUEST['hauth_action'])) {
                      switch ($_REQUEST['hauth_action']) {
                              case 'login':
                                      $HybridAuth->Login(@$_REQUEST['provider']);
                                      break;
                              case 'logout':
                                      $HybridAuth->Logout();
                                      break;
                      }
              }
              else {
                      $HybridAuth->processAuth();
              }
      } 
      
      //$css = $modx->getOption('ha.frontend_css');
      //echo $css;
      
       ?>
        <link rel="stylesheet" href="/assets/components/hybridauth/css/web/default.css" type="text/css" />
      <?php
        
      // run HybridAuth (add your config params to the array)
      $HybridAuth = $modx->runSnippet('HybridAuth', array('providers' => "Facebook")); 
       
      echo $HybridAuth;
      
      ?> 
      


      You'll notice that the CSS is hardcoded, as I don't know how (I'm sure someone can assist with it), but it comes out looking like this and not compiled: [[+assetsUrl]]css/web/default.css

      I couldn't get the plugin to execute and load and do what it should, instead because it was so small, I just pasted it in. It was using the "OnHandleRequest" plugin, for System, Default, 10.

      This instead works.

      I did however make at least one change to the main source code:

      /core/components/hybridauth/model/hybridauth/hybridauth.class.php
      Line: 140 (approx)
      
      'base_url' => $this->modx->getOption('ha.startURL'),



      What I was finding is that all it wanted to do was go to the start page each time, and not where it had begun. So I made a new System Setting in the 'web' context called "ha.startURL" with the value of "http://websitename.com/test/".

      So far so good. I'm sure there will be a couple more tweaks out there to make, but wanted to share.

      Just remember the little gotachas. I had one, of putting in the wwws vs not put them in. The session thinks it's a different site, and when you are calling it from Facebook in your Dev account, it must be the same. [ed. note: vrdriver last edited this post 12 years, 11 months ago.]
        • 43957
        • 79 Posts
        I ran into a similar type issue with HybridAuth as I was developing with my modx install in a different folder to web root.

        www.example.com/development/test

        The login link generated by HybridAuth would be www.example.com/development/test/development/test/?hauth_action=login&provider=Google

        Resulting in landing on the error page when trying to log in. Setting the &loginResourceId and &logoutResourceId pages in the call resolved my problem.

        [[!HybridAuth?providers=`Google`
        &loginResourceId=`374`
        &logoutResourceId=`372`
        ]]
          • 36763
          • 70 Posts
          Yeah, I have to do it this way, because I'm outside of the MODX "CMS path". I want the goodness of the CMS and the user permissions, but still need full control and custom files. This is what I LOVE about MODX. smiley

          I'm basically doing something like this
          /root/
               /assets/
               /core/ 
               /test/ 


          Which is making it a little more complex than just storing the resource IDs. I was nearly ready to start using cookies and things for storing URL variables, as my /test/ path is using an index file that must be referenced, and is a unique parameter, but fortunately FB returns back to the path it came from with all of the parameters and values. I was very happy about that discovery. smiley

          So, basically this path index.php?steve=reallycool has the hybridauth code, a user authenticates with FB, and returns to the same page, with only one slightly different parameter: index.php?steve=reallycool#_-_
          And this I can live with. smiley I'm just glad I'm not the only one in the boat.