core: simplified login and register, fix: me

This commit is contained in:
starnakin 2023-12-16 17:41:31 +01:00
parent 1f41e62a86
commit b5d73e59fd
3 changed files with 24 additions and 38 deletions

View File

@ -1,10 +1,11 @@
import { client, navigateTo } from "../../index.js";
import { clear, fill_errors } from "../../utils/formUtils.js";
import AbstractNonAuthentifiedView from "../AbstractNonAuthentified.js";
async function login()
{
let username = document.getElementById("username").value;
let password = document.getElementById("password").value;
let username = document.getElementById("username-input").value;
let password = document.getElementById("password-input").value;
let response_data = await client.login(username, password);
@ -14,17 +15,8 @@ async function login()
return;
}
["username", "user", "password"].forEach(error_field => {
let error_display = document.getElementById(`error_${error_field}`);
if (error_display != null)
error_display.innerHTML = "";
});
Object.keys(response_data).forEach(error_field => {
let error_display = document.getElementById(`error_${error_field}`);
if (error_display != null)
error_display.innerHTML = response_data[error_field];
});
clear("innerHTML", ["username", "user", "password"]);
fill_errors(response_data, "innerHTML");
}
export default class extends AbstractNonAuthentifiedView {
@ -34,7 +26,7 @@ export default class extends AbstractNonAuthentifiedView {
async postInit()
{
document.getElementById("button").onclick = login;
document.getElementById("login-button").onclick = login;
}
async getHtml() {
@ -42,12 +34,12 @@ export default class extends AbstractNonAuthentifiedView {
<div class=form>
<label>Login</label>
<link rel="stylesheet" href="/static/css/accounts/login.css">
<input type="text" id="username" placeholder="username">
<span id="error_username"></span>
<input type="password" id="password" placeholder="password">
<span id="error_password"></span>
<input type="button" value="login" id="button">
<span id="error_user"></span>
<input type="text" id="username-input" placeholder="username">
<span id="username"></span>
<input type="password" id="password-input" placeholder="password">
<span id="password"></span>
<input type="button" value="Login" id="login-button">
<span id="user"></span>
<a href="/register" class="nav__link" data-link>Register</a>
</div>
`;

View File

@ -14,17 +14,8 @@ async function register()
return;
}
["username", "user", "password"].forEach(error_field => {
let error_display = document.getElementById(`error_${error_field}`);
if (error_display != null)
error_display.innerHTML = "";
});
Object.keys(response_data).forEach(error_field => {
let error_display = document.getElementById(`error_${error_field}`);
if (error_display != null)
error_display.innerHTML = response_data[error_field];
});
clear("innerHTML", ["username", "user", "password"]);
fill_errors(response_data, "innerHTML");
}
export default class extends AbstractNonAuthentifiedView {
@ -34,7 +25,7 @@ export default class extends AbstractNonAuthentifiedView {
async postInit()
{
document.getElementById("button").onclick = register;
document.getElementById("register-button").onclick = register;
}
async getHtml() {
@ -42,12 +33,12 @@ export default class extends AbstractNonAuthentifiedView {
<div class=form>
<label>Register</label>
<link rel="stylesheet" href="/static/css/accounts/register.css">
<input type="text" id="username" placeholder="username">
<span id="error_username"></span>
<input type="password" id="password" placeholder="password">
<span id="error_password"></span>
<input type="button" value="register" id="button">
<span id="error_user"></span>
<input type="text" id="username-input" placeholder="username">
<span id="username"></span>
<input type="password" id="password-input" placeholder="password">
<span id="password"></span>
<input type="button" value="Register" id="register-button">
<span id="user"></span>
<a href="/login" class="nav__link" data-link>Login</a>
</div>
`;

View File

@ -3,6 +3,7 @@ from rest_framework.parsers import MultiPartParser, FormParser
from rest_framework import permissions, status
from rest_framework import viewsets
from rest_framework.response import Response
from rest_framework.authentication import SessionAuthentication
from django.http import HttpRequest
from django.db.models import QuerySet
@ -33,6 +34,8 @@ class ProfileViewSet(viewsets.ModelViewSet):
class MyProfileViewSet(viewsets.ModelViewSet):
permission_classes = (permissions.IsAuthenticated,)
authentication_classes = (SessionAuthentication,)
serializer_class = ProfileSerializer
queryset = ProfileModel.objects.all