2015-03-02 117 views
6

我正在嘗試將Socialite包實現到我的項目中。這是我的代碼,它工作正常。但是,如果用戶註銷嘗試重新登錄,它會重複數據庫中的條目。另外,我無法使用$ userData-> name獲取用戶的公共名稱。它返回null。Laravel Socialite Google註冊並登錄

這是我的控制器;

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'); 
} 

我的用戶存儲庫;

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(); 

} 



} 

回答

1

我已經能夠通過使用非常相似的代碼來得到這個工作你有什麼在這裏,但我已經修改了findByUsernameOrCreate方法在UserRepository。我創建了一個syncUserDetails方法,看起來像這樣:

public function syncUserDetails($userData) 
{ 
    if ($user = $this->userRecordExists($userData)) 
    { 
     $user->token = $userData->token; 
     $user->google_id = $userData->id; 
     $user->name = $userData->name; 
     $user->avatar = $userData->avatar; 
     $user->first_name = $userData->user['name']['givenName']; 
     $user->last_name = $userData->user['name']['familyName']; 
     $user->save(); 

     return $user; 
    } 

    return $this->user->firstOrCreate([ 
     'email'  => $userData->email, 
     'token'  => $userData->token, 
     'google_id' => $userData->id, 
     'name'  => $userData->name, 
     'avatar'  => $userData->avatar, 
     'first_name' => $userData->user['name']['givenName'], 
     'last_name' => $userData->user['name']['familyName'] 
    ]); 
} 

它檢查是否有用戶已經與匹配的電子郵件分貝:

private function userRecordExists($userData) 
{ 
    return $this->user->where('email', $userData->email)->first(); 
} 

如果是這樣,則與匹配起來新數據並返回用戶。

希望這會有所幫助!