Skip to content

Commit

Permalink
feat: change Auth Storage
Browse files Browse the repository at this point in the history
wip: need to refactor

#128
  • Loading branch information
seo-wo committed Dec 18, 2023
1 parent 51dfca3 commit 3ef33ac
Showing 1 changed file with 22 additions and 2 deletions.
24 changes: 22 additions & 2 deletions src/cert/AuthStorage.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,28 @@
const AUTH = 'auth';

export function saveAuth(token: { id: string; url: string }) {
localStorage.setItem(AUTH, JSON.stringify(token));
export function saveAuth(token: string) {
const decoded = decodeToken(token);
let userData: { id: string; url: string };
userData = { id: decoded.username, url: decoded.imageUrl };
localStorage.setItem(AUTH, JSON.stringify(userData));
}
export function getAuth(): { id: string; url: string } {
return JSON.parse(localStorage.getItem(AUTH));
}

export function removeAuth() {
localStorage.removeItem(AUTH);
}

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);
};

0 comments on commit 3ef33ac

Please sign in to comment.