Skip to content

Commit

Permalink
fixed login bug
Browse files Browse the repository at this point in the history
right now only support username through login
  • Loading branch information
Ardhweb committed Oct 6, 2024
1 parent 77ff6ed commit dd8fd73
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 14 deletions.
4 changes: 2 additions & 2 deletions accounts/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@
# password = forms.CharField(label='Password', widget=forms.PasswordInput(attrs={'class': 'form-control'}))

class LoginForm(forms.Form):
email = forms.CharField(label='Username or Email',widget=forms.TextInput(attrs={'class':'form-control',}))
password = forms.CharField(widget=forms.PasswordInput(attrs={'class':'form-control','type':'password'}))
username = forms.CharField(label='Username or Email',widget=forms.TextInput(attrs={'class':'form-control',}))
password = forms.CharField(widget=forms.PasswordInput(attrs={'class':'form-control'}))

21 changes: 12 additions & 9 deletions accounts/views.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from django.contrib.auth import logout, authenticate, login
from django.shortcuts import render, redirect
from django.http import HttpResponseRedirect
from django.http import HttpResponseRedirect, HttpResponse
from django.contrib import messages
from .forms import LoginForm
def logout_user(request):
Expand All @@ -19,20 +19,23 @@ def logout_user(request):
from django.contrib.auth import authenticate, login
from django.contrib import messages
from .forms import LoginForm # Ensure you have your LoginForm imported
from django.contrib.auth.models import User
def login_user(request):

if request.method == 'POST':
form = LoginForm(request.POST)
if form.is_valid():
cd = form.cleaned_data
user = authenticate(request, email=cd['email'], password=cd['password'])
if user is not None:
if user.is_active:
login(request, user)
return redirect('index')
else:
messages.error(request, "This account is disabled.")
user = authenticate(request, username=cd['username'], password=cd['password'])
print(f"Authenticated user: {user}") # Debug statement
if user is not None and user.is_active:
login(request, user)
return redirect('index')
else:
messages.error(request, "Invalid email or password.")
return HttpResponse("Authentication failed: User not found or inactive")
else:
return HttpResponse("Form is invalid")
else:
form = LoginForm()
return render(request, 'accounts/login.html', {'form': form})

6 changes: 4 additions & 2 deletions coverit/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@
from django.template.loader import get_template
from weasyprint import HTML
from django.views import View
def index(request):
return render(request,'coverit/index.html')


def index(request):
return render(request, 'coverit/index.html')

def create_cover(request):
breadcrumbs = [
{'name': 'Page 1', 'url': '/'},
Expand Down
2 changes: 1 addition & 1 deletion templates/utility/coverit/navbar.html
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
{% if user.is_superuser %}
{% endif %}
<a href="{% url 'cover_listing' %}" type="button" class="btn btn-primary btn-sm">Your Letter's</a>
<a type="button" class="btn btn-secondary btn-sm" href="{% url 'accounts:logout' %}">Log-out</a>
<a type="button" class="btn btn-danger btn-sm" href="{% url 'accounts:logout' %}">Log-out</a>
{% else %}
<a type="button" class="btn btn-secondary btn-sm">Sign-up</a>
<a type="button" class="btn btn-secondary btn-sm" href="{% url 'accounts:login' %}" >Login</a>
Expand Down

0 comments on commit dd8fd73

Please sign in to comment.