Skip to content

Commit a01c94e

Browse files
committed
feat(accounts): add signup, login and logout (urls, views and templates)
1 parent 91fde7d commit a01c94e

File tree

10 files changed

+183
-25
lines changed

10 files changed

+183
-25
lines changed

accounts/__init__.py

Whitespace-only changes.

accounts/apps.py

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
from django.apps import AppConfig
2+
3+
4+
class AccountsConfig(AppConfig):
5+
default_auto_field = 'django.db.models.BigAutoField'
6+
name = 'accounts'

accounts/forms.py

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
from django import forms
2+
from django.contrib.auth.forms import UserCreationForm
3+
from django.contrib.auth.models import User
4+
5+
6+
class CustomUserCreationForm(UserCreationForm):
7+
# email = forms.EmailField(required=True)
8+
9+
class Meta:
10+
model = User
11+
fields = ('username', 'email', 'password1', 'password2')
12+
# fields = '__all__'

accounts/migrations/__init__.py

Whitespace-only changes.
+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{% extends "base.html" %}
2+
{% block content %}
3+
<h2>Accesso Utente</h2>
4+
<form method="post">
5+
{% csrf_token %}
6+
{{ form.as_p }}
7+
<button type="submit">Accedi</button>
8+
</form>
9+
{% endblock %}
+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{% extends "base.html" %}
2+
{% block content %}
3+
<h2>Registrazione Utente</h2>
4+
<form method="post">
5+
{% csrf_token %}
6+
{{ form.as_p }}
7+
<button type="submit">Registrati</button>
8+
</form>
9+
{% endblock %}

accounts/urls.py

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
from django.urls import path
2+
from . import views
3+
4+
urlpatterns = [
5+
# User Registration and Authentication
6+
path('signup/', views.SignUpView.as_view(), name='signup'),
7+
path('login/', views.LoginView.as_view(), name='login'),
8+
path('logout/', views.LogoutView.as_view(), name='logout'),
9+
# Password Handling
10+
# path('password_change/', views.PasswordChangeView.as_view(), name='password_change'),
11+
# path('password_change/done/', views.PasswordChangeDoneView.as_view(), name='password_change_done'),
12+
# path('password_reset/', views.PasswordResetView.as_view(), name='password_reset'),
13+
# path('password_reset/done/', views.PasswordResetDoneView.as_view(), name='password_reset_done'),
14+
# path('reset/<uidb64>/<token>/', views.PasswordResetConfirmView.as_view(), name='password_reset_confirm'),
15+
# path('reset/done/', views.PasswordResetCompleteView.as_view(), name='password_reset_complete'),
16+
]

accounts/views.py

+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
from django.urls import reverse_lazy
2+
from django.views.generic import CreateView
3+
from django.contrib.auth.views import (
4+
LoginView as DjangoLoginView,
5+
LogoutView as DjangoLogoutView,
6+
PasswordChangeView as DjangoPasswordChangeView,
7+
PasswordChangeDoneView as DjangoPasswordChangeDoneView,
8+
PasswordResetView as DjangoPasswordResetView,
9+
PasswordResetDoneView as DjangoPasswordResetDoneView,
10+
PasswordResetConfirmView as DjangoPasswordResetConfirmView,
11+
PasswordResetCompleteView as DjangoPasswordResetCompleteView
12+
)
13+
from .forms import CustomUserCreationForm
14+
15+
from django.shortcuts import redirect
16+
from django.contrib.auth import logout
17+
18+
19+
# Signup
20+
class SignUpView(CreateView):
21+
form_class = CustomUserCreationForm
22+
template_name = 'accounts/signup.html'
23+
success_url = reverse_lazy('login')
24+
25+
26+
# Login
27+
class LoginView(DjangoLoginView):
28+
template_name = 'accounts/login.html'
29+
30+
31+
# Logout
32+
class LogoutView(DjangoLogoutView):
33+
template_name = 'accounts/logout.html'
34+
35+
36+
# Password Change
37+
class PasswordChangeView(DjangoPasswordChangeView):
38+
template_name = 'accounts/password_change.html'
39+
success_url = reverse_lazy('password_change_done')
40+
41+
42+
class PasswordChangeDoneView(DjangoPasswordChangeDoneView):
43+
template_name = 'accounts/password_change_done.html'
44+
45+
46+
# Password Reset
47+
class PasswordResetView(DjangoPasswordResetView):
48+
template_name = 'accounts/password_reset.html'
49+
email_template_name = 'accounts/password_reset_email.html'
50+
success_url = reverse_lazy('password_reset_done')
51+
52+
53+
class PasswordResetDoneView(DjangoPasswordResetDoneView):
54+
template_name = 'accounts/password_reset_done.html'
55+
56+
57+
class PasswordResetConfirmView(DjangoPasswordResetConfirmView):
58+
template_name = 'accounts/password_reset_confirm.html'
59+
success_url = reverse_lazy('password_reset_complete')
60+
61+
62+
class PasswordResetCompleteView(DjangoPasswordResetCompleteView):
63+
template_name = 'accounts/password_reset_complete.html'

