KindeK
Kinde14mo ago
7 replies
dig

Retrieve user data from access token

I have access token which is generated by react SDK.
My backend is implemented in python. I'm planning to use jwt based authentication but I couldn't find way to retrieve user data from access token which is generated by react SDK.

Any way to retrieve user data from access token?
Also I want to know a way to retrieve access token for connected apps under my situation.

Sample
Frontend:
const { user, isLoading, isAuthenticated, logout, getToken } = useKindeAuth()
  const router = useRouter()

  useEffect(() => {
    if (!isLoading && !isAuthenticated) {
      router.push('/auth/signin')
    }
  }, [isAuthenticated, isLoading, router])

  const fetchData = async () => {
    try {
      const accessToken = await getToken()
      const response = await fetch(`${process.env.NEXT_PUBLIC_API_URL}/api/emails`, {
        headers: {
          Authorization: `Bearer ${accessToken}`
        }
      })
      const result = await response.json()
    } catch (err) { xx } finally { xx }
  }


Backend(python, fastapi)
async def verify_token(request: Request):
    auth_header = request.headers.get("Authorization")    
    token = auth_header.split(" ")[1]
    kinde_client = get_kinde_client() 
    signing_key = kinde_client.jwks_client.get_signing_key_from_jwt(token)
    
    try:
        decoded_token = jwt.decode(
            jwt=token,
            key=signing_key.key,
            algorithms=["RS256"],
            options={ xxx }
        )
        print(f"decoded_token: {decoded_token}")

        kinde_client.configuration.access_token = token
        details = kinde_client.get_user_details() ###. THIS WONT WORK ###
        print(f"details: {details}")
    ....


@app.get("/backend/api")
async def api(
    user = Depends(verify_token)
):
    try:
    ...
Was this page helpful?