Skip to content

Commit

Permalink
refactor: session token handling
Browse files Browse the repository at this point in the history
  • Loading branch information
dewanakl committed Dec 31, 2024
1 parent 9ae6267 commit 9564c64
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 21 deletions.
33 changes: 17 additions & 16 deletions js/guest.js
Original file line number Diff line number Diff line change
Expand Up @@ -214,42 +214,43 @@ export const guest = (() => {
storage('tracker').clear();
}

const presence = information.get('presence');
if (presence !== undefined) {
document.getElementById('form-presence').value = presence ? '1' : '2';
if (information.has('presence')) {
document.getElementById('form-presence').value = information.get('presence') ? '1' : '2';
}

const info = document.getElementById('information');
if (info && information.get('info')) {
info.remove();
}

if (document.body.getAttribute('data-key')?.length === 0) {
const token = document.body.getAttribute('data-key');
if (!token || token.length === 0) {
progress.init();
document.getElementById('comment')?.remove();
document.querySelector('a.nav-link[href="#comment"]')?.closest('li.nav-item')?.remove();
progress.init();
return;
}

progress.add();
progress.add();
progress.init();
if (token.length > 0) {
// add 2 progress for config and comment.
progress.add();
progress.add();
progress.init();

session.guest()
.then((res) => {
session.setToken(token);
session.guest().then((res) => {
if (res.code !== 200) {
progress.invalid('request');
return res;
progress.invalid('config');
return;
}

progress.complete('request');
progress.complete('config');

comment.init();
comment.comment()
.then(() => progress.complete('comment'))
.catch(() => progress.invalid('comment'));
})
.catch(() => progress.invalid('request'));
}).catch(() => progress.invalid('config'));
}
};

return {
Expand Down
8 changes: 3 additions & 5 deletions js/session.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,6 @@ export const session = (() => {
* @returns {Promise<ReturnType<typeof dto.baseResponse<object>>}
*/
const guest = () => {
const config = storage('config');

return request(HTTP_GET, '/api/config')
.token(document.body.getAttribute('data-key'))
.send()
Expand All @@ -64,11 +62,11 @@ export const session = (() => {
return res;
}

for (let [key, value] of Object.entries(res.data)) {
config.set(key, value);
const config = storage('config');
for (let [k, v] of Object.entries(res.data)) {
config.set(k, v);
}

setToken(document.body.getAttribute('data-key'));
return res;
});
};
Expand Down

0 comments on commit 9564c64

Please sign in to comment.