Search code examples
androidangularjscookiesonsen-uimonaca

Use cookie to save the login information,but for android platform it cannot work


I use onsen-ui and angularjs to achieve a login function on Monaca, actually it works well when I debug on iphone and preview on the website, but when I try to use it on a android system, i find that cookies are not saved. The next time I access the login page, what I see is a welcome page, but on the android system, the system asked me to log in again. When I login success , I save my information on cookie. My code as follows:

$scope.login = function(){  
    $.ajax({
        type: 'post',
        url: 'https://www.xxxxxxx.com/api/public/user_login',
        data:{
            username:document.getElementById('login_username').value, 
            password:document.getElementById('login_password').value
        },
        success: function(data) {
            if(data.error != "" & data.error != null ){
                alert(data.error); 
                delCookie('username');                  
            }else if(data.status == 0){
                setCookie('username',data.username,365);
                $scope.username = getCookie('username');
                document.getElementById('loginInfo').style.display="none";
                document.getElementById('welcome').style.display="block";
            }
        },
    });
};
function getCookie(login_name){
if (document.cookie.length>0){ 
    login_start=document.cookie.indexOf(login_name + "=")
    if (login_start!=-1){ 
        login_start=login_start + login_name.length+1; 
        login_end=document.cookie.indexOf(";",login_start);
        if (login_end==-1){
            login_end=document.cookie.length;
        }
        return unescape(document.cookie.substring(login_start,login_end));
    } 
}
return "";
}

function setCookie(c_name,value,expiredays){
var exdate=new Date();
exdate.setDate(exdate.getDate()+expiredays);
document.cookie=c_name+ "=" +escape(value)+((expiredays==null) ? "" : "; expires="+exdate.toGMTString());
}

function  delCookie(name){
var  exp  =  new  Date();
exp.setTime  (exp.getTime()  -  1);
var  cval  =  getCookie (name);
document.cookie  =  name  +  "="  +  cval  +  ";  expires="+  exp.toGMTString();
}

Solution

  • You can use the $cookieStore it seems deprecated now, instead `'$cookie is the one we need to use.

    Instead of using the document.cookie, which is a web API, use the angular inbuilt service for the same, you won't have difficulties with this in android and ios.

    You need to just inject the dependency in your controller:

    as:

    angular.module('sampleApp', ['ngCookies'])
    .controller('AuthController', function ($scope,
                                            $cookies,//injecting the dependency here
                                            $timeout,
                                            ngDialog) {
    
    
    $scope.login = function(){  
        $.ajax({
            type: 'post',
            url: 'https://www.xxxxxxx.com/api/public/user_login',
            data:{
                username:document.getElementById('login_username').value, 
                password:document.getElementById('login_password').value
            },
            success: function(data) {
                if(data.error != "" & data.error != null ){
                    alert(data.error); 
                    delCookie('username');                  
                }else if(data.status == 0){
                    setCookie('username',data.username,365);
                    $scope.username = getCookie('username');
                    document.getElementById('loginInfo').style.display="none";
                    document.getElementById('welcome').style.display="block";
                }
            },
        });
    };
    function getCookie(login_name){
    if (document.cookie.length>0){ 
        login_start= document.cookie.indexOf(login_name + "=")
        //use $cookies.get(login_name) to get value of the login_name param set in cookie
        if (login_start!=-1){ 
            login_start=login_start + login_name.length+1; 
            login_end=document.cookie.indexOf(";",login_start);
           //use $cookies.get(login_name) to get value of the login_name param set in cookie
            if (login_end==-1){
                login_end=document.cookie.length;
                //use $cookies.get(login_name) to get value of the login_name param set in cookie
            }
            return unescape(document.cookie.substring(login_start,login_end));
        } 
    }
    return "";
    }
    
    function setCookie(c_name,value,expiredays){
    var exdate=new Date();
    exdate.setDate(exdate.getDate()+expiredays);
    document.cookie=c_name+ "=" +escape(value)+((expiredays==null) ? "" : "; expires="+exdate.toGMTString());
    //use $cookies.put(key, value) to get value of the login_name param set in cookie
    }
    
    function  delCookie(name){
    var  exp  =  new  Date();
    exp.setTime  (exp.getTime()  -  1);
    var  cval  =  getCookie (name);
    document.cookie  =  name  +  "="  +  cval  +  ";  expires="+  exp.toGMTString();
    //use $cookies.put(key, value) to get value of the login_name param set in cookie
    }
    
    
    }
    

    It's easy to implement and use, please refer the Angular documentation for the same.