Facebook SDKを使用してのFacebookログインはできたのですが、
一度ブラウザを閉じてからサイトアクセスすると
ログアウト状態になり、再度ログインボタンを押さなければなりません。
その時はパスワードの入力は省けているのですが、
ログインボタンを押さずにアクセスしたページで
ログイン認証済みかを確認するにはどうすればいいでしょうか。
いろいろ調べてみてJavascriptで「userID」を取得する方法は分かったのですが、
PHPで同じようにする方法が分からず困ってます。
参考にしたのは以下のサイトです。
http://c-geru.com/as_blind_side/2017/04/facebookapiphp.html
このページ内の図解にあり「member.php」に直接アクセスして、
既にFacebookログイン中であれば、そのままサイトにログイン状態になるようにしたいのです。
どうかよろしくお願いします。
<補足>
ただ、この仕様がセキュリティー上良くないことなのか分かりません。
その点でも助言等頂けると助かります。
<追記 2018/4/24>
ご返信ありがとうございます。
質問だけで詳細を記載していなかったので、追記します。
サイトには独自の会員登録があります。(PHP + MysQL)
それとは別にFacebookログインを追加で実装したいんです。
現在は、SDKを使用して上記の状態です。
初めてFacebookログインしたときは、フォームに移動し、
会員登録に必要な項目を入力して登録完了になります。
その後は、facebookログインボタンを押すと、ログイン状態になります。
ただ「食べログ」のように、すでにFacebookにログインした状態であれば、
サイトにアクセスした時にサイト内でログインした状態に自動で切り替わるようにしたいのです。
一応Javascriptでのやり方は、どこでか忘れてしまったのですが、
以下の内容で「UserID」の取得は出来ました。
// This is called with the results from from FB.getLoginStatus(). function statusChangeCallback(response) { console.log('statusChangeCallback'); console.log(response); // The response object is returned with a status field that lets the // app know the current login status of the person. // Full docs on the response object can be found in the documentation // for FB.getLoginStatus(). if (response.status === 'connected') { // Logged into your app and Facebook. testAPI(); FB.getLoginStatus(function(responsezz) { console.log(responsezz); if (response.status === 'connected') { // FBにログインしてて,このユーザはアプリ認証もとってる. //ユーザーIDを取得accessToken alert(responsezz.authResponse.userID); var fbUid = responsezz.authResponse.userID; } else if (response.status === 'not_authorized') { // FBにログインはしてるけど,このアプリの認証はない. } else { // FBにログインしてない } }); } else { // The person is not logged into your app or we are unable to tell. document.getElementById('status').innerHTML = 'Please log ' + 'into this app.'; } } // This function is called when someone finishes with the Login // Button. See the onlogin handler attached to it in the sample // code below. function checkLoginState() { FB.getLoginStatus(function(response) { statusChangeCallback(response); }); } window.fbAsyncInit = function() { FB.init({ appId : '●●●●●●●●●●●●●●●●●●', cookie : true, // enable cookies to allow the server to access // the session xfbml : true, // parse social plugins on this page version : 'v2.12' // use graph api version 2.8 }); // Now that we've initialized the JavaScript SDK, we call // FB.getLoginStatus(). This function gets the state of the // person visiting this page and can return one of three states to // the callback you provide. They can be: // // 1. Logged into your app ('connected') // 2. Logged into Facebook, but not your app ('not_authorized') // 3. Not logged into Facebook and can't tell if they are logged into // your app or not. // // These three cases are handled in the callback function. FB.getLoginStatus(function(response) { statusChangeCallback(response); }); }; // Load the SDK asynchronously (function(d, s, id) { var js, fjs = d.getElementsByTagName(s)[0]; if (d.getElementById(id)) return; js = d.createElement(s); js.id = id; js.src = "https://connect.facebook.net/en_US/sdk.js"; fjs.parentNode.insertBefore(js, fjs); }(document, 'script', 'facebook-jssdk')); // Here we run a very simple test of the Graph API after login is // successful. See statusChangeCallback() for when this call is made. function testAPI() { console.log('Welcome! Fetching your information.... '); FB.api('/me', function(response) { console.log('Successful login for: ' + response.name); document.getElementById('status').innerHTML = 'Thanks for logging in, ' + response.name + '!'; }); } </script>
これで気になったのが、「appId」がそのままソースに出ているのですが、
これはセキュリティー上問題ないのでしょうか?
(他のサイトでも見かけたので、気にしすぎかもしれませんが・・・・)
あと、Javascriptで「UserID」を取得して、この内容をPHPに移して会員認証をしたいのですが、
自分がJavascriptをまだよくわかっていないので、
「$.ajax」やJSONを使うのは分かっても、そこからデータを飛ばすのが、
どうしても出来ません。
質問内容がズレてきているかもしれませんが、
最初に「PHPで同じようにする方法が分からず困ってます」と書きましたが、
それは前述の理由と、自分がPHPならまだ分かるという理由です。
JavaScript SDKでも構いません。
長文になり申し訳ありませんが、
何卒よろしくお願いします。
回答1件
あなたの回答
tips
プレビュー