From c5f2b2909d80834462c54a90926ea28b308cc5e8 Mon Sep 17 00:00:00 2001 From: mathieu Date: Thu, 9 Jul 2015 11:30:10 -0400 Subject: [PATCH] Change facebook login flow. Fix #20 --- application/controllers/ajax.php | 80 ++++++++++++++++------------- application/views/header.php | 8 +-- application/views/modal/login.php | 1 + application/views/modal/sign-up.php | 1 + assets/js/facebook.js | 64 ++++++++++------------- 5 files changed, 77 insertions(+), 77 deletions(-) diff --git a/application/controllers/ajax.php b/application/controllers/ajax.php index 6aa3e84f..511cb9c4 100755 --- a/application/controllers/ajax.php +++ b/application/controllers/ajax.php @@ -53,61 +53,69 @@ function checkEmail() function facebookSignup() { + $result['success'] = false; + if($this->input->post('email')) { - $result = array(); + $email = $this->input->post('email'); $password = "FB_"+$this->input->post('id'); - $name = $this->input->post('name'); + $name = $this->input->post('last_name'); $firstname = $this->input->post('firstname'); $timezone = $this->input->post('timezone'); $country = $this->input->post('country'); - if($this->user->signup($email, $password, $name, $firstname, $timezone, $country)) - { - $this->event->add($this->event->LOGIN_FB); + $emailExists = $this->user->checkUserEmail($email); - $this->load->helper('mcapi'); - $api = new MCAPI('eff18c4c882e5dc9b4c708a733239c82-us9'); - $api->listSubscribe('7f94c4aa71', $email, ''); + if(!$emailExists){ - $this->load->library('email'); - - $config['protocol'] = "smtp"; - $config['smtp_host'] = "smtp.mandrillapp.com"; - $config['smtp_port'] = "587"; - $config['smtp_user'] = "marc@toolwatch.io"; - $config['smtp_pass'] = "pUOMLUusBKdoR604DpcOnQ"; - $config['charset'] = "utf-8"; - $config['mailtype'] = "html"; - $config['newline'] = "\r\n"; + if($this->user->signup($email, $password, $name, $firstname, $timezone, $country)) + { - $this->email->initialize($config); - - $this->email->from('hello@toolwatch.io', 'Toolwatch'); - $this->email->to($email, $name.' '.$firstname); - $this->email->reply_to('hello@toolwatch.io', 'Toolwatch'); + $this->event->add($this->event->SIGN_UP_FB); - $this->email->subject('Welcome to Toolwatch!'); - - $message = $this->load->view('email/signup', '', true); - $this->email->message($message); + $this->load->helper('mcapi'); + $api = new MCAPI('eff18c4c882e5dc9b4c708a733239c82-us9'); + $api->listSubscribe('7f94c4aa71', $email, ''); + + $this->load->library('email'); + + $config['protocol'] = "smtp"; + $config['smtp_host'] = "smtp.mandrillapp.com"; + $config['smtp_port'] = "587"; + $config['smtp_user'] = "marc@toolwatch.io"; + $config['smtp_pass'] = "pUOMLUusBKdoR604DpcOnQ"; + $config['charset'] = "utf-8"; + $config['mailtype'] = "html"; + $config['newline'] = "\r\n"; + + $this->email->initialize($config); + + $this->email->from('hello@toolwatch.io', 'Toolwatch'); + $this->email->to($email, $name.' '.$firstname); + $this->email->reply_to('hello@toolwatch.io', 'Toolwatch'); + + $this->email->subject('Welcome to Toolwatch!'); + + $message = $this->load->view('email/signup', '', true); + $this->email->message($message); + + if($this->email->send()) + { + $result['success'] = "signup"; + $this->user->login($email, $password); + } - if($this->email->send()) - { - $result['success'] = "signup"; - $this->user->login($email, $password); } }else if($this->user->login($email, $password)){ - $this->event->add($this->event->SIGN_UP_FB); + $this->event->add($this->event->LOGIN_FB); - $result['success'] = "signin"; - }else { - $result['success'] = false; + $result['success'] = "signin"; } - echo json_encode($result); } + + echo json_encode($result); } function signup() diff --git a/application/views/header.php b/application/views/header.php index 47048c56..785d5d7e 100755 --- a/application/views/header.php +++ b/application/views/header.php @@ -36,13 +36,13 @@ m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m) })(window,document,'script','//www.google-analytics.com/analytics.js','ga'); - (function(d, s, id) { + (function(d, s, id){ var js, fjs = d.getElementsByTagName(s)[0]; - if (d.getElementById(id)) return; + if (d.getElementById(id)) {return;} js = d.createElement(s); js.id = id; - js.src = "//connect.facebook.net/en_US/sdk.js"; + js.src = "https://connect.facebook.net/en_US/sdk.js#version=v2.2&appId=807383452677000&status=true&cookie=true&xfbml=true"; fjs.parentNode.insertBefore(js, fjs); - }(document, 'script', 'facebook-jssdk')); + }(document, 'script', 'facebook-jssdk')); ga('create', 'UA-59148878-1', 'auto'); ga('send', 'pageview'); diff --git a/application/views/modal/login.php b/application/views/modal/login.php index 4434ef09..b7aa29e7 100755 --- a/application/views/modal/login.php +++ b/application/views/modal/login.php @@ -1,5 +1,6 @@
+
diff --git a/application/views/modal/sign-up.php b/application/views/modal/sign-up.php index c01c3528..1e5a52cd 100755 --- a/application/views/modal/sign-up.php +++ b/application/views/modal/sign-up.php @@ -1,5 +1,6 @@
+
diff --git a/assets/js/facebook.js b/assets/js/facebook.js index a4b43934..f17666a3 100755 --- a/assets/js/facebook.js +++ b/assets/js/facebook.js @@ -1,21 +1,20 @@ // 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(); + sendLoginFb(); } } function fb_login(){ FB.login(function(response) { if (response.status === 'connected') { - // Logged into your app and Facebook. + sendLoginFb(); } else if (response.status === 'not_authorized') { // The person is logged into Facebook, but not your app. } else { @@ -36,15 +35,6 @@ }); } - window.fbAsyncInit = function() { - FB.init({ - appId : '807383452677000', - cookie : true, // enable cookies to allow the server to access - // the session - xfbml : true, // parse social plugins on this page - version : 'v2.2' // use version 2.2 - }); - // 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 @@ -56,36 +46,36 @@ // your app or not. // // These three cases are handled in the callback function. +window.fbAsyncInit = function() { FB.getLoginStatus(function(response) { statusChangeCallback(response); }); - }; +}; - // 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) { +// Here we run a very simple test of the Graph API after login is +// successful. See statusChangeCallback() for when this call is made. +function sendLoginFb() { + FB.api('/me', function(response) { - $.post('/ajax/signup', {email: response.email, name: response.name, firstname: response.first_name, timezone: response.timezone, country: response.country}, function(data) - { - var result = $.parseJSON(data); - if(result.success == "signup") - { - $.post('/sign-up-success/', {ajax: true}, function(data) - { - $('#pageModal .modal-body').html(data); - setTimeout('window.location.replace("/measures/")', 5000); - }); - }else if(result.success == "signin"){ - setTimeout('window.location.replace("/measures/")', 1000); - - } else { - $('.global-error').html('Something went wrong... Try again later.').show(); - } - }); - console.log(response); + $.post('/ajax/facebookSignup', {email: response.email, last_name: response.last_name, firstname: response.first_name, timezone: response.timezone, country: response.country}, function(data) + { + var result = $.parseJSON(data); + if(result.success == "signup") + { + $.post('/sign-up-success/', {ajax: true}, function(data) + { + $('#pageModal .modal-body').html(data); + setTimeout('window.location.replace("/measures/")', 5000); + }); + }else if(result.success == "signin"){ + setTimeout('window.location.replace("/measures/")', 1000); + + } else { + $('#fb_error').html('Something went wrong... Try again later.').show(); + } }); - } \ No newline at end of file + console.log(response); + }); +} \ No newline at end of file