fix, add auto login when register

This commit is contained in:
starnakin 2023-12-04 12:32:15 +01:00
parent 25f315c24f
commit 910644a804
6 changed files with 29 additions and 11 deletions

View File

@ -3,6 +3,7 @@ from ..serializers.register import RegisterSerialiser
from rest_framework.views import APIView from rest_framework.views import APIView
from rest_framework.response import Response from rest_framework.response import Response
from django.http import HttpRequest from django.http import HttpRequest
from django.contrib.auth import login
class RegisterView(APIView): class RegisterView(APIView):
permission_classes = (permissions.AllowAny,) permission_classes = (permissions.AllowAny,)
@ -12,5 +13,6 @@ class RegisterView(APIView):
if serializer.is_valid(raise_exception=True): if serializer.is_valid(raise_exception=True):
user = serializer.create(data) user = serializer.create(data)
if user: if user:
login(request, user)
return Response("user created", status=status.HTTP_201_CREATED) return Response("user created", status=status.HTTP_201_CREATED)
return Response(status=status.HTTP_400_BAD_REQUEST) return Response(status=status.HTTP_400_BAD_REQUEST)

View File

@ -23,7 +23,13 @@ class Account
let response = await this.client._delete("/api/accounts/delete", {password: password}); let response = await this.client._delete("/api/accounts/delete", {password: password});
let response_data = await response.json(); 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; this.client._logged = false;
return response_data; return response_data;
} }
@ -35,7 +41,7 @@ class Account
if (JSON.stringify(response_data) == JSON.stringify({'detail': 'Authentication credentials were not provided.'})) 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 null;
} }
return response_data; return response_data;
@ -43,13 +49,13 @@ class Account
async update(data, password) async update(data, password)
{ {
data.password = password; data.current_password = password;
let response = await this.client._patch_json("/api/accounts/edit", data); let response = await this.client._patch_json("/api/accounts/edit", data);
let response_data = await response.json(); let response_data = await response.json();
if (JSON.stringify(response_data) == JSON.stringify({'detail': 'Authentication credentials were not provided.'})) 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 null;
} }
return response_data; return response_data;

View File

@ -1,3 +1,4 @@
import { client } from "../index.js";
import AbstractAuthentificateView from "./AbstractAuthentifiedView.js"; import AbstractAuthentificateView from "./AbstractAuthentifiedView.js";
export default class extends AbstractAuthentificateView { export default class extends AbstractAuthentificateView {

View File

@ -20,6 +20,11 @@ export default class extends AbstractAuthentificateView
{ {
let data = await client.account.get(); let data = await client.account.get();
if (data === null)
{
navigateTo("/login")
return;
}
document.getElementById("username").value = data.username; document.getElementById("username").value = data.username;
} }
@ -66,7 +71,12 @@ export default class extends AbstractAuthentificateView
if (response_data === null) if (response_data === null)
{ {
navigateTo(super.redirect_url); navigateTo("/login");
return;
}
else if (response_data === "data has been alterate")
{
navigateTo("/me");
return; return;
} }

View File

@ -1,5 +1,5 @@
import { client, navigateTo } from "../../index.js"; import { client, navigateTo } from "../../index.js";
import AbstractAuthentifiedView from "../AbstractNonAuthentified.js"; import AbstractNonAuthentifiedView from "../AbstractNonAuthentified.js";
async function register() async function register()
{ {
@ -14,7 +14,6 @@ async function register()
return; return;
} }
["username", "user", "password"].forEach(error_field => { ["username", "user", "password"].forEach(error_field => {
let error_display = document.getElementById(`error_${error_field}`); let error_display = document.getElementById(`error_${error_field}`);
if (error_display != null) if (error_display != null)
@ -28,7 +27,7 @@ async function register()
}); });
} }
export default class extends AbstractAuthentifiedView { export default class extends AbstractNonAuthentifiedView {
constructor(params) { constructor(params) {
super(params, "Register", "/home"); super(params, "Register", "/home");
} }