Skip to content

Commit

Permalink
fix: cookie, token logic
Browse files Browse the repository at this point in the history
  • Loading branch information
SoonGwan committed Aug 11, 2021
1 parent 0a6a986 commit 09d17c6
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 7 deletions.
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"@testing-library/react": "^11.1.0",
"@testing-library/user-event": "^12.1.10",
"@types/jest": "^26.0.15",
"@types/js-cookie": "^2.2.7",
"@types/node": "^12.0.0",
"@types/react": "^17.0.0",
"@types/react-dom": "^17.0.0",
Expand All @@ -20,6 +21,7 @@
"axios": "^0.21.1",
"dotenv": "^10.0.0",
"history": "^5.0.0",
"js-cookie": "^3.0.0",
"polished": "^4.1.3",
"react": "^17.0.2",
"react-dom": "^17.0.2",
Expand Down
21 changes: 21 additions & 0 deletions src/lib/cookie/cookie.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import cookies from 'js-cookie';

class Cookie {
public static getCookie(key: string): any {
return cookies.get(key);
}

public static setCookie(
key: string,
value: any,
expires: number | Date = 7
): void {
cookies.set(key, value, { expires });
}

public static removeCookie(key: string): void {
cookies.remove(key);
}
}

export default Cookie;
1 change: 1 addition & 0 deletions src/lib/cookie/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { default } from './cookie';
2 changes: 1 addition & 1 deletion src/lib/token/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export { setToken, getToken } from './token';
export { default } from './token';
22 changes: 16 additions & 6 deletions src/lib/token/token.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,17 @@
export const setToken = (tokenKey: string, token: string) => {
sessionStorage.setItem(tokenKey, token);
};
import Cookie from 'lib/cookie';

export const getToken = (): string => {
return sessionStorage.getItem('access_token') as string;
};
class Token {
private static FIRE_DAY = 9 * 60 * 60 * 60 * 1000;

public static setToken(tokenKey: string, token: string): void {
const date = new Date();
date.setTime(date.getTime() + this.FIRE_DAY);
Cookie.setCookie(tokenKey, token, date);
}

public static getToken(tokenKey: string): string {
return Cookie.getCookie(tokenKey) as string;
}
}

export default Token;
10 changes: 10 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2069,6 +2069,11 @@
jest-diff "^26.0.0"
pretty-format "^26.0.0"

"@types/js-cookie@^2.2.7":
version "2.2.7"
resolved "https://registry.yarnpkg.com/@types/js-cookie/-/js-cookie-2.2.7.tgz#226a9e31680835a6188e887f3988e60c04d3f6a3"
integrity sha512-aLkWa0C0vO5b4Sr798E26QgOkss68Un0bLjs7u9qxzPT5CG+8DuNTffWES58YzJs3hrVAOs1wonycqEBqNJubA==

"@types/json-schema@*", "@types/json-schema@^7.0.3", "@types/json-schema@^7.0.5", "@types/json-schema@^7.0.6":
version "7.0.7"
resolved "https://registry.yarnpkg.com/@types/json-schema/-/json-schema-7.0.7.tgz#98a993516c859eb0d5c4c8f098317a9ea68db9ad"
Expand Down Expand Up @@ -7473,6 +7478,11 @@ [email protected]:
import-local "^3.0.2"
jest-cli "^26.6.0"

js-cookie@^3.0.0:
version "3.0.0"
resolved "https://registry.yarnpkg.com/js-cookie/-/js-cookie-3.0.0.tgz#db1661d5459920ec95aaf186ccf74ceb4a495164"
integrity sha512-oUbbplKuH07/XX2YD2+Q+GMiPpnVXaRz8npE7suhBH9QEkJe2W7mQ6rwuMXHue3fpfcftQwzgyvGzIHyfCSngQ==

"js-tokens@^3.0.0 || ^4.0.0", js-tokens@^4.0.0:
version "4.0.0"
resolved "https://registry.yarnpkg.com/js-tokens/-/js-tokens-4.0.0.tgz#19203fb59991df98e3a287050d4647cdeaf32499"
Expand Down

0 comments on commit 09d17c6

Please sign in to comment.