如何检查用户是否使用Facebook Laravel 5登录

时间:2015-02-15 21:10:00

标签: php facebook laravel-5

我需要检查用户是否已登录,如果他已登录,则使用数据trought app(将数据保存到数据库并获取欢迎消息)这里是我的代码:

 <?php namespace App;
    use Illuminate\Contracts\Auth\Authenticator;
    use Laravel\Socialite\Contracts\Factory as Socialite;
    use App\Repositories\UserRepository;
    use App\UserListener;
    use Illuminate\Auth\Guard;
    use Illuminate\Support\Facades\Auth;

    class AuthenticateUser {
        private $users;
        private $socialite;


        public function __construct(UserRepository $users,  Socialite $socialite) {
            $this ->users = $users;
            $this ->socialite= $socialite;

        }

        public function execute($hasCode){
          if(! $hasCode) return $this -> getAuthorizationFrist();  
         $user = $this -> users -> findOrCreate($this -> getFacebookUser());

              redirect('/login');


        }
        private function getAuthorizationFrist(){
            return $this -> socialite ->driver('facebook')->redirect();
        }

        private function getFacebookUser(){
         return $this->socialite->driver('facebook')->user();


        }

    }
?>

这是控制器:

<?php namespace App\Http\Controllers;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use Laravel\Socialite\Facades\Socialite;
use Illuminate\Http\Request;
use App\AuthenticateUser;


    class AuthController extends Controller{
    public function login(AuthenticateUser $authenticateUser,Request $request){
           return $authenticateUser->execute($request->has('code'));
        }
     }
?>

执行函数将检查“代码”是否存在如果存在我将用户数据导入$ user变量然后重定向到登录,我需要一些示例如何检查该用户是否登录并写入欢迎消息...我使用laravel packetage socialite ... enter code here

此处是路线,但不起作用:

get('face',function(){
    if(Auth::check()) return 'Wlecome,' .Auth::user()->username;

    return 'Hi guest.' . link_to('login', 'login to face');
});

get('login','AuthController@login');

0 个答案:

没有答案