3
votes

I want to write a login feature via social networks.
if the user is not logged it persists it into the database, if the user exists, log the user in. What should I write into my provider? Docs state :

The bundle needs a service that is able to load users based on the user response of the oauth endpoint. If you have a custom service it should implement the interface: HWI\Bundle\OAuthBundle\Security\Core\User\OAuthAwareUserProviderInterface.

So this is what I wrote and then got stuck

<?php
namespace ng\MyBundle\Controller\Listeners;

use HWI\Bundle\OAuthBundle\Security\Core\User\OAuthAwareUserProviderInterface;

class OAuthUserProvider implements OAuthAwareUserProviderInterface
{

}

Can you tell me what are the methods That I should use ?
Can anybody give me a provider example not using FOSuserBundle ?
Thanks

1
I don't know if you are using FOS or not, but this gist has a pretty example neverthelessstevenll
That's my problem I'm not using FOSuserBundle .. I persist users manuallyAysennoussi
The interface has only one method, look at the gist and you can build you own implementationstevenll
I can't see where they used OAuthAwareUserProviderInterface. and what is the method for the interface ?Aysennoussi
I found the function it's public function loadUserByOAuthUserResponse(UserResponseInterface $response);Aysennoussi

1 Answers

3
votes

If you open the OAuthAwareUserProviderInterface you can see it has only one method :

/**
 * Loads the user by a given UserResponseInterface object.
 *
 * @param UserResponseInterface $response
 *
 * @return UserInterface
 *
 * @throws UsernameNotFoundException if the user is not found
 */
public function loadUserByOAuthUserResponse(UserResponseInterface $response);

Below there is an example on how to implement it, ofcourse in your case, you should call your entity managers, and access users the way you have designed it.

/**
 * {@inheritdoc}
 */
 public function loadUserByOAuthUserResponse(UserResponseInterface $response)
 {
   $username = $response->getUsername();
   $user = $this->userManager->findUserBy(array($this->getProperty($response) => $username));
   //when the user is registrating
   if (null === $user) {
       $service = $response->getResourceOwner()->getName();
       $setter = 'set'.ucfirst($service);
       $setter_id = $setter.'Id';
       $setter_token = $setter.'AccessToken';
       // create new user here
       $user = $this->userManager->createUser();
       $user->$setter_id($username);
       $user->$setter_token($response->getAccessToken());
       //I have set all requested data with the user's username
       //modify here with relevant data
       $user->setUsername($username);
       $user->setEmail($username);
       $user->setPassword($username);
       $user->setEnabled(true);
       $this->userManager->updateUser($user);
       return $user;
    }

    //if user exists - go with the HWIOAuth way
    $user = parent::loadUserByOAuthUserResponse($response);

    $serviceName = $response->getResourceOwner()->getName();
    $setter = 'set' . ucfirst($serviceName) . 'AccessToken';

    //update access token
    $user->$setter($response->getAccessToken());

    return $user;
}