From 910644a8043dfd8f59ea08593a535897dfecc30a Mon Sep 17 00:00:00 2001 From: starnakin Date: Mon, 4 Dec 2023 12:32:15 +0100 Subject: [PATCH] fix, add auto login when register --- accounts/views/register.py | 2 ++ frontend/static/js/api/account.js | 14 ++++++++++---- frontend/static/js/api/client.js | 4 ++-- frontend/static/js/views/HomeView.js | 1 + frontend/static/js/views/MeView.js | 14 ++++++++++++-- frontend/static/js/views/accounts/RegisterView.js | 5 ++--- 6 files changed, 29 insertions(+), 11 deletions(-) diff --git a/accounts/views/register.py b/accounts/views/register.py index 5bdacc2..ee5724a 100644 --- a/accounts/views/register.py +++ b/accounts/views/register.py @@ -3,6 +3,7 @@ from ..serializers.register import RegisterSerialiser from rest_framework.views import APIView from rest_framework.response import Response from django.http import HttpRequest +from django.contrib.auth import login class RegisterView(APIView): permission_classes = (permissions.AllowAny,) @@ -12,5 +13,6 @@ class RegisterView(APIView): if serializer.is_valid(raise_exception=True): user = serializer.create(data) if user: + login(request, user) return Response("user created", status=status.HTTP_201_CREATED) return Response(status=status.HTTP_400_BAD_REQUEST) \ No newline at end of file diff --git a/frontend/static/js/api/account.js b/frontend/static/js/api/account.js index d2b590a..1004c1c 100644 --- a/frontend/static/js/api/account.js +++ b/frontend/static/js/api/account.js @@ -23,7 +23,13 @@ class Account let response = await this.client._delete("/api/accounts/delete", {password: password}); let response_data = await response.json(); - if (response_data === "user deleted") + if (JSON.stringify(response_data) == JSON.stringify({'detail': 'Authentication credentials were not provided.'})) + { + this.client._logged = false; + return null; + } + console.log(response_data) + if (response_data == "user deleted") this.client._logged = false; return response_data; } @@ -35,7 +41,7 @@ class Account if (JSON.stringify(response_data) == JSON.stringify({'detail': 'Authentication credentials were not provided.'})) { - console.log("error, client is not logged"); + this.client._logged = false; return null; } return response_data; @@ -43,13 +49,13 @@ class Account async update(data, password) { - data.password = password; + data.current_password = password; let response = await this.client._patch_json("/api/accounts/edit", data); let response_data = await response.json(); if (JSON.stringify(response_data) == JSON.stringify({'detail': 'Authentication credentials were not provided.'})) { - console.log("error, client is not logged"); + this.client._logged = false; return null; } return response_data; diff --git a/frontend/static/js/api/client.js b/frontend/static/js/api/client.js index fa0d94d..4469896 100644 --- a/frontend/static/js/api/client.js +++ b/frontend/static/js/api/client.js @@ -4,8 +4,8 @@ function getCookie(name) { let cookie = {}; document.cookie.split(';').forEach(function(el) { - let split = el.split('='); - cookie[split[0].trim()] = split.slice(1).join("="); + let split = el.split('='); + cookie[split[0].trim()] = split.slice(1).join("="); }) return cookie[name]; } diff --git a/frontend/static/js/views/HomeView.js b/frontend/static/js/views/HomeView.js index d9fd194..1bd431e 100644 --- a/frontend/static/js/views/HomeView.js +++ b/frontend/static/js/views/HomeView.js @@ -1,3 +1,4 @@ +import { client } from "../index.js"; import AbstractAuthentificateView from "./AbstractAuthentifiedView.js"; export default class extends AbstractAuthentificateView { diff --git a/frontend/static/js/views/MeView.js b/frontend/static/js/views/MeView.js index d20ce8c..ce5c600 100644 --- a/frontend/static/js/views/MeView.js +++ b/frontend/static/js/views/MeView.js @@ -20,6 +20,11 @@ export default class extends AbstractAuthentificateView { let data = await client.account.get(); + if (data === null) + { + navigateTo("/login") + return; + } document.getElementById("username").value = data.username; } @@ -63,12 +68,17 @@ export default class extends AbstractAuthentificateView if (new_password.length != 0) data.new_password = new_password; let response_data = await client.account.update(data, current_password); - + if (response_data === null) { - navigateTo(super.redirect_url); + navigateTo("/login"); return; } + else if (response_data === "data has been alterate") + { + navigateTo("/me"); + return; + } ["username", "new_password", "current_password"].forEach(error_field => { let error_display = document.getElementById(`error_${error_field}`); diff --git a/frontend/static/js/views/accounts/RegisterView.js b/frontend/static/js/views/accounts/RegisterView.js index 444149f..7e4aae6 100644 --- a/frontend/static/js/views/accounts/RegisterView.js +++ b/frontend/static/js/views/accounts/RegisterView.js @@ -1,5 +1,5 @@ import { client, navigateTo } from "../../index.js"; -import AbstractAuthentifiedView from "../AbstractNonAuthentified.js"; +import AbstractNonAuthentifiedView from "../AbstractNonAuthentified.js"; async function register() { @@ -14,7 +14,6 @@ async function register() return; } - ["username", "user", "password"].forEach(error_field => { let error_display = document.getElementById(`error_${error_field}`); if (error_display != null) @@ -28,7 +27,7 @@ async function register() }); } -export default class extends AbstractAuthentifiedView { +export default class extends AbstractNonAuthentifiedView { constructor(params) { super(params, "Register", "/home"); }