-
-
Notifications
You must be signed in to change notification settings - Fork 260
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
118 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,67 +1,108 @@ | ||
import { dto } from './dto.js'; | ||
import { storage } from './storage.js'; | ||
import { progress } from './progress.js'; | ||
import { request, HTTP_POST, HTTP_GET } from './request.js'; | ||
|
||
export const session = (() => { | ||
|
||
let session = null; | ||
/** | ||
* @type {ReturnType<typeof storage>} | ||
*/ | ||
let ses = null; | ||
|
||
const getToken = () => session.get('token'); | ||
/** | ||
* @returns {string} | ||
*/ | ||
const getToken = () => ses.get('token'); | ||
|
||
/** | ||
* @param {object} body | ||
* @returns {Promise<boolean>} | ||
*/ | ||
const login = (body) => { | ||
return request(HTTP_POST, '/api/session') | ||
.body(body) | ||
.send(dto.tokenResponse) | ||
.then((res) => { | ||
if (res.code === 200) { | ||
session.set('token', res.data.token); | ||
} | ||
.then( | ||
(res) => { | ||
if (res.code === 200) { | ||
setToken(res.data.token); | ||
} | ||
|
||
return res; | ||
}) | ||
.then((res) => res.code === 200, () => false); | ||
return res.code === 200; | ||
}, | ||
() => false | ||
); | ||
}; | ||
|
||
const logout = () => session.unset('token'); | ||
/** | ||
* @returns {void} | ||
*/ | ||
const logout = () => ses.unset('token'); | ||
|
||
const isAdmin = () => String(getToken() ?? '.').split('.').length === 3; | ||
/** | ||
* @param {string} token | ||
* @returns {void} | ||
*/ | ||
const setToken = (token) => ses.set('token', token); | ||
|
||
/** | ||
* @returns {boolean} | ||
*/ | ||
const isAdmin = () => getToken().split('.').length === 3; | ||
|
||
/** | ||
* @returns {Promise<ReturnType<typeof dto.baseResponse<object>>} | ||
*/ | ||
const guest = () => { | ||
progress.add(); | ||
const config = storage('config'); | ||
|
||
return request(HTTP_GET, '/api/config') | ||
.token(document.body.getAttribute('data-key')) | ||
.send() | ||
.then((res) => { | ||
if (res.code !== 200) { | ||
progress.invalid('request'); | ||
return res; | ||
} | ||
|
||
const config = storage('config'); | ||
for (let [key, value] of Object.entries(res.data)) { | ||
config.set(key, value); | ||
} | ||
|
||
session.set('token', document.body.getAttribute('data-key')); | ||
progress.complete('request'); | ||
|
||
setToken(document.body.getAttribute('data-key')); | ||
return res; | ||
}).catch(() => { | ||
progress.invalid('request'); | ||
}); | ||
}; | ||
|
||
/** | ||
* @returns {object|null} | ||
*/ | ||
const decode = () => { | ||
if (!isAdmin()) { | ||
return null; | ||
} | ||
|
||
try { | ||
return JSON.parse(window.atob(getToken().split('.')[1])); | ||
} catch { | ||
return null; | ||
} | ||
}; | ||
|
||
/** | ||
* @returns {void} | ||
*/ | ||
const init = () => { | ||
session = storage('session'); | ||
ses = storage('session'); | ||
}; | ||
|
||
return { | ||
init, | ||
guest, | ||
login, | ||
logout, | ||
decode, | ||
isAdmin, | ||
setToken, | ||
getToken, | ||
}; | ||
})(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,39 +1,59 @@ | ||
export const storage = (table) => { | ||
|
||
/** | ||
* @param {string=} key | ||
* @returns {any} | ||
*/ | ||
const get = (key = null) => { | ||
const data = JSON.parse(localStorage.getItem(table)); | ||
return key ? data[String(key)] : data; | ||
}; | ||
|
||
/** | ||
* @param {string} key | ||
* @param {any} value | ||
* @returns {void} | ||
*/ | ||
const set = (key, value) => { | ||
let storage = get(); | ||
storage[String(key)] = value; | ||
localStorage.setItem(table, JSON.stringify(storage)); | ||
let data = get(); | ||
data[String(key)] = value; | ||
localStorage.setItem(table, JSON.stringify(data)); | ||
}; | ||
|
||
/** | ||
* @param {string} key | ||
* @returns {boolean} | ||
*/ | ||
const has = (key) => Object.keys(get()).includes(String(key)); | ||
|
||
/** | ||
* @param {string} key | ||
* @returns {void} | ||
*/ | ||
const unset = (key) => { | ||
if (!has(key)) { | ||
return; | ||
} | ||
|
||
let storage = get(); | ||
delete storage[String(key)]; | ||
localStorage.setItem(table, JSON.stringify(storage)); | ||
let data = get(); | ||
delete data[String(key)]; | ||
localStorage.setItem(table, JSON.stringify(data)); | ||
}; | ||
|
||
const clear = () => localStorage.setItem(table, JSON.stringify({})); | ||
/** | ||
* @returns {void} | ||
*/ | ||
const clear = () => localStorage.setItem(table, '{}'); | ||
|
||
if (!localStorage.getItem(table)) { | ||
clear(); | ||
} | ||
|
||
return { | ||
get, | ||
set, | ||
unset, | ||
get, | ||
has, | ||
clear, | ||
unset, | ||
}; | ||
}; |