1

I am attempting to use django for my website. i implemented login using this code:7

user_django = authenticate(username=email, password=passw)
login(request, user_django)

which using django rest framework is working. But when i use it with an ajax request i think its not storing cookies. i am also using a django function to check if the user is logged in:

if request.user.is_authenticated():
        return Response('1')
    else:
        return Response('0')

but it always return 0. Do i have to do anything special on the client side to store the login information?

my javascript login:

function userLogin(){
    var user_email = $('#login_user_email').val();
    var user_pwd   = $('#login_user_pwd').val();

    var data = {email:user_email, password:user_pwd};

    $.ajax(prefix+ 'userLogin/', {
        success: function(output, status, xhr){
            $('#login_btn').click();
        },
        data: data,
        type: 'POST'
    });


}
pedrotorres
  • 1,222
  • 2
  • 13
  • 26
  • Can you post the django log file? – rudolph9 Mar 24 '13 at 16:44
  • this logs?[24/Mar/2013 11:26:45] "POST /api/userLogin/ HTTP/1.1" 200 17 [24/Mar/2013 11:30:03] "GET /api/getOpeningAds/?format=json HTTP/1.1" 200 4121 [24/Mar/2013 11:31:29] "POST /api/userLogin/ HTTP/1.1" 200 17 [24/Mar/2013 11:31:50] "GET /api/checkLogin/ HTTP/1.1" 200 3 [24/Mar/2013 11:49:35] "GET /api/getOpeningAds/?format=json HTTP/1.1" 200 4121 [24/Mar/2013 11:49:51] "POST /api/userLogin/ HTTP/1.1" 200 17 – pedrotorres Mar 24 '13 at 16:54
  • the problem is that django is returning "set-cookie" with the response but when i go to resources using chrome "inspect element" the session id isnt there? – pedrotorres Mar 24 '13 at 17:17
  • function userLogin(){ var user_email = $('#login_user_email').val(); var user_pwd = $('#login_user_pwd').val(); var data = {email:user_email, password:user_pwd}; $.ajax(prefix+ 'userLogin/', { success: function(output, status, xhr){ $('#login_btn').click(); }, data: data, type: 'POST' }); } – pedrotorres Mar 27 '13 at 23:03

1 Answers1

0

This is a familiar problem to me and might be duplicate to other question on SO,

look at these, you will find the solution .

Community
  • 1
  • 1
Alireza Savand
  • 3,462
  • 3
  • 26
  • 36