dig
dig
KKinde
Created by dig on 12/14/2024 in #💻┃support
Other connected app support like gmail, outlook
Is there any way to achieve the above?
13 replies
KKinde
Created by dig on 12/13/2024 in #💻┃support
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 }
}
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:
...
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:
...
7 replies