fix: connexion
This commit is contained in:
parent
b5b54a98ba
commit
6dc0293455
@ -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")
|
||||
|
||||
|
16
accounts/views/logged.py
Normal file
16
accounts/views/logged.py
Normal 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)
|
@ -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)
|
@ -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)
|
||||
@ -32,16 +33,32 @@ class Client
|
||||
},
|
||||
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";
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -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();
|
||||
|
@ -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 `
|
||||
<h1>HOME</h1>
|
||||
<a href="/logout" class="nav__link" data-link>Logout</a>
|
||||
`;
|
||||
}
|
||||
}
|
@ -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;
|
||||
}
|
||||
|
||||
|
13
frontend/static/js/views/accounts/LogoutView.js
Normal file
13
frontend/static/js/views/accounts/LogoutView.js
Normal 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")
|
||||
}
|
||||
}
|
@ -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;
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user