OAuth2 client module¶
Note
This part is accessible from the webtools, so if you created your project with the -a option or with the create-project command. The OAuth module is not installed by default. It uses HybridAuth library.
Installation¶
In the root of your project:
composer require phpmv/ubiquity-oauth
Note
It is also possible to add the ubiquity-oauth dependency using the Composer part of the administration module.

OAuth configuration¶
Global configuration¶

Click on the Global configuration button, and modify the callback URL, which corresponds to the local callback url after a successful connection.

OAuth controller¶
Click on the Create Oauth controller button and assign to the route the value previously given to the callback:

Validate and reset the router cache:

Providers¶
Note
For an OAuth authentication, it is necessary to create an application at the provider beforehand, and to take note of the keys of the application (id and secret).
Click on the Add provider button and select Google:

Check the connection by clicking on the Check button:

Post Login Information:

OAuthController customization¶
The controller created is the following:
namespace controllers;
use Hybridauth\Adapter\AdapterInterface;
/**
* Controller OAuthTest
*/
class OAuthTest extends \Ubiquity\controllers\auth\AbstractOAuthController{
public function index(){
}
/**
* @get("oauth/{name}")
*/
public function _oauth(string $name):void {
parent::_oauth($name);
}
protected function onConnect(string $name,AdapterInterface $provider){
//TODO
}
}
- The _oauth method corresponds to the callback url
- The onConnect method is triggered on connection and can be overridden.
Example :
- Possible retrieval of an associated user in the database
- or creation of a new user
- Adding the logged-in user and redirection
protected function onConnect(string $name, AdapterInterface $provider) {
$userProfile = $provider->getUserProfile();
$key = md5($name . $userProfile->identifier);
$user = DAO::getOne(User::class, 'oauth= ?', false, [
$key
]);
if (! isset($user)) {
$user = new User();
$user->setOauth($key);
$user->setLogin($userProfile->displayName);
DAO::save($user);
}
USession::set('activeUser', $user);
\header('location:/');
}