django - Authenticate user using email and password -
i have form allow users login via username . decided allow user login via email instead of username.
first, not duplication of question relating logging via email because in scenario, validate , authenticate user in forms.py
before proceed final login in views give me chance raise error incorrect login passwords etc.
the issue i'm facing modified forms.py
raise error if email doesn't exist works wouldn't let user login via email.
def loginrequest(request): form = loginform(request.post or none) if request.post , form.is_valid(): user = form.login(request) if user: login(request, user) return httpresponseredirect(reverse('hello')) return render(request, 'login.html',{'form': form})
this original code allow users login via username
class loginform(forms.form): username = forms.charfield() password = forms.charfield( widget=forms.passwordinput(render_value=false) ) def clean(self): username = self.cleaned_data.get('username') password = self.cleaned_data.get('password') user = authenticate(username=username, password=password) if not user or not user.is_active: raise forms.validationerror("sorry, login invalid. please try again.") return self.cleaned_data def login(self, request): username = self.cleaned_data.get('username') password = self.cleaned_data.get('password') user = authenticate(username=username, password=password) return user
this modified code allow users login via email. thought lot how best idea came with. sorry it's bit confusing. problem is, wouldn't let user login. don't understand why.
class loginform(forms.form): username = forms.charfield() password = forms.charfield( widget=forms.passwordinput(render_value=false) ) def clean(self): user = self.cleaned_data.get('username') password = self.cleaned_data.get('password') if user.objects.filter(email=user).exists(): password = self.cleaned_data.get('password') user = authenticate(username=user.username, password=password) if not user or not user.is_active: raise forms.validationerror("sorry, login invalid. please try again.") return self.cleaned_data def login(self, request): username = self.cleaned_data.get('username') password = self.cleaned_data.get('password') user = authenticate(username=username, password=password) return user
can please me?
as dougis
pointed out trying authenticate user using email, authenticate
function authenticates user based on username , password. here trick authenticate user using email (complete working form):
from django.db.models import q django.core.exceptions import objectdoesnotexist django.contrib.auth.models import user django import forms django.contrib.auth import authenticate class loginform(forms.form): email = forms.charfield() password = forms.charfield( widget=forms.passwordinput(render_value=false) ) def clean(self): user = self.authenticate_via_email() if not user: raise forms.validationerror("sorry, login invalid. please try again.") else: self.user = user return self.cleaned_data def authenticate_user(self): return authenticate( username=self.user.username, password=self.cleaned_data['password']) def authenticate_via_email(self): """ authenticate user using email. returns user object if authenticated else none """ email = self.cleaned_data['email'] if email: try: user = user.objects.get(email__iexact=email) if user.check_password(self.cleaned_data['password']): return user except objectdoesnotexist: pass return none
views.py
def loginrequest(request): form = loginform(request.post or none) if request.method == 'post' , form.is_valid(): user = form.authenticate_user() login(request, user) return httpresponseredirect(reverse('hello')) return render(request, 'login.html',{'form': form})
Comments
Post a Comment