Skip to content

Commit

Permalink
feat: create getuserFromToken function
Browse files Browse the repository at this point in the history
this function extract user info from token

#19
  • Loading branch information
timepresent95 committed Aug 13, 2024
1 parent 26522b2 commit 6c99712
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions src/utils/token.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import {Member} from '@/types';

//NOTE: 유저 정보를 토큰에서 얻기 위한 함수
//FIXME: BE에서 전달해준 payload가 정해지면 수정해야 함
export function getUserFromToken(token: string): Member | null {
try {
const parts = token.split('.');
if (parts.length !== 3) {
return null;
}

const [, encodedPayload] = parts;
const decodedPayload = Buffer.from(encodedPayload, 'base64').toString(
'utf-8',
);
const payload = JSON.parse(decodedPayload);

return payload as Member;
} catch (error) {
return null;
}
}

0 comments on commit 6c99712

Please sign in to comment.