Example #1
0
 /**
  * Autneticat
  *
  * @param $provider
  * @return bool
  */
 public function authenticate($provider)
 {
     $socialUser = $this->social->with($provider)->stateless()->user();
     if (!$socialUser) {
         return false;
     }
     $identity = $this->oauth->findByProviderNameAndId($socialUser->id, $provider);
     if ($identity) {
         $this->oauth->update($identity, ['token' => $socialUser->token]);
         $this->auth->loginUsingId($identity->user_id, true);
         return true;
     }
     $user = $this->user->findByEmail($socialUser->email);
     if (!is_null($user)) {
         $this->oauth->create(['provider_id' => $socialUser->id, 'provider' => $provider, 'user_id' => $user->id, 'token' => $socialUser->token]);
         $this->user->update($user, ['status' => 1]);
         $this->auth->login($user, true);
         return true;
     }
     if (!setting('registration', true)) {
         return false;
     }
     // Just create the user
     $newUser = $this->user->create(['name' => $this->emailToName($socialUser->email), 'email' => $socialUser->email, 'password' => '', 'status' => 1, 'avatar' => $socialUser->avatar]);
     event(new UserCreatedThroughOAuth($newUser));
     $this->oauth->create(['provider_id' => $socialUser->id, 'provider' => $provider, 'user_id' => $newUser->id, 'token' => $socialUser->token]);
     $this->auth->login($newUser, true);
     return true;
 }
 /**
  * Get a driver instance.
  *
  * @param string $driver
  * @return mixed 
  * @static 
  */
 public static function with($driver)
 {
     return \Laravel\Socialite\SocialiteManager::with($driver);
 }
$request = Request::capture();
/*
 * 使用 Symfony Session
 */
$session = new Session();
/*
 * session start
 */
$session->start();
/*
 * 設置 session
 */
$request->setSession($session);
/*
 * Facebook飍數
 */
$config['services.facebook'] = ['client_id' => '900358040081723', 'client_secret' => '06134ad6946069c6457d06688b22c259', 'redirect' => 'http://localhost/test/socialite/index.php/callback'];
/*
 * 將 config, request置入 app
 */
$app['config'] = $config;
$app['request'] = $request;
// 以上為 socalite app所需設定,接下來參考官方文件即可
$socialiteManager = new SocialiteManager($app);
$provider = $socialiteManager->with('facebook')->stateless();
if (strpos($_SERVER['REQUEST_URI'], '/callback') === false) {
    $response = $provider->redirect();
    return $response->send();
}
echo '<pre>';
var_dump((array) $provider->user());