Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can't read payloads instantly #22

Open
khokonm opened this issue Dec 21, 2021 · 5 comments
Open

Can't read payloads instantly #22

khokonm opened this issue Dec 21, 2021 · 5 comments

Comments

@khokonm
Copy link

khokonm commented Dec 21, 2021

const { decodedToken, isExpired } = useJwt(jwt)
const exp = decodedToken.exp

this gives me an error like:
Unhandled Runtime Error
TypeError: Cannot read properties of null (reading 'exp')

@wanrabbae
Copy link

Do you know how to access the payloads ?

@AlessandroMinoccheri
Copy link
Contributor

@khokonm I have created a PR to solve the problem using generics.
#28

I hope that could be merged to access to properties of our custom types.

@ningaro
Copy link

ningaro commented Jun 19, 2022

@AlessandroMinoccheri Thank you ❤️
For those who want to use this library with the Typescript project, you can use this example

/* 
Example JWT
...
payload : {
  "name": "Ningaro",
  "iat": 1516239022
} 
...
*/
const token = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJuYW1lIjoiTmluZ2FybyIsImlhdCI6MTUxNjIzOTAyMn0.13qtyXZqQi3k2MPaxEuBBPOlQUiqWPn6_3elaK3JJVo'
// Component.tsx
interface TokenPayload {
name: string
iat: number
}
const nameFromJWT = useJwt<TokenPayload>(token).decodedToken?.name!

@hijizaly
Copy link

const { decodedToken, isExpired } = useJwt(yourjwt);
const exp = decodedToken?.exp;

Try this my friend
It Work for me 💯

@ilhamije
Copy link

const exp = decodedToken?.exp;

Thanks a lot, @hijizaly
This solution works for me. I've been struggling for the same issue for a few days.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants