fix, add auto login when register
This commit is contained in:
parent
25f315c24f
commit
910644a804
@ -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)
|
@ -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;
|
||||||
|
@ -4,8 +4,8 @@ function getCookie(name)
|
|||||||
{
|
{
|
||||||
let cookie = {};
|
let cookie = {};
|
||||||
document.cookie.split(';').forEach(function(el) {
|
document.cookie.split(';').forEach(function(el) {
|
||||||
let split = el.split('=');
|
let split = el.split('=');
|
||||||
cookie[split[0].trim()] = split.slice(1).join("=");
|
cookie[split[0].trim()] = split.slice(1).join("=");
|
||||||
})
|
})
|
||||||
return cookie[name];
|
return cookie[name];
|
||||||
}
|
}
|
||||||
|
@ -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 {
|
||||||
|
@ -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;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -63,12 +68,17 @@ export default class extends AbstractAuthentificateView
|
|||||||
if (new_password.length != 0)
|
if (new_password.length != 0)
|
||||||
data.new_password = new_password;
|
data.new_password = new_password;
|
||||||
let response_data = await client.account.update(data, current_password);
|
let response_data = await client.account.update(data, current_password);
|
||||||
|
|
||||||
if (response_data === null)
|
if (response_data === null)
|
||||||
{
|
{
|
||||||
navigateTo(super.redirect_url);
|
navigateTo("/login");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
else if (response_data === "data has been alterate")
|
||||||
|
{
|
||||||
|
navigateTo("/me");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
["username", "new_password", "current_password"].forEach(error_field => {
|
["username", "new_password", "current_password"].forEach(error_field => {
|
||||||
let error_display = document.getElementById(`error_${error_field}`);
|
let error_display = document.getElementById(`error_${error_field}`);
|
||||||
|
@ -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");
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user