Skip to content

Commit

Permalink
feat: update auth key (#35)
Browse files Browse the repository at this point in the history
  • Loading branch information
WoeOm authored Sep 24, 2024
1 parent 476d2ed commit 4bbb8da
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 4 deletions.
10 changes: 6 additions & 4 deletions src/editor/api/subscan/AxiosRequest.ts
Original file line number Diff line number Diff line change
@@ -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<T> = {
Expand All @@ -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) {
Expand Down
88 changes: 88 additions & 0 deletions src/utils/cookies.ts
Original file line number Diff line number Diff line change
@@ -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;
},
};

0 comments on commit 4bbb8da

Please sign in to comment.