Sometimes it's desirable to change the HybridAuth endpoint URL to something else than what's provided by default.
For MVC frameworks like CakePHP and CodeIgniter, developers usually drop the hybridauth package inside the public directory. This means the callback or return URL for a provider, (ex. Google) look something like this:

http://mywebsite.com/hybridauth/index.php?hauth.done=Google

However, it's possible to fully customize the HybridAuth Endpoint URL by rewrapping the endpoint. This means your endpoint URL could look something like (example #1):

http://mywebsite.com/app/userscontroller/hybridauth_endpoint/?hauth.done=Google

For the adventurous, it's even possible to completely change the start and done endpoints to something like this (example #2):

http://mywebsite.com/app/userscontroller/hybridauth_endpoint/done/google


Example #1

1. First drop hybridauth package inside the third_party or vendors forder (depending on your frameworks).

2. Then create a new method within your users authentication controlller:

  1. <?php
  2. class UsersAuthentication extends Controller {
  3.  
  4. function hybridauth_endpoint() {
  5. include "/path/to/framework/application/vendors/hybridauth/index.php";
  6. }
  7.  
  8. function signin_with_hybridauth( $provider ) {
  9.  
  10. $config = "/path/to/framework/application/vendors/hybridauth/config.php";
  11. include "/path/to/framework/application/vendors/hybridauth/Hybrid/Auth.php";
  12.  
  13. $hybridauth = new Hybrid_Auth( $config );
  14. $adapter = $hybridauth->authenticate( $provider );
  15.  
  16. /* etc. */
  17. }
  18.  
  19. function signin() {}
  20.  
  21. function signup() {}
  22.  
  23. /* ... */
  24. }

If we assume your website domain is http://mywebsite.com/ and your application url is http://mywebsite.com/app/ then:

HybridAuth Endpoint will be (base_url on configuration):
http://mywebsite.com/app/usersauthentication/hybridauth_endpoint/

And you could provide it as callback url for your external applications. ex:
http://mywebsite.com/app/usersauthentication/hybridauth_endpoint/?hauth.done=Google


Example #2 (advanced)

You should only use this approach if you really know what you are doing. Normally, you don't want to set custom login_start or login_done params when you authenticate. However, this may come handy when you want complete transparency of your endpoints, or cannot change the return/callback URL for a provider (when distributing your code as a plugin, for example).

We'll need to manually adjust the $_REQUEST variable, since this is where HybridAuth looks for the hauth_start and hauth_done keys, which it uses to determine if we are starting the auth process, or we have been redirected back from the service provider. It also tells HybridAuth which provider we are dealing with.

Additionally, we'll need to tell HybridAuth to use custom login_start and login_done params when authenticating, so that HybridAuth knows where to redirect the user.

  1. <?php
  2. class UsersAuthentication extends Controller {
  3.  
  4. function hybridauth_endpoint( $action, $provider ) {
  5.  
  6. include "/path/to/framework/application/vendors/hybridauth/Hybrid/Auth.php";
  7. include "/path/to/framework/application/vendors/hybridauth/Hybrid/Endpoint.php";
  8.  
  9. $key = 'hauth_' . $action; // either `hauth_start` or `hauth_done`
  10.  
  11. $_REQUEST[ $key ] = $provider; // provider will be something like `facebook` or `google`
  12.  
  13. Hybrid_Endpoint::process();
  14. }
  15.  
  16. function signin_with_hybridauth( $provider ) {
  17.  
  18. $config = "/path/to/framework/application/vendors/hybridauth/config.php";
  19. include "/path/to/framework/application/vendors/hybridauth/Hybrid/Auth.php";
  20.  
  21. // you can more than likely get this URL programmatically in your app,
  22. // we'll set it manually here just for demonstration purposes
  23. $base_url = 'http://mywebsite.com/app/userscontroller/hybridauth_endpoint';
  24.  
  25. $hybridauth = new Hybrid_Auth( $config );
  26.  
  27. // we'll need to tell HybridAuth to use custom endpoint URLs
  28. $adapter = $hybridauth->authenticate( $provider, array(
  29. 'login_start' => $base_url . '/start/' . strtolower( $provider ),
  30. 'login_done' => $base_url . '/done/' . strtolower( $provider ),
  31. ) );
  32.  
  33. /* etc. */
  34. }
  35.  
  36. function signin() {}
  37.  
  38. function signup() {}
  39.  
  40. /* ... */
  41. }

If we assume your website domain is http://mywebsite.com/ and your application url is http://mywebsite.com/app/ then:

HybridAuth Endpoint will be (base_url on configuration):
http://mywebsite.com/app/usersauthentication/hybridauth_endpoint/

HybridAuth will redirect users logging in via Google to (this will load the Hybrid_Endpoint and start the auth process):
http://mywebsite.com/app/usersauthentication/hybridauth_endpoint/start/google

And you could provide it as callback url for your external applications. ex:
http://mywebsite.com/app/usersauthentication/hybridauth_endpoint/done/google