How can I make the user login automatically after email verification?

How can I make the user login automatically after email verification?

This function works if the password is not hashed.

from django.contrib import auth

try:
    uid = urlsafe_base64_decode(uidb64).decode()
    user = Account._default_manager.get(pk=uid)
except(TypeError, ValueError, OverflowError, Account.DoesNotExist):
    user = None

userlogin = auth.authenticate(email=user.email, password=user.password)
auth.login(request, userlogin)

I want email confirmation from the user, so I can only use the hashed password but encrypted password does not work with this function.

Error:
AttributeError: ‘AnonymousUser’ object has no attribute ‘_meta’

Do you have any suggestion?

Thank you.

If you are sure that it’s a valid user clicking on that link (a really bad idea), then you can just log the user in using the login function - you don’t need to use the authenticate function.

1 Like