2015-03-02 18 views
6

Tôi đang cố gắng triển khai gói Socialite vào dự án của mình. Đây là mã của tôi hoạt động tốt. Tuy nhiên nó có mục trùng lặp trong cơ sở dữ liệu nếu người dùng đăng xuất cố gắng đăng nhập lại. Ngoài ra tôi không thể lấy tên công khai của người dùng với $ userData-> name. Nó trả về null.Laravel Socialite Google Đăng ký và Đăng nhập

Đây là bộ điều khiển của tôi;

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

Kho lưu trữ người dùng của tôi;

namespace App\Repositories; 
use App\User; 


class UserRepository { 


public function findByUsernameOrCreate($userData) 
{ 

    return User::firstOrCreate([ 
     'username' => $userData->name, 
     'email' => $userData->email, 

    ]); 

} 
} 

Xác thực lớp người dùng;

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

} 



} 

Trả lời

1

tôi đã có thể làm việc này bằng cách sử dụng mã rất giống với những gì bạn có ở đây, ngoại trừ tôi đã sửa đổi phương pháp findByUsernameOrCreate trong UserRepository của bạn. Tôi tạo ra một phương pháp syncUserDetails trông như thế này:

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

Nó sẽ kiểm tra nếu có một người sử dụng đã có trong db với một email phù hợp:

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

và nếu như vậy, sau đó phù hợp với nó lên với dữ liệu mới và trả về người dùng.

Hy vọng điều này sẽ hữu ích!

Các vấn đề liên quan