Skip to content

Commit

Permalink
feat: add clear local storage
Browse files Browse the repository at this point in the history
  • Loading branch information
dewanakl committed Aug 16, 2024
1 parent c8da5e4 commit 0cd7d84
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 6 deletions.
2 changes: 1 addition & 1 deletion js/dashboard.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import { pagination } from './pagination.js';

document.addEventListener('DOMContentLoaded', () => {
theme.check();
comment.init();
session.init();
comment.init();
pagination.init();

window.like = like;
Expand Down
7 changes: 7 additions & 0 deletions js/session.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,13 @@ export const session = (() => {
const init = () => {
const token = session.get('token');

if (token?.split('.').length !== 3) {
storage('owns').clear();
storage('likes').clear();
storage('config').clear();
storage('comment').clear();
}

if (token?.split('.').length !== 3 || JSON.parse(atob(token.split('.')[1])).exp < ((new Date()).getTime() / 1000)) {
comment.renderLoading();
(new bootstrap.Modal('#loginModal')).show();
Expand Down
11 changes: 7 additions & 4 deletions js/storage.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
export const storage = (table) => {

if (!localStorage.getItem(table)) {
localStorage.setItem(table, JSON.stringify({}));
}

const get = (key = null) => {
const data = JSON.parse(localStorage.getItem(table));
return key ? data[String(key)] : data;
Expand All @@ -27,10 +23,17 @@ export const storage = (table) => {

const has = (key) => Object.keys(get()).includes(String(key));

const clear = () => localStorage.setItem(table, JSON.stringify({}));

if (!localStorage.getItem(table)) {
clear();
}

return {
get,
set,
unset,
has,
clear,
};
};
11 changes: 10 additions & 1 deletion js/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,15 @@ export const util = (() => {
const init = () => {
countDownDate();

const session = storage('session');
if (session.get('token')?.split('.').length === 3) {
storage('user').clear();
storage('owns').clear();
storage('likes').clear();
storage('comment').clear();
storage('tracker').clear();
}

if (storage('information').get('info')) {
document.getElementById('information')?.remove();
}
Expand All @@ -214,7 +223,7 @@ export const util = (() => {
return;
}

storage('session').set('token', token);
session.set('token', token);
request(HTTP_GET, '/api/config')
.token(token)
.then((res) => {
Expand Down

0 comments on commit 0cd7d84

Please sign in to comment.