2017-09-08 12 views
0

私のアプリにFacebookのオプションログインを統合したい。私は自分のデータベースに既に保存されているユーザープロファイルを持っています。現在のプロフィールをFacebookアカウントにリンクすると、私は自分のサーバーに何を保存すればよいですか?Facebookを使用して自分のアプリにログインする

私がやっていることは、FacebookのユーザーのIDをデータベースのユーザーのプロファイルに保存することだけです。そうすれば、誰かがFacebookにログインしたときにFacebookのユーザーIDを取得し、そのプロフィールのデータベースと照合して取得することができます。私が保管すべき他に何かありますか?

ありがとうございます!

+0

あなたはユーザ名、プロフィール写真など – luk2302

+0

を表示したい場合は、no、単にユーザID格納し、あなたのユースケースに完全に依存 - あなたはより多くを必要とする場合以上に。 IDはユーザーを識別するのに十分です。 – luschn

+0

ありがとうございました!はい、メールアドレスを取得する可能性もあります。 – Tometoyou

答えて

0

はい、Facebookのログインコールバックからメールと名前を保存できます。

public function fb_login_back() 
{ 
    $this->load->library('Fb_login'); 

    $info=$this->fb_login->login_callback();   

    if(is_array($info) && !empty($info) && isset($info["email"]) && isset($info["name"])) 
    { 


     if(!$this->basic->is_exist("users",array("email"=>$info["email"]))) 
     { 
      $insert_data=array 
      (
       "email"=>$info["email"], 
       "name"=>$info["name"], 
       "user_type"=>"Member", 
       "status"=>"1", 
       "add_date"=>date("Y-m-d H:i:s"), 
       "package_id"=>$package_id, 
       "expired_date"=>$expiry_date, 
       "activation_code"=>"", 
       "deleted"=>"0" 
      ); 
      $this->basic->insert_data("users",$insert_data); 
     } 


     $table = 'users'; 
     $where['where'] = array('email' => $info["email"], "deleted" => "0","status"=>"1"); 

     $info = $this->basic->get_data($table, $where, $select = '', $join = '', $limit = '', $start = '', $order_by = '', $group_by = '', $num_rows = 1); 


     $count = $info['extra_index']['num_rows']; 

     if ($count == 0) 
     { 
      $this->session->set_flashdata('login_msg', $this->lang->line("invalid email or password")); 
      redirect("home/login_page"); 
     } 
     else 
     { 
      $username = $info[0]['name']; 
      $user_type = $info[0]['user_type']; 
      $user_id = $info[0]['id']; 

      $this->session->set_userdata('logged_in', 1); 
      $this->session->set_userdata('username', $username); 
      $this->session->set_userdata('user_type', $user_type); 
      $this->session->set_userdata('user_id', $user_id); 
      $this->session->set_userdata('download_id', time()); 
      $this->session->set_userdata('expiry_date',$info[0]['expired_date']); 

      // for getting usable facebook api 

      $this->basic->update_data("users",array("id"=>$user_id),array("last_login_at"=>date("Y-m-d H:i:s"))); 

      if ($this->session->userdata('logged_in') == 1 && $this->session->userdata('user_type') == 'Admin') 
      { 
       redirect('facebook_ex_dashboard/index', 'location'); 
      } 
      if ($this->session->userdata('logged_in') == 1 && $this->session->userdata('user_type') == 'Member') 
      {  
       redirect('facebook_ex_dashboard/index', 'location'); 
      } 
     }    
    } 
} 

その

パブリック関数login_callback(){ のsession_start()のようなライブラリ関数。

$fb = new Facebook\Facebook([ 
     'app_id' => $this->app_id, // Replace {app-id} with your app id 
     'app_secret' => $this->app_secret, 
     'default_graph_version' => 'v2.2', 
    ]); 

    $user=array(); 

    $helper = $fb->getRedirectLoginHelper(); 
     try { 
      $accessToken = $helper->getAccessToken(); 
      $response = $fb->get('/me?fields=id,name,email', $accessToken); 
      $user = $response->getGraphUser()->asArray(); 
     } catch(Facebook\Exceptions\FacebookResponseException $e) { 

     return $user; 

     } catch(Facebook\Exceptions\FacebookSDKException $e) { 
      $user['status']="0"; 
      $user['message']= $e->getMessage(); 
      return $user; 
     }    

return $user;  

}