fix: connexion

This commit is contained in:
starnakin 2023-11-29 16:05:49 +01:00
parent b5b54a98ba
commit 6dc0293455
9 changed files with 88 additions and 27 deletions

View File

@ -1,11 +1,12 @@
from django.urls import path from django.urls import path
from .views import register, login, logout, delete, change_password from .views import register, login, logout, delete, change_password, logged
urlpatterns = [ urlpatterns = [
path("register", register.RegisterView.as_view(), name="register"), path("register", register.RegisterView.as_view(), name="register"),
path("login", login.LoginView.as_view(), name="login"), path("login", login.LoginView.as_view(), name="login"),
path("logout", logout.LogoutView.as_view(), name="logout"), path("logout", logout.LogoutView.as_view(), name="logout"),
path("logged", logged.LoggedView.as_view(), name="logged"),
path("delete", delete.DeleteView.as_view(), name="delete"), path("delete", delete.DeleteView.as_view(), name="delete"),
path("change_password", change_password.ChangePasswordView.as_view(), name="change_password") path("change_password", change_password.ChangePasswordView.as_view(), name="change_password")

16
accounts/views/logged.py Normal file
View File

@ -0,0 +1,16 @@
from rest_framework.views import APIView
from rest_framework.response import Response
from rest_framework import permissions, status
from django.http import HttpRequest
from django.contrib.auth import login
from rest_framework.authentication import SessionAuthentication
from ..serializers.login import LoginSerializer
class LoggedView(APIView):
permission_classes = (permissions.AllowAny,)
authentication_classes = (SessionAuthentication,)
def get(self, request: HttpRequest):
return Response(str(request.user.is_authenticated), status=status.HTTP_200_OK)

View File

@ -8,6 +8,6 @@ from rest_framework.authentication import SessionAuthentication
class LogoutView(APIView): class LogoutView(APIView):
permission_classes = (permissions.IsAuthenticated,) permission_classes = (permissions.IsAuthenticated,)
authentication_classes = (SessionAuthentication,) authentication_classes = (SessionAuthentication,)
def post(self, request: HttpRequest): def get(self, request: HttpRequest):
logout(request) logout(request)
return Response("user unlogged", status=status.HTTP_200_OK) return Response("user unlogged", status=status.HTTP_200_OK)

View File

@ -1,26 +1,27 @@
import { Accounts } from "./accounts.js"; import { Accounts } from "./accounts.js";
function extract_token(response)
{
let cookies = response.headers.get("set-cookie");
if (cookies == null)
return null;
let token = cookies.slice(cookies.indexOf("=") + 1, cookies.indexOf(';'))
return token;
}
class Client class Client
{ {
constructor(url) constructor(url)
{ {
this._url = url; this._url = url;
this.accounts = new Accounts(this); this.accounts = new Accounts(this);
this._token = undefined; this._logged = undefined;
} }
isAuthentificate() async isAuthentificate()
{ {
return this.token != undefined; if (this._logged == undefined)
this.logged = await this._test_logged();
return this.logged;
}
async _get(uri)
{
let response = await fetch(this._url + uri, {
method: "GET",
});
return response;
} }
async _post(uri, data) async _post(uri, data)
@ -29,19 +30,35 @@ class Client
method: "POST", method: "POST",
headers: { headers: {
"Content-Type": "application/json", "Content-Type": "application/json",
}, },
body: JSON.stringify(data), body: JSON.stringify(data),
}); });
let token = extract_token(response);
if (token != null)
this.token = token;
return response; return response;
} }
async login(username, password) async login(username, password)
{ {
let response = await this._post("/api/accounts/login", {username: username, password: password}) let response = await this._post("/api/accounts/login", {username: username, password: password})
return response let data = await response.json();
if (data == "user connected")
{
this.logged = true;
return null;
}
return data;
}
async logout()
{
await this._get("/api/accounts/logout");
this.logged = false;
}
async _test_logged()
{
let response = await this._get("/api/accounts/logged");
let data = await response.json();
return data === "True";
} }
} }

View File

@ -6,6 +6,7 @@ import Settings from "./views/Settings.js";
import Chat from "./views/Chat.js"; import Chat from "./views/Chat.js";
import HomeView from "./views/HomeView.js"; import HomeView from "./views/HomeView.js";
import RegisterView from "./views/accounts/RegisterView.js"; import RegisterView from "./views/accounts/RegisterView.js";
import LogoutView from "./views/accounts/LogoutView.js";
import { Client } from "./api/client.js"; import { Client } from "./api/client.js";
@ -34,6 +35,7 @@ const router = async () => {
{ path: "/posts/:id", view: PostView }, { path: "/posts/:id", view: PostView },
{ path: "/settings", view: Settings }, { path: "/settings", view: Settings },
{ path: "/login", view: LoginView }, { path: "/login", view: LoginView },
{ path: "/logout", view: LogoutView },
{ path: "/register", view: RegisterView }, { path: "/register", view: RegisterView },
{ path: "/chat", view: Chat }, { path: "/chat", view: Chat },
{ path: "/home", view: HomeView }, { path: "/home", view: HomeView },
@ -55,7 +57,6 @@ const router = async () => {
result: [location.pathname] result: [location.pathname]
}; };
} }
const view = new match.route.view(getParams(match)); const view = new match.route.view(getParams(match));
document.querySelector("#app").innerHTML = await view.getHtml(); document.querySelector("#app").innerHTML = await view.getHtml();

View File

@ -4,14 +4,18 @@ import { client, navigateTo } from "../index.js";
export default class extends AbstractView { export default class extends AbstractView {
constructor(params) { constructor(params) {
super(params); super(params);
if (client.isAuthentificate() == false) this.setTitle("Home");
navigateTo("/home");
this.setTitle("register");
} }
async getHtml() { async getHtml() {
if (await client.isAuthentificate() === false)
{
navigateTo("/login");
return;
}
return ` return `
<h1>HOME</h1> <h1>HOME</h1>
<a href="/logout" class="nav__link" data-link>Logout</a>
`; `;
} }
} }

View File

@ -6,10 +6,9 @@ async function login()
let username = document.getElementById("username").value; let username = document.getElementById("username").value;
let password = document.getElementById("password").value; let password = document.getElementById("password").value;
let response = await client.login(username, password); let response_data = await client.login(username, password);
let response_data = await response.json();
if (response_data == "user connected") if (response_data == null)
{ {
navigateTo("/home"); navigateTo("/home");
return; return;
@ -36,6 +35,11 @@ export default class extends AbstractView {
async postInit() async postInit()
{ {
if (await client.isAuthentificate())
{
navigateTo("/home")
return;
}
document.getElementById("button").onclick = login; document.getElementById("button").onclick = login;
} }

View File

@ -0,0 +1,13 @@
import { client, navigateTo } from "../../index.js";
import AbstractView from "../AbstractView.js";
export default class extends AbstractView
{
constructor(params) {
super(params);
this.setTitle("Logout");
if (client.logged)
client.logout();
navigateTo("/login")
}
}

View File

@ -1,5 +1,5 @@
import AbstractView from "../AbstractView.js"; import AbstractView from "../AbstractView.js";
import { client } from "../../index.js"; import { client, navigateTo } from "../../index.js";
async function register() async function register()
{ {
@ -30,6 +30,11 @@ export default class extends AbstractView {
async postInit() async postInit()
{ {
if (client.isAuthentificate())
{
navigateTo("/home")
return;
}
document.getElementById("button").onclick = register; document.getElementById("button").onclick = register;
} }