Google login using cakephp












0














I am trying to setup google login theough cakephp, I can get to the authentication screen but when it comes back to my webpage, I recieve the alert Invalid user, which means something wrong in the username handover.



Can you someone help me troubleshoot it more? how can I test I am getting all the user name, email and photo from google?



Login.php:



<script src="https://apis.google.com/js/api:client.js"></script>
<script>
var googleUser = {};
var startApp = function() {
gapi.load('auth2', function(){
// Retrieve the singleton for the GoogleAuth library and set up the client.
auth2 = gapi.auth2.init({
client_id: 'xxxxxxx.apps.googleusercontent.com',
cookiepolicy: 'single_host_origin',
// Request scopes in addition to 'profile' and 'email'
scope: "https://www.googleapis.com/auth/userinfo.email https://www.googleapis.com/auth/plus.me"
});
attachSignin(document.getElementById('googleLoginBtn'));//id of google login button
});
};

function attachSignin(element) { // call this function on your login button

auth2.attachClickHandler(element, {},
function(googleUser) {
setUserDetailsForGoogle(googleUser);
}, function(error) {
//alert(JSON.stringify(error, undefined, 2));
});
}
function setUserDetailsForGoogle(userData){

var email=userData.getBasicProfile().getEmail();
var socialid=userData.getBasicProfile().getId();
var name=userData.getBasicProfile().getName();
var gender=1;//userData.getBasicProfile().getGender();

$.ajax({
type:'Post',
data:{email:email,socialid:socialid}, // add the parameters you want to send to save
url:'users/googleLogin',
success:function(res){

var res = $.trim(res);
if(res=="true")
{
location.reload();
}
else if(res=="0")
{
alert("Not valid user!!");

}
else if(res=="error")
{
alert("Error in controller!!");
}

}
,
error:function(){
alert("Error in controller!!");
}
});
}
</script>


Users Controller



/*function used to login */
public function googleLogin()
{
$this->loadModel("User");

if ($this->request->is('Ajax'))
{
$data=$_POST;
$userInfo=$this->User->find('first',array('conditions' =>array('User.email'=>trim($data['email']))));
if (!empty($userInfo)) // user exist
{
$userStatus=trim($userInfo['User']['status']);
if($userStatus==1)// code to check if user is in active state
{
$userID=trim($userInfo['User']['id']);
$userSocialId=trim($data['socialid']);
$this->User->updateAll(array('google_id' =>"'".$userSocialId."'"),array('id'=>$userID));
/* write parameter in session to login */
$this->Session->write('User.email', trim($userInfo['User']['email']));
$login_user = $this->Auth->login();


echo "true";
}
else{
echo "0";//status inactive
}
}
else // new signup-
{
//$userData['User']['first_name']=trim($data['first_name']);
//$userData['User']['last_name']=trim($data['last_name']);
$userData['User']['email']=trim($data['email']);
$userData['User']['status']=1;
$userData['User']['google_id']=trim($data['socialid']);

$userData['User']['password']=md5($userPass);
$this->User->save($userData);
$userID=$this->User->getLastInsertId();
$userData['User']['id']=$userID;
$login_user = $this->Auth->login();


echo "true";
}
}
else{
echo "error";
}
die;
}
}









share|improve this question
























  • Maybe You should try github.com/kicaj/social
    – kicaj
    Nov 15 at 8:24
















0














I am trying to setup google login theough cakephp, I can get to the authentication screen but when it comes back to my webpage, I recieve the alert Invalid user, which means something wrong in the username handover.



Can you someone help me troubleshoot it more? how can I test I am getting all the user name, email and photo from google?



Login.php:



<script src="https://apis.google.com/js/api:client.js"></script>
<script>
var googleUser = {};
var startApp = function() {
gapi.load('auth2', function(){
// Retrieve the singleton for the GoogleAuth library and set up the client.
auth2 = gapi.auth2.init({
client_id: 'xxxxxxx.apps.googleusercontent.com',
cookiepolicy: 'single_host_origin',
// Request scopes in addition to 'profile' and 'email'
scope: "https://www.googleapis.com/auth/userinfo.email https://www.googleapis.com/auth/plus.me"
});
attachSignin(document.getElementById('googleLoginBtn'));//id of google login button
});
};

function attachSignin(element) { // call this function on your login button

auth2.attachClickHandler(element, {},
function(googleUser) {
setUserDetailsForGoogle(googleUser);
}, function(error) {
//alert(JSON.stringify(error, undefined, 2));
});
}
function setUserDetailsForGoogle(userData){

var email=userData.getBasicProfile().getEmail();
var socialid=userData.getBasicProfile().getId();
var name=userData.getBasicProfile().getName();
var gender=1;//userData.getBasicProfile().getGender();

$.ajax({
type:'Post',
data:{email:email,socialid:socialid}, // add the parameters you want to send to save
url:'users/googleLogin',
success:function(res){

var res = $.trim(res);
if(res=="true")
{
location.reload();
}
else if(res=="0")
{
alert("Not valid user!!");

}
else if(res=="error")
{
alert("Error in controller!!");
}

}
,
error:function(){
alert("Error in controller!!");
}
});
}
</script>


Users Controller



/*function used to login */
public function googleLogin()
{
$this->loadModel("User");

if ($this->request->is('Ajax'))
{
$data=$_POST;
$userInfo=$this->User->find('first',array('conditions' =>array('User.email'=>trim($data['email']))));
if (!empty($userInfo)) // user exist
{
$userStatus=trim($userInfo['User']['status']);
if($userStatus==1)// code to check if user is in active state
{
$userID=trim($userInfo['User']['id']);
$userSocialId=trim($data['socialid']);
$this->User->updateAll(array('google_id' =>"'".$userSocialId."'"),array('id'=>$userID));
/* write parameter in session to login */
$this->Session->write('User.email', trim($userInfo['User']['email']));
$login_user = $this->Auth->login();


echo "true";
}
else{
echo "0";//status inactive
}
}
else // new signup-
{
//$userData['User']['first_name']=trim($data['first_name']);
//$userData['User']['last_name']=trim($data['last_name']);
$userData['User']['email']=trim($data['email']);
$userData['User']['status']=1;
$userData['User']['google_id']=trim($data['socialid']);

$userData['User']['password']=md5($userPass);
$this->User->save($userData);
$userID=$this->User->getLastInsertId();
$userData['User']['id']=$userID;
$login_user = $this->Auth->login();


echo "true";
}
}
else{
echo "error";
}
die;
}
}









share|improve this question
























  • Maybe You should try github.com/kicaj/social
    – kicaj
    Nov 15 at 8:24














0












0








0







I am trying to setup google login theough cakephp, I can get to the authentication screen but when it comes back to my webpage, I recieve the alert Invalid user, which means something wrong in the username handover.



Can you someone help me troubleshoot it more? how can I test I am getting all the user name, email and photo from google?



Login.php:



<script src="https://apis.google.com/js/api:client.js"></script>
<script>
var googleUser = {};
var startApp = function() {
gapi.load('auth2', function(){
// Retrieve the singleton for the GoogleAuth library and set up the client.
auth2 = gapi.auth2.init({
client_id: 'xxxxxxx.apps.googleusercontent.com',
cookiepolicy: 'single_host_origin',
// Request scopes in addition to 'profile' and 'email'
scope: "https://www.googleapis.com/auth/userinfo.email https://www.googleapis.com/auth/plus.me"
});
attachSignin(document.getElementById('googleLoginBtn'));//id of google login button
});
};

function attachSignin(element) { // call this function on your login button

auth2.attachClickHandler(element, {},
function(googleUser) {
setUserDetailsForGoogle(googleUser);
}, function(error) {
//alert(JSON.stringify(error, undefined, 2));
});
}
function setUserDetailsForGoogle(userData){

var email=userData.getBasicProfile().getEmail();
var socialid=userData.getBasicProfile().getId();
var name=userData.getBasicProfile().getName();
var gender=1;//userData.getBasicProfile().getGender();

$.ajax({
type:'Post',
data:{email:email,socialid:socialid}, // add the parameters you want to send to save
url:'users/googleLogin',
success:function(res){

var res = $.trim(res);
if(res=="true")
{
location.reload();
}
else if(res=="0")
{
alert("Not valid user!!");

}
else if(res=="error")
{
alert("Error in controller!!");
}

}
,
error:function(){
alert("Error in controller!!");
}
});
}
</script>


Users Controller



/*function used to login */
public function googleLogin()
{
$this->loadModel("User");

if ($this->request->is('Ajax'))
{
$data=$_POST;
$userInfo=$this->User->find('first',array('conditions' =>array('User.email'=>trim($data['email']))));
if (!empty($userInfo)) // user exist
{
$userStatus=trim($userInfo['User']['status']);
if($userStatus==1)// code to check if user is in active state
{
$userID=trim($userInfo['User']['id']);
$userSocialId=trim($data['socialid']);
$this->User->updateAll(array('google_id' =>"'".$userSocialId."'"),array('id'=>$userID));
/* write parameter in session to login */
$this->Session->write('User.email', trim($userInfo['User']['email']));
$login_user = $this->Auth->login();


echo "true";
}
else{
echo "0";//status inactive
}
}
else // new signup-
{
//$userData['User']['first_name']=trim($data['first_name']);
//$userData['User']['last_name']=trim($data['last_name']);
$userData['User']['email']=trim($data['email']);
$userData['User']['status']=1;
$userData['User']['google_id']=trim($data['socialid']);

$userData['User']['password']=md5($userPass);
$this->User->save($userData);
$userID=$this->User->getLastInsertId();
$userData['User']['id']=$userID;
$login_user = $this->Auth->login();


echo "true";
}
}
else{
echo "error";
}
die;
}
}









