使用Facebook SDK从iOS登录Laravel webapp

时间:2015-02-16 14:48:55

标签: php ios facebook laravel oauth

我有一个使用Laravel的webapp。 我有一些用户在我的webapp中注册。 不想创建帐户的用户可以使用Facebook / Google +登录。

要建立Facebook / Google +连接,我使用了oAuth2连接: oauth-4-laravel

在iOS应用程序上,用户可以使用用户名和密码登录,但他们也可以使用FB / G + SDK登录Facebook / Google +。

我的问题是,如何登录iOS上通过Facebook / Google +连接的webapp用户。

artdarek / oauth-4-laravel需要社交网络提供的“代码”,我不知道如何在iOS上获取它。

这里是想要通过网络浏览器连接到webapp的用户的代码:

 public function loginWithFacebook() {

// get data from input
    $code = Input::get( 'code' );

    // get fb service
    $fb = OAuth::consumer( 'Facebook' );

    // check if code is valid

    // if code is provided get user data and sign in
    if ( !empty( $code ) ) {

        // This was a callback request from facebook, get the token
        $token = $fb->requestAccessToken( $code );

        // Send a request with it
        $result = json_decode( $fb->request( '/me' ), true );

       // $message = 'Your unique facebook user id is: ' . $result['id'] . ' and your name is ' . $result['name'];
        //echo $message. "<br/>";

        //Var_dump
        //display whole array().
       // dd($result);
        if (User::where('fb_id','=', $result['id'])->count() == 0) {
             $user = new User;
            $user->firstname = $result['first_name'];
            $user->lastname = $result['last_name'];
            $user->username = $result['email'];
            $user->email = $result['email'];
            $user->fb_id = $result['id'];
            $user->yearofbirth = substr($result['birthday'],6,9);
            $user->fk_role=3;

            if ($result['gender'] == 'male') {
                $user->sex = 1;
            }
            else{
                $user->sex = 0;
            }


            $user->save();

        }
        else{
            $user = User::where('fb_id','=', $result['id'])->first();

        }

    Auth::login($user);

    Userslog::log('desktop_facebook_login');

    return Redirect::to('/')->with('message', 'Logged in with Facebook');

    }
    // if not ask for permission first
    else {
        // get fb authorization
        $url = $fb->getAuthorizationUri();

        // return to facebook login url
         return Redirect::to( (string)$url );
    }

}

2 个答案:

答案 0 :(得分:1)

我终于找到了关于github上oauth-4-laravel问题的解决方案,

我只需使用iOS AccessToken并使用此令牌将用户连接到webapp。

这里是使用令牌的代码laravel代码;)

use OAuth\OAuth2\Token\StdOAuth2Token;

$token_interface = new StdOAuth2Token(Input::get( 'token' ));       
$network = OAuth::consumer( 'Facebook' );
$network->getStorage()->storeAccessToken('Facebook', $token_interface);
$result = json_decode( $network->request( '/me' ), true );
$fb_id = $result['id'];

要恢复, 最好的方法是;在iOS上登录Facebook / Google +,

当登录成功时,获取AccessToken并发送到webapp.And然后webapp使用您的令牌再次登录用户到Facebook / Google +,最后,用户已经过验证,您可以登录到laravel webapp。

度过愉快的一天。

答案 1 :(得分:0)

我在这里情况相同。

使用您提供的解决方案,您如何阻止其他应用程序发送自己的AccessToken并访问您的Laravel应用程序?

我假设你通过POST应用程序从你的iOS应用程序发送令牌到你的Laravel应用程序的某个端点,因此知道这个端点的所有开发者都可能假装登录你的移动应用程序,不是吗?