I have project on SF4.3 and i don't use FOSUserBundle. How to setup HWIOAuthBundle? Configuration that i have now:
security.yaml
main:
anonymous: ~
oauth:
resource_owners:
facebook: "/login/check-facebook"
google: "/login/check-google"
#my_custom_provider: "/login/check-custom"
#my_github: "/login/check-github"
login_path: /login
use_forward: false
failure_path: /login
provider: users
oauth_user_provider:
service: my.oauth_aware.user_provider.service
hwi_oauth.yaml
hwi_oauth:
# list of names of the firewalls in which this bundle is active, this setting MUST be set
firewall_names: [main]
# https://github.com/hwi/HWIOAuthBundle/blob/master/Resources/doc/2-configuring_resource_owners.md
resource_owners:
facebook:
type: facebook
client_id: '%env(FB_ID)%'
client_secret: '%env(FB_SECRET)%'
scope: "email"
options:
display: popup
csrf: true
google:
type: google
client_id: '%env(G_ID)%'
client_secret: '%env(G_SECRET)%'
scope: "email"
and in security.yaml
my.oauth_aware.user_provider.service:
class: HWI\Bundle\OAuthBundle\Security\Core\User\FOSUBUserProvider
arguments:
- '@fos_user.user_manager'
- ['@fos_user.user_manager', google: google ]
if i don't use FOSUser for users provider in security.yaml
has to be different, how to configure provider for my User
?
Ok, i made my own provider:
class OAuthUserProvider extends BaseClass {
public $entityManager;
public $userRepository;
public function __construct(
UserManagerInterface $userManager,
array $properties,
UserRepository $userRepository,
EntityManagerInterface $entityManager
) {
parent::__construct($userManager, $properties);
$this->userRepository = $userRepository;
$this->entityManager = $entityManager;
}
/**
* {@inheritdoc}
* @throws \Doctrine\ORM\NonUniqueResultException
*/
public function loadUserByOAuthUserResponse(UserResponseInterface $response)
{
$socialID = $response->getUsername();
$user = $this->userRepository->findByGoogleId($socialID);
$email = $response->getEmail();
//check if the user already has the corresponding social account
if (null === $user) {
//check if the user has a normal account
$user = $this->userRepository->findUserByEmail($email);
if (null === $user || !$user instanceof UserInterface) {
//if the user does not have a normal account, set it up:
$user = new User();
$user->setEmail($email);
$user->setPlainPassword(md5(uniqid('', true)));
$user->setActive(true);
}
//then set its corresponding social id
$service = $response->getResourceOwner()->getName();
switch ($service) {
case 'google':
$user->setGoogleID($socialID);
break;
case 'facebook':
$user->setFacebookID($socialID);
break;
}
$em = $this->entityManager;
$em->persist($user);
$em->flush();
//$this->userManager->updateUser($user);
} else {
//and then login the user
$checker = new UserChecker();
$checker->checkPreAuth($user);
}
return $user;
}
}
in my services.yaml
:
app.provider.oauth:
class: App\Security\Providers\OAuthUserProvider
arguments: ['@fos_user.user_manager',{google: googleID, facebook: facebook}]