login/register ui, add register to menu

This commit is contained in:
j 2023-08-21 12:18:23 +02:00
parent 567ae6bde3
commit 7e4be4a233
4 changed files with 47 additions and 2 deletions

View file

@ -142,3 +142,31 @@ nav.overlay {
opacity: 0.8;
}
.login, .register {
margin-top: 16px;
margin-left: 24px;
textarea,
input {
padding: 4px;
margin-left: 0;
margin-top: 4px;
margin-bottom: 4px;
margin-right: 4px;
background: none;
color: white;
border: 1px solid green;
}
button {
margin-top: 4px;
background: black;
color: white;
border: solid 1px green;
padding: 8px;
}
button:hover,
button:active {
border: solid 1px lightgreen;
cursor: pointer;
}
}

View file

@ -53,6 +53,8 @@
<a href="/logout/">logout</a>
{% else %}
<a href="/login/">login</a>
<br>
<a href="/register/">register</a>
{% endif %}
</div>
</nav>

View file

@ -7,8 +7,9 @@
<form method="POST">
{% csrf_token %}
<input name="username" type="text" placeholder="your username" required></input>
<br>
<input name="password" type="password" placeholder="your password" required></input>
<div class="buttons login">
<div class="buttons">
<button id="login">Login</button>
</div>
<div class="error">{{ error }}</div>

View file

@ -16,8 +16,14 @@ User = get_user_model()
@ratelimit(method="POST", block=True, rate="5/m")
def register(request):
context = default_context(request)
response = {}
request_type = 'json'
if request.method == "POST":
if "username" in request.POST and "password" in request.POST:
data = request.POST
request_type = 'html'
else:
data = json.loads(request.body)
if User.objects.filter(username__iexact=data['username']).exists():
response['error'] = 'username not allowed'
@ -33,8 +39,16 @@ def register(request):
user = django.contrib.auth.authenticate(username=data['username'], password=data['password'])
django.contrib.auth.login(request, user)
response['user'] = user.username
if request_type == 'html':
if 'error' in response:
context['error'] = response['error']
return render(request, 'register.html', context)
else:
return redirect('/')
return render_to_json(response)
else:
if request.user.is_authenticated:
return redirect('/')
context = default_context(request)
return render(request, 'register.html', context)