def test_unknown_user_by_email(self): form = auth_forms.LoginForm({ 'redirect': '/', 'email': '*****@*****.**', 'password': '******' }) self.assertFalse(form.is_valid())
def login(request): loginForm = forms.LoginForm() error = None if request.method == "POST": loginForm = forms.LoginForm(request.POST) if loginForm.is_valid(): username = loginForm.cleaned_data['username'] password = loginForm.cleaned_data['password'] user = authenticate(username=username, password=password) if user: auth_login(request, user) return HttpResponseRedirect('/') else: error = "Invalid UserName or Password" context = {"form": loginForm, "error": error} return render(request, 'auth/login.html', context)
def reg(): form = forms.LoginForm() if form.validate_on_submit(): name = form.name.data form.name.data = '' passwd = form.passwd.data form.name.data = '' print name, passwd # return render_template('forms.html', form=form, name=name, passwd=passwd) return render_template('forms.html', form=form)
def login(request): error = None form = forms.LoginForm() if request.method=='POST': form = forms.LoginForm(request.POST) if form.is_valid(): username = form.cleaned_data['username'] password = form.cleaned_data['password'] user = authenticate(username = username, password = password) if user: auth_login(request, user) return HttpResponseRedirect('/') else: error = "INVALID USERNAME OR PWD" context = { "form" : form, "error" : error } return render(request, 'login.html', context)
def login(request): loginForm = forms.LoginForm() error = None if request.method == "POST": loginForm = forms.LoginForm(request.POST) if loginForm.is_valid(): username = loginForm.cleaned_data['username'] password = loginForm.cleaned_data['password'] user = authenticate(username=username, password=password) if user: auth_login(request, user) # STARTS A SESSION FOR THIS USER return HttpResponseRedirect('/') else: error = "Invalid username and password" context = {'form': loginForm, 'error': error} return render(request, 'auth/login.html', context)
def auth_login(request): loginform = forms.LoginForm() error = None if request.method == 'POST': loginform = forms.LoginForm(request.POST) if loginform.is_valid(): username = loginform.cleaned_data['username'] password = loginform.cleaned_data['password'] user = authenticate(username=username, password=password) if user: login( request, user ) # login() saves the user’s ID in the session, using Django’s session framework. # it is used to attach the user to the current session. return HttpResponseRedirect('/') else: error = "Invalid username or password" context = { 'loginform': loginform, 'error': error, } return render(request, 'auth/login.html', context=context)
def Login(request): if request.method == "POST": form = request.POST.copy() username = form.get("UserName") password = form.get("password") user = authenticate(request, username=username, password=password) if user is not None: login(request, user) return render(request, 'Home/homepage.html') else: error = "Incorrect Username or Password" form = forms.LoginForm() return render(request, 'auth/login.html', { 'error': error, "form": form }) else: form = forms.LoginForm() return render(request, 'auth/login.html', {"form": form})
def test_login_with_case_insensitive_email(self): # Set up a test user. email = '*****@*****.**' user = User(email=email) user.set_password('password') user.save() # This should succeed. form = auth_forms.LoginForm({ 'redirect': '/', 'email': '*****@*****.**', 'password': '******' }) self.assertTrue(form.is_valid())
def hello(request): """Implements our login page.""" redirect = '/' tmpl = loader.get_template('auth/hello.html') if request.method == 'GET': redirect = request.GET.get('redirect', '/') # Already signed in? Then redirect immediately. if request.user: return http.HttpResponseRedirect(redirect) form = auth_forms.LoginForm(initial={ 'redirect': redirect, }) else: form = auth_forms.LoginForm(request.POST) if form.is_valid(): response = http.HttpResponseRedirect(form.cleaned_data['redirect']) auth.attach_credentials(response, form.user) # Update the last login time in the User record. form.user.last_login = datetime.datetime.now() AutoRetry(form.user).save() return response ctx = RequestContext(request, {'form': form}) return http.HttpResponse(tmpl.render(ctx))
def test_login_form(self): # Set up a test user. email = '*****@*****.**' user = User(email=email) user.set_password('password') user.save() # Missing required data form = auth_forms.LoginForm({'redirect': '/'}) self.assertFalse(form.is_valid()) form = auth_forms.LoginForm({'redirect': '/', 'email': email}) self.assertFalse(form.is_valid()) # Form should fail to validate if password is incorrect. form = auth_forms.LoginForm({ 'redirect': '/', 'email': email, 'password': '******' }) self.assertFalse(form.is_valid()) # This should succeed. form = auth_forms.LoginForm({ 'redirect': '/', 'email': email, 'password': '******' }) self.assertTrue(form.is_valid()) # The form should reject inactive users. user.is_active = False user.save() form = auth_forms.LoginForm({ 'redirect': '/', 'email': email, 'password': '******' }) self.assertFalse(form.is_valid()) # The form should reject unknown users. form = auth_forms.LoginForm({ 'redirect': '/', 'email': '*****@*****.**', 'password': '******' }) self.assertFalse(form.is_valid())