How to change HybridAuth Endpoint URL (HybridAuth internal Callback URL)
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:
- <?php
- class UsersAuthentication extends Controller {
- function hybridauth_endpoint() {
- include "/path/to/framework/application/vendors/hybridauth/index.php";
- }
- function signin_with_hybridauth( $provider ) {
- $config = "/path/to/framework/application/vendors/hybridauth/config.php";
- include "/path/to/framework/application/vendors/hybridauth/Hybrid/Auth.php";
- $hybridauth = new Hybrid_Auth( $config );
- $adapter = $hybridauth->authenticate( $provider );
- /* etc. */
- }
- function signin() {}
- function signup() {}
- /* ... */
- }
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.
- <?php
- class UsersAuthentication extends Controller {
- function hybridauth_endpoint( $action, $provider ) {
- include "/path/to/framework/application/vendors/hybridauth/Hybrid/Auth.php";
- include "/path/to/framework/application/vendors/hybridauth/Hybrid/Endpoint.php";
- $key = 'hauth_' . $action; // either `hauth_start` or `hauth_done`
- $_REQUEST[ $key ] = $provider; // provider will be something like `facebook` or `google`
- Hybrid_Endpoint::process();
- }
- function signin_with_hybridauth( $provider ) {
- $config = "/path/to/framework/application/vendors/hybridauth/config.php";
- include "/path/to/framework/application/vendors/hybridauth/Hybrid/Auth.php";
- // you can more than likely get this URL programmatically in your app,
- // we'll set it manually here just for demonstration purposes
- $base_url = 'http://mywebsite.com/app/userscontroller/hybridauth_endpoint';
- $hybridauth = new Hybrid_Auth( $config );
- // we'll need to tell HybridAuth to use custom endpoint URLs
- $adapter = $hybridauth->authenticate( $provider, array(
- 'login_start' => $base_url . '/start/' . strtolower( $provider ),
- 'login_done' => $base_url . '/done/' . strtolower( $provider ),
- ) );
- /* etc. */
- }
- function signin() {}
- function signup() {}
- /* ... */
- }
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