From a64e36fcf7dc2b5d7d04a47e39d73e0e91b26222 Mon Sep 17 00:00:00 2001 From: mathieu Date: Mon, 25 May 2015 07:51:06 -0400 Subject: [PATCH] Buisiness logic for Facebook login. Refs #20 --- application/controllers/ajax.php | 54 ++++++++++++++++++++++++++++++++ assets/js/facebook.js | 22 +++++++++++-- 2 files changed, 73 insertions(+), 3 deletions(-) diff --git a/application/controllers/ajax.php b/application/controllers/ajax.php index a6d8de0b..ce303da0 100644 --- a/application/controllers/ajax.php +++ b/application/controllers/ajax.php @@ -48,6 +48,60 @@ function checkEmail() echo json_encode($result); } } + + function facebookSignup() + { + if($this->input->post('email')) + { + $result = array(); + + $password = "FB_"+$this->input->post('id'); + $name = $this->input->post('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->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); + } + }else if($this->user->login($email, $password)){ + $result['success'] = "signin"; + }else { + $result['success'] = false; + } + echo json_encode($result); + } + } function signup() { diff --git a/assets/js/facebook.js b/assets/js/facebook.js index c9306d43..6cc7e101 100644 --- a/assets/js/facebook.js +++ b/assets/js/facebook.js @@ -53,8 +53,24 @@ 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 + '!'; + + $.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); }); } \ No newline at end of file