File tree Expand file tree Collapse file tree 1 file changed +15
-2
lines changed Expand file tree Collapse file tree 1 file changed +15
-2
lines changed Original file line number Diff line number Diff line change
1
+ import {
2
+ AccessToken ,
3
+ AccessTokenPayload ,
4
+ AccessTokenPayloadSchema ,
5
+ } from "@/types/Token" ;
1
6
import { UserProfile } from "@/types/User" ;
2
7
import React , {
3
8
createContext ,
@@ -28,8 +33,16 @@ export function useUser() {
28
33
return useContext ( UserContext ) ;
29
34
}
30
35
31
- function parseJwt ( token : string ) {
36
+ function parseJwt ( token : AccessToken ) : AccessTokenPayload {
32
37
const base64Url = token . split ( "." ) [ 1 ] ;
33
38
const base64 = base64Url . replace ( / - / g, "+" ) . replace ( / _ / g, "/" ) ;
34
- return JSON . parse ( atob ( base64 ) ) ;
39
+ const jsonPayload = decodeURIComponent (
40
+ atob ( base64 )
41
+ . split ( "" )
42
+ . map ( ( c ) => {
43
+ return "%" + ( "00" + c . charCodeAt ( 0 ) . toString ( 16 ) ) . slice ( - 2 ) ;
44
+ } )
45
+ . join ( "" )
46
+ ) ;
47
+ return AccessTokenPayloadSchema . parse ( JSON . parse ( jsonPayload ) ) ;
35
48
}
You can’t perform that action at this time.
0 commit comments