share|improve this question















I am trying to setup google login theough cakephp, I can get to the authentication screen but when it comes back to my webpage, I recieve the alert Invalid user, which means something wrong in the username handover.



Can you someone help me troubleshoot it more? how can I test I am getting all the user name, email and photo from google?



Login.php:



<script src="https://apis.google.com/js/api:client.js"></script>
<script>
var googleUser = {};
var startApp = function() {
gapi.load('auth2', function(){
// Retrieve the singleton for the GoogleAuth library and set up the client.
auth2 = gapi.auth2.init({
client_id: 'xxxxxxx.apps.googleusercontent.com',
cookiepolicy: 'single_host_origin',
// Request scopes in addition to 'profile' and 'email'
scope: "https://www.googleapis.com/auth/userinfo.email https://www.googleapis.com/auth/plus.me"
});
attachSignin(document.getElementById('googleLoginBtn'));//id of google login button
});
};

function attachSignin(element) { // call this function on your login button

auth2.attachClickHandler(element, {},
function(googleUser) {
setUserDetailsForGoogle(googleUser);
}, function(error) {
//alert(JSON.stringify(error, undefined, 2));
});
}
function setUserDetailsForGoogle(userData){

var email=userData.getBasicProfile().getEmail();
var socialid=userData.getBasicProfile().getId();
var name=userData.getBasicProfile().getName();
var gender=1;//userData.getBasicProfile().getGender();

$.ajax({
type:'Post',
data:{email:email,socialid:socialid}, // add the parameters you want to send to save
url:'users/googleLogin',
success:function(res){

var res = $.trim(res);
if(res=="true")
{
location.reload();
}
else if(res=="0")
{
alert("Not valid user!!");

}
else if(res=="error")
{
alert("Error in controller!!");
}

}
,
error:function(){
alert("Error in controller!!");
}
});
}
</script>


Users Controller



/*function used to login */
public function googleLogin()
{
$this->loadModel("User");

if ($this->request->is('Ajax'))
{
$data=$_POST;
$userInfo=$this->User->find('first',array('conditions' =>array('User.email'=>trim($data['email']))));
if (!empty($userInfo)) // user exist
{
$userStatus=trim($userInfo['User']['status']);
if($userStatus==1)// code to check if user is in active state
{
$userID=trim($userInfo['User']['id']);
$userSocialId=trim($data['socialid']);
$this->User->updateAll(array('google_id' =>"'".$userSocialId."'"),array('id'=>$userID));
/* write parameter in session to login */
$this->Session->write('User.email', trim($userInfo['User']['email']));
$login_user = $this->Auth->login();


echo "true";
}
else{
echo "0";//status inactive
}
}
else // new signup-
{
//$userData['User']['first_name']=trim($data['first_name']);
//$userData['User']['last_name']=trim($data['last_name']);
$userData['User']['email']=trim($data['email']);
$userData['User']['status']=1;
$userData['User']['google_id']=trim($data['socialid']);

$userData['User']['password']=md5($userPass);
$this->User->save($userData);
$userID=$this->User->getLastInsertId();
$userData['User']['id']=$userID;
$login_user = $this->Auth->login();


echo "true";
}
}
else{
echo "error";
}
die;
}
}






cakephp google-login






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 17 at 18:18









M.suleman Khan

269214




269214










asked Nov 12 at 16:50









Emad A

207




207












  • Maybe You should try github.com/kicaj/social
    – kicaj
    Nov 15 at 8:24


















  • Maybe You should try github.com/kicaj/social
    – kicaj
    Nov 15 at 8:24
















Maybe You should try github.com/kicaj/social
– kicaj
Nov 15 at 8:24




Maybe You should try github.com/kicaj/social
– kicaj
Nov 15 at 8:24

















active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53266670%2fgoogle-login-using-cakephp%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown






























active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53266670%2fgoogle-login-using-cakephp%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Xamarin.iOS Cant Deploy on Iphone

Glorious Revolution

Dulmage-Mendelsohn matrix decomposition in Python