-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: create getuserFromToken function
this function extract user info from token #19
- Loading branch information
1 parent
26522b2
commit 6c99712
Showing
1 changed file
with
22 additions
and
0 deletions.
There are no files selected for viewing
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 |
---|---|---|
@@ -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; | ||
} | ||
} |