I am using ADmad/CakePHP-HybridAuth Plugin for Social auth. For this i followed the doc that given at https://github.com/ADmad/CakePHP-HybridAuth. I did same as it that done on doc but i am facing an Exception :-
You must attach a listener for "HybridAuth.newUser" event which saves new user record and returns an user entity.
Kindly help me to solve this problem.
Example:
UserModel:
<?php
namespace Accounts\Model\Table;
use Accounts\Model\Entity\User;
use Cake\ORM\Query;
use Cake\ORM\RulesChecker;
use Cake\ORM\Table;
use Cake\Validation\Validation;
use Cake\Validation\Validator;
use Cake\Event\Event;
use Cake\Event\EventManager;
/**
* Users Model
*
* @property \Cake\ORM\Association\BelongsTo $Roles
*/
class UsersTable extends Table
{
public function initialize(array $config)
{
parent::initialize($config);
$this->table('users');
$this->displayField('first_name');
$this->primaryKey('id');
$this->addBehavior('Timestamp');
$this->belongsTo('Roles', [
'foreignKey' => 'role_id',
'joinType' => 'LEFT',
'className' => 'Accounts.Roles',
]);
$this->hasOne('Profiles', [
'foreignKey' => 'user_id',
'className' => 'Accounts.Profiles',
'dependent' => true
]);
$this->hasMany('ADmad/HybridAuth.SocialProfiles');
EventManager::instance()->on('HybridAuth.newUser', [$this, 'createUser']);
}
public function createUser(Event $event) {
// Entity representing record in social_profiles table
$profile = $event->data()['profile'];
$user = $this->newEntity(['email' => $profile->email]);
$user = $this->save($user);
if (!$user) {
throw new \RuntimeException('Unable to save new user');
}
return $user;
}
public function validationDefault(Validator $validator)
{
$validator
->integer('id')
->allowEmpty('id', 'create');
return $validator;
}
public function buildRules(RulesChecker $rules)
{
$rules->add($rules->isUnique(['email']));
$rules->add($rules->isUnique(['username']));
$rules->add($rules->existsIn(['role_id'], 'Roles'));
return $rules;
}
public function beforeSave($event, $entity, $options){
if($entity->first_name)
$entity->slug = $this->createSlug($entity->first_name);
}
}
In App Controller :
$this->loadComponent('Auth', [
'authenticate' => [
'Form' => [
'fields' => [
'username' => 'username',
'password' => 'password'
]
],
'ADmad/HybridAuth.HybridAuth'
],
'loginAction' => [
'controller' => 'Users',
'action' => 'login',
'plugin' => 'Accounts'
],
'loginRedirect' => [
'controller' => 'Users',
'action' => 'login',
'plugin' => 'Accounts'
],
]);
In UsersController:
public function login()
{
if( $this->Auth->user()){
$this->redirect(array('action'=>'index', 'controller'=>'Users', 'plugin'=>'Accounts'));
}else{
if ($this->request->is('post') || $this->request->query('provider')) {
$user = $this->Auth->identify();
if ($user) {
$this->Auth->setUser($user);
return $this->redirect($this->Auth->redirectUrl());
}
$this->Flash->error(__('Invalid username or password, try again'));
}
}
}
The error is in this line
$User = $this-> new Entity (['email' => $profile-> email]);
You should be well
$User = $this-> newEntity ([ 'username' => 'valorunico', 'user' => $profile-> email, 'role_id' => 1]);
test to see ...