From 4cb15e8854acfa0a03e82c5fa6032404dd7e2333 Mon Sep 17 00:00:00 2001 From: Daniil Mironenko Date: Sun, 29 Sep 2024 13:40:08 +0300 Subject: [PATCH] add function to remove current user from local storage --- src/components/header/header.js | 4 ++-- src/modules/user.js | 7 +++++++ 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/src/components/header/header.js b/src/components/header/header.js index e1b95fb..f10a6b9 100644 --- a/src/components/header/header.js +++ b/src/components/header/header.js @@ -1,6 +1,6 @@ import { View } from "../../view.js"; import { Ajax } from "../../modules/ajax.js"; -import { getCurrentUser } from "../../modules/user.js"; +import { getCurrentUser, removeCurrentUser } from "../../modules/user.js"; import { API_URL } from "../../app/config.js"; export class HeaderView extends View { @@ -92,7 +92,7 @@ export class HeaderView extends View { const response = await Ajax.post(url); if (response.status === 200) { - localStorage.removeItem("user"); + removeCurrentUser(); this.router.renderLayout(); } else { console.error("logout failed:", response.body); diff --git a/src/modules/user.js b/src/modules/user.js index b1c90f8..abbf2b3 100644 --- a/src/modules/user.js +++ b/src/modules/user.js @@ -16,3 +16,10 @@ export const getCurrentUser = () => { export const setCurrentUser = (user) => { localStorage.setItem("user", JSON.stringify(user)); }; + +/** + * Removes current user from local storage + */ +export const removeCurrentUser = () => { + localStorage.removeItem("user"); +};