场景
package介绍
安装 && 使用
- composer require “overtrue/laravel-socialite:~2.0”
- 操作很简单就, 具体见github文档
实例
<?php
namespace App\Http\Controllers;
use App\Http\Repositories\AuthRepository;
class AuthController extends Controller
{
protected $repository_auth;
public function __construct(AuthRepository $repository_auth)
{
$this->repository_auth = $repository_auth;
}
public function redirectToProvider()
{
try {
return $this->repository_auth->redirectToProvider();
} catch (\Exception $e) {
flash()->error($e->getMessage());
return redirect('/login');
}
}
public function gitHubCallback()
{
try {
return $this->repository_auth->gitHubCallback();
} catch (\Exception $e) {
flash()->error($e->getMessage());
return redirect('/login');
}
}
}
<?php
namespace App\Http\Repositories;
use App\User;
use Overtrue\LaravelSocialite\Socialite;
class AuthRepository
{
protected $user;
protected $list_legal_oauth = [
'github' => 'GITHUB',
];
public function gitHubCallback()
{
$this->user = Socialite::driver('github')->user();
$verify_status = $this->verifyRegisterForGithub();
$this->dealProviderData($verify_status);
return $this->loginForThird();
}
protected function loginForThird()
{
$this->loginDo();
return $this->loginRedirect();
}
protected function loginRedirect()
{
$redirect_url = request()->get('redirect_url');
$redirect_url = $redirect_url ? $redirect_url : '';
return redirect($redirect_url);
}
protected function loginDo()
{
$email = $this->user->getEmail();
$user = User::where(compact('email'))->first();
\Auth::login($user);
}
protected function dealProviderData($verify_status)
{
switch ($verify_status) {
case 'register_not':
$this->registerUserThenLogin();
break;
case 'register_only':
$this->bindUserFromThird();
break;
case 'register_not_any_more':
$this->bindWhenSomeThingWrong();
break;
case 'register_then_login':
break;
}
}
protected function bindWhenSomeThingWrong()
{
throw new \Exception('该账号已经被绑定');
}
protected function bindUserFromThird()
{
$params = $this->genParamsForBind();
$this->bindDo($params);
}
protected function bindDo($params)
{
$email = $this->user->getEmail();
User::where(compact('email'))->update($params);
flash('谢谢来访, 您的github账号已经绑定到您之前注册Email: ' . $email . ', 如果有疑问请及时联系')->success();
}
protected function genParamsForBind()
{
$provider_name = strtolower(trim($this->user->getProviderName()));
$provider_id = $this->user->getId();
return compact('provider_id', 'provider_name');
}
protected function registerUserThenLogin()
{
$params = $this->genParamsForRegister();
User::create($params);
}
protected function genParamsForRegister()
{
$provider_id = $this->user->getId();
$name = $this->user->getName();
$email= $this->user->getEmail();
$provider_name= strtolower($this->user->getProviderName());
$avatar = $this->user->getAvatar();
$confirmation_token = sha1(str_random(40));
$api_token = str_random(64);
$settings = ['area' => '', 'bio' => ''];
return compact('provider_name', 'provider_id', 'name', 'email', 'avatar', 'confirmation_token', 'api_token', 'settings');
}
protected function verifyRegisterForGithub()
{
$email= $this->user->getEmail();
$provider_name= strtolower(trim($this->user->getProviderName()));
$user_search = User::where(compact('email'))->first();
if (!$user_search) {
return 'register_not';
}
if (!$user_search->provider_name) {
return 'register_only';
}
if ($user_search->provider_name !== $provider_name) {
return 'register_not_any_more';
}
return 'register_then_login';
}
public function redirectToProvider()
{
$this->verifyParams();
return $this->redirectOauth();
}
protected function redirectOauth()
{
$oauth_login = request('oauth_login');
switch ($oauth_login) {
case 'github':
return Socialite::driver('github')->redirect();
break;
default:
}
}
protected function verifyParams()
{
$oauth_login = request('oauth_login');
if (!$oauth_login) {
throw new \Exception('网络故障,请刷新后再试');
}
if (!array_key_exists($oauth_login, $this->list_legal_oauth)) {
$list_legal = implode(',', $this->list_legal_oauth);
throw new \Exception('抱歉,目前仅支持' . $list_legal . '的第三方登陆');
}
}
}