0

I am trying to login a user . after creating his profile . but when he authorises him he logs in as anonymous user.

from django.contrib.auth import authenticate as authorise

def shopify_register(request):

    print "############*****GOT DETAILS*****############"
    password = request.POST['pass']


    subs, stat = Subscription.objects.get_or_create(validity=datetime.now()+timedelta(days=30))
    newuser, stat = User.objects.get_or_create(username=request.POST['email'])
    newuser.set_password(password)
    newuser.save()
    user, stat = UserProfile.objects.get_or_create(subscription=subs,user=newuser)
    domain, stat = Domain.objects.get_or_create(user_profile=user, shortname=request.POST['shop_name'], keywords=request.POST['keywords'])

    profile,stat = ShopifyUserProfile.objects.get_or_create(shop_user =user.. etc )
    username = request.POST['email']



    authuser = authorise(username=username, password=mdpass)
    domain = Domain.objects.get(user_profile=user)
    testimonies = Testimony.objects.filter(domain=domain).filter(show=0).order_by('-timestamp')
    c = RequestContext(request, {
        'user' : user,
        'testimonies': testimonies,
        'request': request,
        'mentions': 1,
        'domain': domain
    })
    return render_to_response('homepage.html', context_instance=c)

UPDATE :

authuser = authorise(username=username, password=mdpass)

this does not authorises the user . though the User has been created .

It works fine but it logs user as anonymous user ? what is wrong here ?

Removed the md5 hashing but still user is not being authorised.

UPDATE :2

ipdb> authuser = authorise(username=username, password=password)
ipdb> authuser
<User: ratan@kumar.com>
ipdb> temp=auth_login(request,authuser)
ipdb> temp
ipdb> print temp
None

It means it has authorised the user but it has not able to log him in.

Ratan Kumar
  • 1,640
  • 3
  • 25
  • 52
  • 1
    I don't know what you're trying to do here, but you should definitely not be hashing the password before passing it to `set_password` - that method takes care of the hashing itself. – Daniel Roseman May 15 '13 at 08:43
  • I am trying to authenticate a user. User class is derived user from basic django user model. – Ratan Kumar May 15 '13 at 08:44
  • I can see that, but my point remains. You should not be hashing the password there. – Daniel Roseman May 15 '13 at 08:51
  • that's not the issue . my user get logged in as anonymous i don't know why ? i did the authentication. and he does exist in django admin panel. – Ratan Kumar May 15 '13 at 08:54
  • You claim that's not the issue, but for all we know it might well be. What, for example, is your `authorise` function here? Is it actually doing the login? How? We can't tell. – Daniel Roseman May 15 '13 at 08:56
  • sorry for being rude ! :) my authorise function is from django.contrib.auth import authenticate as authorise – Ratan Kumar May 15 '13 at 08:57
  • @RATS your authorise function isn't working because you're passing the user password wrong. Like Daniel said several times, you *should not* hash the password. – Bibhas Debnath May 15 '13 at 09:08
  • I removed the md5 hashing but still user is not being logged in . – Ratan Kumar May 15 '13 at 09:24

2 Answers2

2

You need to call login function after authenticate.

from django.contrib.auth import login as auth_login
auth_login(request, user)
Aldarund
  • 17,312
  • 5
  • 73
  • 104
1

You need to send password entered by user to authorise as it is, don't need to shs224 it. authorise will take care of hashing it.

from django.contrib.auth import login

#remove md5 step
authuser = authorise(username=username, password=password)
login(authuser, request)

You need to call login after authenticate.

Akshar Raaj
  • 14,231
  • 7
  • 51
  • 45