config/urls.py

+17-1
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,22 @@
3434
]
3535

3636

37+
urlpatterns += [
38+
path('accounts/', include('accounts.urls')),
39+
# User Authentication
40+
path('accounts/', include('django.contrib.auth.urls')),
41+
# Login: /accounts/login/
42+
# Logout: /accounts/logout/
43+
# Password Change: /accounts/password_change/
44+
# Password Change Done: /accounts/password_change/done/
45+
# Password Reset: /accounts/password_reset/
46+
# Password Reset Done: /accounts/password_reset/done/
47+
# Password Reset Confirm: /accounts/reset/<uidb64>/<token>/
48+
# Password Reset Complete: /accounts/reset/done/
49+
50+
]
51+
52+
3753
if settings.DEBUG:
3854
urlpatterns += static(settings.STATIC_URL, document_root=settings.BASE_DIR / 'static')
39-
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
55+
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

templates/includes/navbar.html

+51-24
Original file line numberDiff line numberDiff line change
@@ -74,32 +74,59 @@
7474

7575

7676
<!-- Dropdown Authentication -->
77-
<li class="nav-item dropdown">
78-
<a
79-
class="nav-link dropdown-toggle" href="#"
80-
id="authentication-dropdown"
81-
role="button" data-bs-toggle="dropdown" aria-expanded="false"
82-
>
83-
{% if user.is_authenticated %}
84-
Esci
85-
{% else %}
86-
Accedi
77+
<li class="nav-item dropdown">
78+
<a
79+
class="nav-link dropdown-toggle"
80+
href="#"
81+
id="authentication-dropdown"
82+
role="button"
83+
data-bs-toggle="dropdown"
84+
aria-expanded="false"
85+
>
86+
{% if user.is_authenticated %}
87+
👤 {{ user.username }}
88+
{% else %}
89+
🔑 Autenticazione
90+
{% endif %}
91+
</a>
92+
<ul class="dropdown-menu" aria-labelledby="authentication-dropdown">
93+
{% if user.is_authenticated %}
94+
<li>
95+
<a class="dropdown-item" href="#">
96+
📋 Profilo
97+
</a>
98+
</li>
99+
<li>
100+
<form method="post" action="{% url 'logout' %}">
101+
{% csrf_token %}
102+
<button type="submit" class="dropdown-item">
103+
🚪 Esci
104+
</button>
105+
</form>
106+
</li>
107+
{% if user.is_staff or user.is_superuser %}
108+
<li><hr class="dropdown-divider"></li>
109+
<li>
110+
<a class="dropdown-item" href="/admin/">
111+
⚙️ Admin Area
112+
</a>
113+
</li>
87114
{% endif %}
88-
</a>
89-
<ul class="dropdown-menu" aria-labelledby="authentication-dropdown">
90-
<li><a class="dropdown-item" href="#">Esci</a></li>
91-
<li><a
92-
class="dropdown-item"
93-
href="https://aleattene.github.io/pcto-deffenu-informatica-frontend/"
94-
target="_blank"
95-
>Accedi</a></li>
96-
<li><hr class="dropdown-divider"></li>
97-
<li><a class="dropdown-item" href="#">Registrati</a></li>
98-
<li><hr class="dropdown-divider"></li>
99-
<li><a class="dropdown-item" href="admin/">Admin Area</a></li>
115+
{% else %}
116+
<li>
117+
<a class="dropdown-item" href="{% url 'login' %}">
118+
🔓 Accedi
119+
</a>
120+
</li>
121+
<li>
122+
<a class="dropdown-item" href="{% url 'signup' %}">
123+
📝 Registrati
124+
</a>
125+
</li>
126+
{% endif %}
127+
</ul>
128+
</li>
100129

101-
</ul>
102-
</li>
103130

104131
<!-- Temporary -->
105132
<!--

0 commit comments

Comments
 (0)