I'm trying to implement Socialite package into my project. Here is my code which works fine. However it does duplicate entry in the database if the user logs out tries to log back in. Also I can't get the user's public name with $userData->name. It returns null.
Here is my controller;
namespace App\Http\Controllers;
use App\AuthenticateUser;
use App\AuthenticateUserListener;
use App\Http\Requests;
use Illuminate\Http\Request;
class SocialLoginsController extends Controller implements
AuthenticateUserListener
{
/**
* @param AuthenticateUser $authenticateUser
* @param Request $request
* @return \Symfony\Component\HttpFoundation\RedirectResponse
*/
public function socialLogin(AuthenticateUser $authenticateUser, Request $request)
{
$hasCode = $request->has('code');
return $authenticateUser->execute($hasCode, $this);
}
/**
* When a user has successfully been logged in...
*
* @param $user
* @return \Illuminate\Routing\Redirector
*/
public function userHasLoggedIn($user)
{
return redirect('student');
}
My user repository;
namespace App\Repositories;
use App\User;
class UserRepository {
public function findByUsernameOrCreate($userData)
{
return User::firstOrCreate([
'username' => $userData->name,
'email' => $userData->email,
]);
}
}
Authenticate User Class;
<?php namespace App;
use Illuminate\Contracts\Auth\Guard;
use App\Repositories\UserRepository;
use Laravel\Socialite\Contracts\Factory as Socialite;
class AuthenticateUser {
/**
* @var UserRepository
*/
private $users;
/**
* @var Socialite
*/
private $socialite;
/**
* @var Guard
*/
private $auth;
/**
* @param UserRepository $users
* @param Socialite $socialite
* @param Guard $auth
*/
public function __construct(UserRepository $users, Socialite $socialite, Guard $auth)
{
$this->users = $users;
$this->socialite = $socialite;
$this->auth = $auth;
}
/**
* @param boolean $hasCode
* @param AuthenticateUserListener $listener
* @return \Symfony\Component\HttpFoundation\RedirectResponse
*/
public function execute($hasCode, AuthenticateUserListener $listener)
{
if ( ! $hasCode) return $this->getAuthorizationFirst();
elseif($hasCode) {
$user = $this->users->findByUsernameOrCreate($this->getGoogleUser());
$this->auth->login($user, true);
return $listener->userHasLoggedIn($user);
}
}
/**
* @return \Symfony\Component\HttpFoundation\RedirectResponse
*/
private function getAuthorizationFirst()
{
return $this->socialite->driver('google')->redirect();
}
/**
* @return \Laravel\Socialite\Contracts\User
*/
private function getGoogleUser()
{
return $this->socialite->driver('google')->user();
}
}