Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Trying to save username / password in cookie ; JQuery

I saw this example about storing login information in cookie.

http://eisabainyo.net/weblog/2009/02/20/store-login-information-in-cookie-using-jquery/

Tried it, but can't seem to get it to work. I have to plugin and I don't see what's wrong here.

This page is Login.aspx which points to AccountMenu.aspx

EDIT IT'S FOR A DEMO, TEST, WHATEVER YOU WANNA CALL IT. this site will never go online. I KNOW this is not the way to do it. I'm looking for help to solve my problem, not people telling me this is bad design.

(...)

    <div data-role="content">

    <form action="AccountMenu.aspx" method="post" id="login">

        <label for="email">Email</label>
        <input type="text" id="email" name="email"/>

        <label for="password"><%= GetLabel("password") %></label>
        <input id="password" type="password" name="password" />

        <div data-role="fieldcontain">
        <fieldset data-role="controlgroup">
            <input type="checkbox" name="remember" id="remember" class="custom" checked="true" />
            <label for="remember">Remember me ?</label>
        </fieldset>
        </div>

        <input type="hidden" name="submit" value="submitted" />
        <button type="submit" name="submit" id="submit" data-theme="a"  value="<%= GetLabel("new-login") %>" ></button>
        <button type="button" disabled="disabled" data-theme="a" value="<%= GetLabel("new-account") %>"></button>        

    </div>

 </form>

    <script type="text/javascript">

        if ($('#remember').attr('checked')) 
        {
            var email = $('#email').attr("value");
            var password = $('#password').attr("value");

            // set cookies to expire in 14 days
            $.cookie('email', email, { expires: 14 });
            $.cookie('password', password, { expires: 14 });
            $.cookie('remember', true, { expires: 14 });                
        }
        else
        {
            // reset cookies
            $.cookie('email', null);
            $.cookie('password', null);
            $.cookie('remember', null);
        }

        var remember = $.cookie('remember');
        if (remember == 'true') 
        {
            var email = $.cookie('email');
            var password = $.cookie('password');
            // autofill the fields
            $('#email').attr("value", email);
            $('#password').attr("value", password);
        }

    </script>    

like image 254
JFFF Avatar asked Dec 14 '11 17:12

JFFF


People also ask

Does cookies Save username password?

Cookies can be useful, saving time to type in previously visited website login information for instance. Cookies do not directly display passwords, instead they contain a hash that stores your password. When a password has been hashed, it has been scrambled so only the website it came from can read it.

How do I store login information in cookies?

For login cookies, there are two common methods of storing login information in cookies: a signed cookie or a token cookie. Signed cookies typically store the user's name, maybe their user ID, when they last logged in, and whatever else the service may find useful.

Can jQuery create cookie?

Cookies can be set in the browser with the help of JavaScript or the jQuery.


1 Answers

you need to actually call the code when the user fills in the form

$(document).ready(function() {

        var remember = $.cookie('remember');
        if (remember == 'true') 
        {
            var email = $.cookie('email');
            var password = $.cookie('password');
            // autofill the fields
            $('#email').val(email);
            $('#password').val(password);
        }


    $("#login").submit(function() {
        if ($('#remember').is(':checked')) {
            var email = $('#email').val();
            var password = $('#password').val();

            // set cookies to expire in 14 days
            $.cookie('email', email, { expires: 14 });
            $.cookie('password', password, { expires: 14 });
            $.cookie('remember', true, { expires: 14 });                
        }
        else
        {
            // reset cookies
            $.cookie('email', null);
            $.cookie('password', null);
            $.cookie('remember', null);
        }
  });
});
like image 128
mplungjan Avatar answered Nov 06 '22 06:11

mplungjan