Skip to content

Commit

Permalink
Change facebook login flow. Fix #20
Browse files Browse the repository at this point in the history
  • Loading branch information
MathieuNls committed Jul 9, 2015
1 parent 9db2a33 commit c5f2b29
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 77 deletions.
80 changes: 44 additions & 36 deletions application/controllers/ajax.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'] = "[email protected]";
$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('[email protected]', 'Toolwatch');
$this->email->to($email, $name.' '.$firstname);
$this->email->reply_to('[email protected]', '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'] = "[email protected]";
$config['smtp_pass'] = "pUOMLUusBKdoR604DpcOnQ";
$config['charset'] = "utf-8";
$config['mailtype'] = "html";
$config['newline'] = "\r\n";

$this->email->initialize($config);

$this->email->from('[email protected]', 'Toolwatch');
$this->email->to($email, $name.' '.$firstname);
$this->email->reply_to('[email protected]', '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()
Expand Down
8 changes: 4 additions & 4 deletions application/views/header.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
1 change: 1 addition & 0 deletions application/views/modal/login.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<div class="form-group">
<center><button onclick="fb_login();" class="btn btn-primary btn-lg btn-spinner btn-full">Log in with Facebook<i class="fa fa-spinner fa-pulse"></i></button></center>
<span id="fb_error" class="signup-error login-error"></span>
</div>

<form method="post" name="login">
Expand Down
1 change: 1 addition & 0 deletions application/views/modal/sign-up.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<div class="form-group">
<center><button onclick="fb_login();" class="btn btn-primary btn-lg btn-spinner btn-full">Sign up with Facebook<i class="fa fa-spinner fa-pulse"></i></button></center>
<span id="fb_error" class="signup-error login-error"></span>
</div>
<form method="post" name="signup">
<div class="form-group">
Expand Down
64 changes: 27 additions & 37 deletions assets/js/facebook.js
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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
Expand All @@ -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();
}
});
}
console.log(response);
});
}

0 comments on commit c5f2b29

Please sign in to comment.