Skip to content

Commit

Permalink
add login page
Browse files Browse the repository at this point in the history
  • Loading branch information
antoineverin committed Aug 29, 2024
1 parent b88136c commit 07201a0
Showing 1 changed file with 60 additions and 0 deletions.
60 changes: 60 additions & 0 deletions django/src/rendering/templates/login.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<h1>Login</h1>

<form>
{% csrf_token %}
<div>
<label for="username">Username</label>
<input id="username" type="text" autocomplete="username" />
</div>
<div>
<label for="password">Password</label>
<input id="password" type="password" />
</div>
<button id="login-btn">Login</button>
</form>

<script>
function getInput(id) {
return document.getElementById(id).value;
}

function getCookie(name) {
let cookieValue = null;
if (document.cookie && document.cookie !== '') {
const cookies = document.cookie.split(';');
for (let i = 0; i < cookies.length; i++) {
const cookie = cookies[i].trim();
if (cookie.substring(0, name.length + 1) === (name + '=')) {
cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
break;
}
}
}
return cookieValue;
}

document.getElementById('login-btn').onclick = function(e) {
e.preventDefault();
var username = getInput('username');
var password = getInput('password');
var csrf = getCookie('csrftoken');

fetch('/api/auth/login/', {
method: 'POST',
headers: {
'X-CSRFToken': csrf,
'Content-Type': 'application/json',
},
mode: 'same-origin',
body: JSON.stringify({
username: username,
password: password,
}),
}).then(r => {
if (r.status === 200)
return ; // api call ok
else
return ; // api call error
}).catch(e => console.error(e));
};
</script>

0 comments on commit 07201a0

Please sign in to comment.