I'm developing 'email verification' module.
First of all, I have a model named 'SNUser' having 'One To One' relationship with the default 'user' model below :
class SNUser(models.Model):
# Manager
objects = SNUserManager()
# Auth
user = models.OneToOneField(User, unique=True, primary_key=True)
I finished sending email containing the unique token for an user.
When the link is clicked, I searched the correct SNUser object by the token.(That token is one of the field of SNUser model).
So what I want to develop is that when that token link is clicked, the correct user could use the service without login.
So when that link is clicked, 'login(request,user)' should be done.
I already tried two ways to solve this.
when I retrieved the user model from SNUser model and did 'login(request, user)' directly :
snuser = SNUser.objects.get(token=key)
user = snuser.user
login(request, user) It results with the error : Anonymous User.
- After retrieving the default user from SNUser model, I did 'authenticate(username=user.username, password=user.password)'. But many of you already notice, user.password is the hashed not the rawone. So it fails again.
My goal is to retrieve the user from the token included in the link I sent and make him/her use the service without login module as many services follow this process.
Is there anyway to solve this one?
Thank you!
**** UPDATE ****
I created the custom backend not requiring raw password for my situation below :
class SNUserEmailVerificationAuthBackend(ModelBackend):
def authenticate(self, username=None, email=None):
try:
return User.objects.get(username=username, email=email)
except User.DoesNotExist:
return None
def get_user(self, username):
try:
return User.objects.get(username=username)
except User.DoesNotExist:
return None
The updated critical code in views.py is below :
snuser.is_email_verified = True
snuser.save()
user = snuser.user
manager=SNUserEmailVerificationAuthBackend()
new_user=manager.authenticate(username=user.username,email=user.email)
login(request,new_user)
login(request,new_user)
Above line is working(it means user suceeds in loggin in) but raising the error simultaneously : 'User' object has no attribute 'backend'
****REMOVE DUPLICATED MARK****