-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #142 from Together42/141-TokenStorage_refactor
Fix 로컬스토리지 관리 로직 수정
- Loading branch information
Showing
12 changed files
with
105 additions
and
69 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
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,28 +1,97 @@ | ||
const TOKEN = 'token'; | ||
const expireDays = 5; | ||
|
||
const JWT_PART_LENGTH = 3; | ||
const MS_PER_SEC = 1000; | ||
|
||
enum JWTPart { | ||
Header, | ||
Payload, | ||
Signature, | ||
} | ||
export interface JWTPayload { | ||
id: string; | ||
iat: number; | ||
exp: number; | ||
} | ||
|
||
export function getJWTPart(token: string, part: JWTPart): string { | ||
const parts = token.split('.'); | ||
if (parts.length !== JWT_PART_LENGTH) { | ||
throw new Error('Invalid JWT token format.'); | ||
} | ||
return parts[part]; | ||
} | ||
|
||
export function isValidJWTPayload(object: any): object is JWTPayload { | ||
return ( | ||
object !== null && | ||
typeof object === 'object' && | ||
'id' in object && | ||
typeof object.id === 'number' && | ||
'iat' in object && | ||
typeof object.iat === 'number' && | ||
'exp' in object && | ||
typeof object.exp === 'number' | ||
); | ||
} | ||
|
||
export function parseJWTPayload(token: string): JWTPayload { | ||
try { | ||
const payload = decodeToken(token); | ||
if (!isValidJWTPayload(payload)) { | ||
throw new Error('Invalid JWTPayload'); | ||
} | ||
return payload; | ||
} catch (error) { | ||
throw error; | ||
} | ||
} | ||
|
||
export function isJWTExpired(token: string): boolean { | ||
try { | ||
const payload = parseJWTPayload(token); | ||
return payload.exp < Date.now() / MS_PER_SEC; | ||
} catch (error) { | ||
console.error(error); | ||
return true; | ||
} | ||
} | ||
|
||
export function clearToken() { | ||
localStorage.clear(); | ||
localStorage.removeItem(TOKEN); | ||
} | ||
|
||
export function saveToken(token: string) { | ||
const tokenObj = { | ||
value: token, | ||
expire: Date.now() + expireDays * 24 * 60 * 60 * 1000, | ||
}; | ||
const tokenObjString = JSON.stringify(tokenObj); | ||
localStorage.setItem(TOKEN, tokenObjString); | ||
localStorage.setItem(TOKEN, token); | ||
} | ||
|
||
export function getToken() { | ||
const tokenObjString = localStorage.getItem(TOKEN); | ||
if (!tokenObjString) { | ||
return null; | ||
} | ||
const tokenObj = JSON.parse(tokenObjString); | ||
if (Date.now() > tokenObj.expire) { | ||
let token = localStorage.getItem(TOKEN); | ||
if (token !== null && isJWTExpired(token)) { | ||
clearToken(); | ||
return null; | ||
token = null; | ||
} | ||
return tokenObj.value; | ||
return token; | ||
} | ||
|
||
export function getDecodedToken(): { id: string; url: string } { | ||
const token = localStorage.getItem(TOKEN); | ||
if (!token) { | ||
return { id: null, url: null }; | ||
} | ||
const decoded = decodeToken(token); | ||
return { id: decoded.username, url: decoded.imageUrl }; | ||
} | ||
|
||
const decodeToken = (token: string) => { | ||
const base64Url = token.split('.')[1]; | ||
const base64 = base64Url.replace(/-/g, '+').replace(/_/g, '/'); | ||
const jsonPayload = decodeURIComponent( | ||
atob(base64) | ||
.split('') | ||
.map((c) => '%' + ('00' + c.charCodeAt(0).toString(16)).slice(-2)) | ||
.join(''), | ||
); | ||
|
||
return JSON.parse(jsonPayload); | ||
}; |
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
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
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