login/register ui, add register to menu
This commit is contained in:
parent
567ae6bde3
commit
7e4be4a233
4 changed files with 47 additions and 2 deletions
|
@ -142,3 +142,31 @@ nav.overlay {
|
||||||
opacity: 0.8;
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -53,6 +53,8 @@
|
||||||
<a href="/logout/">logout</a>
|
<a href="/logout/">logout</a>
|
||||||
{% else %}
|
{% else %}
|
||||||
<a href="/login/">login</a>
|
<a href="/login/">login</a>
|
||||||
|
<br>
|
||||||
|
<a href="/register/">register</a>
|
||||||
{% endif %}
|
{% endif %}
|
||||||
</div>
|
</div>
|
||||||
</nav>
|
</nav>
|
||||||
|
|
|
@ -7,8 +7,9 @@
|
||||||
<form method="POST">
|
<form method="POST">
|
||||||
{% csrf_token %}
|
{% csrf_token %}
|
||||||
<input name="username" type="text" placeholder="your username" required></input>
|
<input name="username" type="text" placeholder="your username" required></input>
|
||||||
|
<br>
|
||||||
<input name="password" type="password" placeholder="your password" required></input>
|
<input name="password" type="password" placeholder="your password" required></input>
|
||||||
<div class="buttons login">
|
<div class="buttons">
|
||||||
<button id="login">Login</button>
|
<button id="login">Login</button>
|
||||||
</div>
|
</div>
|
||||||
<div class="error">{{ error }}</div>
|
<div class="error">{{ error }}</div>
|
||||||
|
|
|
@ -16,8 +16,14 @@ User = get_user_model()
|
||||||
|
|
||||||
@ratelimit(method="POST", block=True, rate="5/m")
|
@ratelimit(method="POST", block=True, rate="5/m")
|
||||||
def register(request):
|
def register(request):
|
||||||
|
context = default_context(request)
|
||||||
response = {}
|
response = {}
|
||||||
|
request_type = 'json'
|
||||||
if request.method == "POST":
|
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)
|
data = json.loads(request.body)
|
||||||
if User.objects.filter(username__iexact=data['username']).exists():
|
if User.objects.filter(username__iexact=data['username']).exists():
|
||||||
response['error'] = 'username not allowed'
|
response['error'] = 'username not allowed'
|
||||||
|
@ -33,8 +39,16 @@ def register(request):
|
||||||
user = django.contrib.auth.authenticate(username=data['username'], password=data['password'])
|
user = django.contrib.auth.authenticate(username=data['username'], password=data['password'])
|
||||||
django.contrib.auth.login(request, user)
|
django.contrib.auth.login(request, user)
|
||||||
response['user'] = user.username
|
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)
|
return render_to_json(response)
|
||||||
else:
|
else:
|
||||||
|
if request.user.is_authenticated:
|
||||||
|
return redirect('/')
|
||||||
context = default_context(request)
|
context = default_context(request)
|
||||||
return render(request, 'register.html', context)
|
return render(request, 'register.html', context)
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue