diff --git a/src/editor/api/subscan/AxiosRequest.ts b/src/editor/api/subscan/AxiosRequest.ts index 2f7de40..0493751 100644 --- a/src/editor/api/subscan/AxiosRequest.ts +++ b/src/editor/api/subscan/AxiosRequest.ts @@ -1,14 +1,16 @@ import axios from 'axios'; import type { AxiosInstance, AxiosRequestConfig, AxiosResponse } from 'axios'; +import { docCookies } from 'utils/cookies'; -export const STORAGE_KEY_TOKEN = 'token'; +export const PLATFORM_AUTH_KEY = 'sub_token'; export function getLocalToken() { - return localStorage.getItem(STORAGE_KEY_TOKEN); + const token = docCookies.getItem(typeof document !== 'undefined' ? document.cookie : '', PLATFORM_AUTH_KEY); + return token; } export function removeLocalToken() { - localStorage.removeItem(STORAGE_KEY_TOKEN); + docCookies.removeItem(document.cookie, PLATFORM_AUTH_KEY, '/', 'subscan.io'); } type Result = { @@ -26,7 +28,7 @@ export class Request { this.instance.interceptors.request.use( (config: AxiosRequestConfig) => { - const token = localStorage.getItem('token') as string; + const token = getLocalToken() as string; const organizationId = localStorage.getItem('organization:id') as string; if (token) { diff --git a/src/utils/cookies.ts b/src/utils/cookies.ts new file mode 100644 index 0000000..5c6639c --- /dev/null +++ b/src/utils/cookies.ts @@ -0,0 +1,88 @@ +/* eslint-disable no-useless-escape */ +/* eslint-disable no-useless-backreference */ +/* \ +|*| +|*| :: cookies.js :: +|*| +|*| A complete cookies reader/writer framework with full unicode support. +|*| +|*| https://developer.mozilla.org/en-US/docs/DOM/document.cookie +|*| +|*| This framework is released under the GNU Public License, version 3 or later. +|*| http://www.gnu.org/licenses/gpl-3.0-standalone.html +|*| +|*| Syntaxes: +|*| +|*| * docCookies.setItem(name, value[, end[, path[, domain[, secure]]]]) +|*| * docCookies.getItem(name) +|*| * docCookies.removeItem(name[, path], domain) +|*| * docCookies.hasItem(name) +|*| * docCookies.keys() +|*| +\ */ + +export const docCookies = { + getItem: function (cookie: string, sKey: string) { + return ( + decodeURIComponent( + cookie.replace( + new RegExp( + '(?:(?:^|.*;)\\s*' + encodeURIComponent(sKey).replace(/[-.+*]/g, '\\$&') + '\\s*\\=\\s*([^;]*).*$)|^.*$' + ), + '$1' + ) + ) || undefined + ); + }, + setItem: function (sKey: string, sValue: string, vEnd?: number, sPath?: string, sDomain?: string, bSecure?: boolean) { + if (!sKey || /^(?:expires|max\-age|path|domain|secure)$/i.test(sKey)) { + return false; + } + let sExpires = ''; + if (vEnd) { + switch (vEnd.constructor) { + case Number: + sExpires = vEnd === Infinity ? '; expires=Fri, 31 Dec 9999 23:59:59 GMT' : '; max-age=' + vEnd; + break; + case String: + sExpires = '; expires=' + vEnd; + break; + // case Date: + // sExpires = "; expires=" + vEnd.toUTCString(); + // break; + } + } + document.cookie = + encodeURIComponent(sKey) + + '=' + + encodeURIComponent(sValue) + + sExpires + + (sDomain ? '; domain=' + sDomain : '') + + (sPath ? '; path=' + sPath : '') + + (bSecure ? '; secure' : ''); + return true; + }, + removeItem: function (cookie: string, sKey: string, sPath?: string, sDomain?: string) { + if (!sKey || !this.hasItem(cookie, sKey)) { + return false; + } + document.cookie = + encodeURIComponent(sKey) + + '=; expires=Thu, 01 Jan 1970 00:00:00 GMT' + + (sDomain ? '; domain=' + sDomain : '') + + (sPath ? '; path=' + sPath : ''); + return true; + }, + hasItem: function (cookie: string, sKey: string) { + return new RegExp('(?:^|;\\s*)' + encodeURIComponent(sKey).replace(/[-.+*]/g, '\\$&') + '\\s*\\=').test(cookie); + }, + keys: /* optional method: you can safely remove it! */ function (cookie: string) { + const aKeys = cookie + .replace(/((?:^|\s*;)[^\=]+)(?=;|$)|^\s*|\s*(?:\=[^;]*)?(?:\1|$)/g, '') + .split(/\s*(?:\=[^;]*)?;\s*/); + for (let nIdx = 0; nIdx < aKeys.length; nIdx++) { + aKeys[nIdx] = decodeURIComponent(aKeys[nIdx]); + } + return aKeys; + }, +};