cordova - “Remember Me” functionality in a PhoneGap application -
cordova - “Remember Me” functionality in a PhoneGap application -
i new phonegap development. phonegap application has login page , need implement "remember me" alternative login page. have no thought functionality can help me. want sample code functionality.
please refer tried code following,
function checkpreauth() { /* console.log("checkpreauth");*/ if(window.localstorage.getitem("remember") == true) { if(window.localstorage.getitem("username") != undefined && window.localstorage.getitem("password") != undefined) { var form = $("#loginform"); $("#username", form).val(window.localstorage.getitem("username")); $("#password", form).val(window.localstorage.getitem("password")); $("#remember_me", form).attr('checked', true).checkboxradio("refresh"); handlelogin(); } } } $(document).on('pageinit', '#loginpage', function(){ //gettimezonename(); $('#submitbutton').click(function(e){ e.preventdefault(); var u = $("#username").val(); var p = $("#password").val(); handlelogin(); }); }); function handlelogin() { var form = $("#loginform"); $("#submitbutton",form).attr("disabled","disabled"); var u = $("#username", form).val(); var p = $("#password", form).val(); if(u != '' && p!= '') { $.ajax({ url: 'http://samplewebsite.com/api/login_api', type: 'post', datatype: 'json', data: $('form#loginform').serialize(), crossdomain: 'true', success: function(data) { if(data) { //window.localstorage.setitem("key", "value"); // var keyname = window.localstorage.key(i); if($("#remember_me").is(':checked')) { window.localstorage.setitem("remember", $("#remember_me").is(':checked')); } window.localstorage.setitem("userid", data.id); window.localstorage.setitem("username", u); window.localstorage.setitem("password", p); $.mobile.changepage("inbox.html"); } else { navigator.notification.alert("your login failed", function() {}); } },error: function (xhr, ajaxoptions, thrownerror) { navigator.notification.alert("your login failed",null); } }); } else { navigator.notification.alert("you must come in username , password", function() {},""); } }
html
<!doctype html> <html> <head> <title>car</title> <meta name="viewport" content="width=device-width, initial-scale=1"> <link rel="stylesheet" href="http://code.jquery.com/mobile/1.4.2/jquery.mobile-1.4.2.min.css"> <script src="http://code.jquery.com/jquery-1.10.2.min.js"></script> <script src="http://code.jquery.com/mobile/1.4.2/jquery.mobile-1.4.2.min.js"></script> </head> <body> <div data-role="page" id="page_login" > <div data-role="header"> <h1>login</h1> </div><!-- /header --> <div role="main" class="ui-content" id="content"> <form id="frm_login"> <input type="text" value="" id="username" name="username" placeholder="username" /> <input type="password" value="" id="vpassword" name="vpassword" placeholder="password" /> <input type="checkbox" name="remember_me" id="remember_me" class="custom check" /> <label for="remember_me" class="check-label" id="lbl_rem_me">remember me</label> <a href="" class="ui-btn" data-transition="slide" data-role="none" id="btn_log">login</a> </form> </div> </div> </body> </html>
javascript
<script> $(document).on("pageshow", "#page_login", function(e){ if(window.localstorage["username_rem"] != undefined && window.localstorage["password_rem"] != undefined) { if(window.localstorage["username_rem"] != "" && window.localstorage["password_rem"] != ""){ $('#frm_login :input[id=username]').val(window.localstorage["username_rem"]); $('#frm_login :input[id=vpassword]').val(window.localstorage["password_rem"]); $("#remember_me").attr('checked', true).checkboxradio("refresh"); } } }) $(document).on("click", "#btn_log", function(e){ var u = $('#frm_login :input[id=username]').val(); var p = $('#frm_login :input[id=vpassword]').val(); if($("#remember_me").is(':checked')){ window.localstorage["username_rem"] = u; window.localstorage["password_rem"] = p; }else{ window.localstorage["username_rem"] = ""; window.localstorage["password_rem"] = ""; } }) </script>
cordova
Comments
Post a Comment