From 6dc029345587723fd9ef8c1738edd29882b687cc Mon Sep 17 00:00:00 2001 From: starnakin Date: Wed, 29 Nov 2023 16:05:49 +0100 Subject: [PATCH] fix: connexion --- accounts/urls.py | 3 +- accounts/views/logged.py | 16 ++++++ accounts/views/logout.py | 2 +- frontend/static/js/api/client.js | 51 ++++++++++++------- frontend/static/js/index.js | 3 +- frontend/static/js/views/HomeView.js | 10 ++-- .../static/js/views/accounts/LoginView.js | 10 ++-- .../static/js/views/accounts/LogoutView.js | 13 +++++ .../static/js/views/accounts/RegisterView.js | 7 ++- 9 files changed, 88 insertions(+), 27 deletions(-) create mode 100644 accounts/views/logged.py create mode 100644 frontend/static/js/views/accounts/LogoutView.js diff --git a/accounts/urls.py b/accounts/urls.py index 7e86455..68e777a 100644 --- a/accounts/urls.py +++ b/accounts/urls.py @@ -1,11 +1,12 @@ 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 = [ path("register", register.RegisterView.as_view(), name="register"), path("login", login.LoginView.as_view(), name="login"), 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("change_password", change_password.ChangePasswordView.as_view(), name="change_password") diff --git a/accounts/views/logged.py b/accounts/views/logged.py new file mode 100644 index 0000000..3956cd2 --- /dev/null +++ b/accounts/views/logged.py @@ -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) \ No newline at end of file diff --git a/accounts/views/logout.py b/accounts/views/logout.py index 9486b5f..f64e5b1 100644 --- a/accounts/views/logout.py +++ b/accounts/views/logout.py @@ -8,6 +8,6 @@ from rest_framework.authentication import SessionAuthentication class LogoutView(APIView): permission_classes = (permissions.IsAuthenticated,) authentication_classes = (SessionAuthentication,) - def post(self, request: HttpRequest): + def get(self, request: HttpRequest): logout(request) return Response("user unlogged", status=status.HTTP_200_OK) \ No newline at end of file diff --git a/frontend/static/js/api/client.js b/frontend/static/js/api/client.js index a66fd59..b0a5439 100644 --- a/frontend/static/js/api/client.js +++ b/frontend/static/js/api/client.js @@ -1,26 +1,27 @@ 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 { constructor(url) { this._url = url; 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) @@ -29,19 +30,35 @@ class Client method: "POST", headers: { "Content-Type": "application/json", - }, + }, body: JSON.stringify(data), }); - let token = extract_token(response); - if (token != null) - this.token = token; return response; } async login(username, 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"; } } diff --git a/frontend/static/js/index.js b/frontend/static/js/index.js index 62af827..9e00432 100644 --- a/frontend/static/js/index.js +++ b/frontend/static/js/index.js @@ -6,6 +6,7 @@ import Settings from "./views/Settings.js"; import Chat from "./views/Chat.js"; import HomeView from "./views/HomeView.js"; import RegisterView from "./views/accounts/RegisterView.js"; +import LogoutView from "./views/accounts/LogoutView.js"; import { Client } from "./api/client.js"; @@ -34,6 +35,7 @@ const router = async () => { { path: "/posts/:id", view: PostView }, { path: "/settings", view: Settings }, { path: "/login", view: LoginView }, + { path: "/logout", view: LogoutView }, { path: "/register", view: RegisterView }, { path: "/chat", view: Chat }, { path: "/home", view: HomeView }, @@ -55,7 +57,6 @@ const router = async () => { result: [location.pathname] }; } - const view = new match.route.view(getParams(match)); document.querySelector("#app").innerHTML = await view.getHtml(); diff --git a/frontend/static/js/views/HomeView.js b/frontend/static/js/views/HomeView.js index 89ad19a..79880c3 100644 --- a/frontend/static/js/views/HomeView.js +++ b/frontend/static/js/views/HomeView.js @@ -4,14 +4,18 @@ import { client, navigateTo } from "../index.js"; export default class extends AbstractView { constructor(params) { super(params); - if (client.isAuthentificate() == false) - navigateTo("/home"); - this.setTitle("register"); + this.setTitle("Home"); } async getHtml() { + if (await client.isAuthentificate() === false) + { + navigateTo("/login"); + return; + } return `

HOME

+ Logout `; } } \ No newline at end of file diff --git a/frontend/static/js/views/accounts/LoginView.js b/frontend/static/js/views/accounts/LoginView.js index e545358..b9a6621 100644 --- a/frontend/static/js/views/accounts/LoginView.js +++ b/frontend/static/js/views/accounts/LoginView.js @@ -6,10 +6,9 @@ async function login() let username = document.getElementById("username").value; let password = document.getElementById("password").value; - let response = await client.login(username, password); - let response_data = await response.json(); + let response_data = await client.login(username, password); - if (response_data == "user connected") + if (response_data == null) { navigateTo("/home"); return; @@ -36,6 +35,11 @@ export default class extends AbstractView { async postInit() { + if (await client.isAuthentificate()) + { + navigateTo("/home") + return; + } document.getElementById("button").onclick = login; } diff --git a/frontend/static/js/views/accounts/LogoutView.js b/frontend/static/js/views/accounts/LogoutView.js new file mode 100644 index 0000000..0df2227 --- /dev/null +++ b/frontend/static/js/views/accounts/LogoutView.js @@ -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") + } +} \ No newline at end of file diff --git a/frontend/static/js/views/accounts/RegisterView.js b/frontend/static/js/views/accounts/RegisterView.js index e5c7c1f..1616050 100644 --- a/frontend/static/js/views/accounts/RegisterView.js +++ b/frontend/static/js/views/accounts/RegisterView.js @@ -1,5 +1,5 @@ import AbstractView from "../AbstractView.js"; -import { client } from "../../index.js"; +import { client, navigateTo } from "../../index.js"; async function register() { @@ -30,6 +30,11 @@ export default class extends AbstractView { async postInit() { + if (client.isAuthentificate()) + { + navigateTo("/home") + return; + } document.getElementById("button").onclick = register; }