Sample Javascript:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 |
var fb_token = ''; var fb_id = ''; function statusChangeCallback(response) { // Called with the results from FB.getLoginStatus(). console.log('statusChangeCallback'); //console.log(response); // The current login status of the person. if (response.status === 'connected') { // Logged into your webpage and Facebook. //fb_token = response.authResponse.accessToken; //fb_id = response.authResponse.userID; //testAPI(); fb_app.auto_login(); } else { // Not logged into your webpage or we are unable to tell. document.getElementById('status').innerHTML = 'Please log ' + 'into this webpage.'; } } function checkLoginState() { console.log('checkLoginState'); // Called when a person is finished with the Login Button. FB.getLoginStatus(function(response) {// See the onlogin handler statusChangeCallback(response); }); } window.fbAsyncInit = function() { FB.init({ appId : '1116666666', cookie : true, // Enable cookies to allow the server to access the session. xfbml : true, // Parse social plugins on this webpage. version : 'v7.0' // Use this Graph API version for this call. }); }; (function(d, s, id) { // Load the SDK asynchronously 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')); function testAPI() { // Testing Graph API after login. See statusChangeCallback() for when this call is made. console.log('Welcome! Fetching your information.... '); FB.api( '/me?fields=name,email', function(response) { console.log(response); console.log('Successful login for: ' + response.name); } ); } $(document).ready(function(){ fb_app.init(); }); var fb_app = function(){ function init(){ $('#login_fb, #signup_fb').on('click', function(e){ e.preventDefault(); FB.login(function(response) { console.log('FB.login'); console.log(response); checkLoginState(); }, {scope: 'public_profile,email'}); }); //Test in dev //login(); } function auto_login(){ FB.api( '/me?fields=name,email', function(response) { console.log(response); login(response); } ); } function login(data){ var params = data; /* Test data var params = { name: "RRR", email: "email@gmail.com", id: "102200000000000000" } */ $.ajax({ type: 'post', url: front_base + '/member/login_fb_memebr', data: params, dataType: 'json', success: function(res){ if(typeof res.status == 'undefined'){ alert(res); }else if(res.status == 'fail'){ alert(res.message); }else{ //console.log(res); var data = res.data; alert('登入成功'); if(data == 'cart'){ window.location.href = front_base + '/cart/checkout/'; }else{ window.location.href = front_base + '/member/'; } } }, error: function(res){ console.log(res); } }); } return { init: init, auto_login: auto_login } }(); |
PHP Example (Laravel)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
public function login_fb_memebr(Request $req){ $data = $req->input(); //logg($data); $member = MemberM::where('email', $data['email']) ->first(); if($member == false){ //自動註冊 $member = new MemberM; $member->fb_id = $data['id']; $member->email = $data['email']; $member->name = $data['name']; $member->password = Hash::make($data['id']); $member->type = 2; $member->save(); $this->send_reg_email($member->id); }else{ //如果再用goggle登入就被蓋掉,所以每次都要更新 $member->fb_id = $data['id']; $member->password = Hash::make($data['id']); $member->type = 2; $member->save(); } $remember = true; $credentials = [ 'email' => $data['email'], 'password' => $data['id'] ]; //logg($credentials); //logg(Hash::make($password)); $result = $this->guard()->attempt($credentials, $remember); $cart = new Cart; if($cart->item_count() > 0){ return $this->success_response('cart'); }else{ return $this->success_response(true); } } |
Reference: