diff --git a/accounts/views/login.py b/accounts/views/login.py index 5987583..fc02e4a 100644 --- a/accounts/views/login.py +++ b/accounts/views/login.py @@ -18,6 +18,6 @@ class LoginView(APIView): serializer.is_valid(raise_exception=True) user = serializer.get_user(data) if user is None: - return Response({'error': ['Username or password wrong.']}, status.HTTP_400_BAD_REQUEST) + return Response({'login': ['Invalid username or password.']}, status.HTTP_400_BAD_REQUEST) login(request, user) return Response({'id': user.pk}, status=status.HTTP_200_OK) diff --git a/frontend/static/js/utils/formUtils.js b/frontend/static/js/utils/formUtils.js index 8b00592..54c0717 100644 --- a/frontend/static/js/utils/formUtils.js +++ b/frontend/static/js/utils/formUtils.js @@ -1,4 +1,4 @@ -function clear(property_name, elements_id) +export function clear(property_name, elements_id) { elements_id.forEach(element_id => { let element = document.getElementById(element_id) @@ -6,7 +6,7 @@ function clear(property_name, elements_id) }); } -function fill_errors(errors, property_name) +export function fill_errors(errors, property_name) { Object.keys(errors).forEach(error_field => { @@ -14,5 +14,3 @@ function fill_errors(errors, property_name) element[property_name] = errors[error_field]; }); } - -export {fill_errors, clear} \ No newline at end of file diff --git a/frontend/static/js/views/accounts/LoginView.js b/frontend/static/js/views/accounts/LoginView.js index de8c1b2..6f2042a 100644 --- a/frontend/static/js/views/accounts/LoginView.js +++ b/frontend/static/js/views/accounts/LoginView.js @@ -4,9 +4,20 @@ import AbstractNonAuthentifiedView from "../abstracts/AbstractNonAuthentified.js async function login() { - let username = document.getElementById("username-input").value; - let password = document.getElementById("password-input").value; - + clear('innerHTML', ['username', 'password', 'login']); + + let username = document.getElementById('usernameInput').value; + let password = document.getElementById('passwordInput').value; + + if (username === '') { + document.getElementById('username').innerHTML = 'This field may not be blank.'; + } + if (password === '') { + document.getElementById('password').innerHTML = 'This field may not be blank.'; + } + if (username === '' || password === '') + return; + let response = await client.login(username, password); if (response.status == 200) { @@ -26,31 +37,42 @@ export default class extends AbstractNonAuthentifiedView { async postInit() { - let usernameField = document.getElementById('username-input'); + let usernameField = document.getElementById('usernameInput'); usernameField.addEventListener('keydown', ev => { if (ev.key === 'Enter') login(); }); usernameField.focus(); - let passwordField = document.getElementById('password-input'); + let passwordField = document.getElementById('passwordInput'); passwordField.addEventListener('keydown', ev => { if (ev.key === 'Enter') login(); }); - document.getElementById("login-button").onclick = login; + document.getElementById('loginButton').onclick = login; } async getHtml() { - return ` -