skip to Main Content

its my first time working with keycloak.
im creating a react native app for my website which is auth each user with keycloak.
im able to login and get a response which is {"authentication": null, "error": null, "errorCode": null, "params": {"code": "32c1d719-c8ce-4c1b-8800-5a853095da71.3fe7ff3e-feb2-47c9-b101-b24c90d596b4.918d5daa-5b42-4480-8832-3c88c5e876ad", "session_state": "3fe7ff3e-feb2-47c9-b101-b24c90d596b4", "state": "sVF3SdDAYg"}, "type": "success", "url": "exp://10.11.1.10:8081?state=sVF3SdDAYg&session_state=3fe7ff3e-feb2-47c9-b101-b24c90d596b4&code=32c1d719-c8ce-4c1b-8800-5a853095da71.3fe7ff3e-feb2-47c9-b101-b24c90d596b4.918d5daa-5b42-4480-8832-3c88c5e876ad"}

but how do i get the user token to use for rest api’s. my code to login is here :

import React, { useEffect, useState } from 'react';
import * as WebBrowser from 'expo-web-browser';
import { useAuthRequest, useAutoDiscovery } from 'expo-auth-session';
import { Button, Text, View } from 'react-native';

WebBrowser.maybeCompleteAuthSession();

export default function LoginPage() {
  const discovery = useAutoDiscovery('https://@@@@@@@@@@@@@@@@@@@@@@@@@');
  const redirectUri = 'exp://10.11.1.10:8081';

  const [accessToken, setAccessToken] = useState();
  const [request, result, promptAsync] = useAuthRequest(
    {
      clientId: 'mywebsite',
      redirectUri: redirectUri,
      scopes: ['openid', 'profile'],
      usePKCE: true,
    },
    discovery,); 

    console.log(result)

  return (
    <View style={{ flex: 1, justifyContent: 'center', alignItems: 'center' }}>
      <Button title="Login!" disabled={!request} onPress={() => promptAsync()} />
      {result && <Text>{JSON.stringify(result, null, 2)}</Text>}
    </View>
  );
}

i tried to use token restapi endpoint with secret key but i get error no token..

2

Answers


  1. We can get an access token from keycloak using the /token endpoint if you’re getting an error while hitting this api then check the body parameters we need to send the client_id,client_secret,grant_type,username and password.

    Login or Signup to reply.
  2. I have exactly the same problem Tomas.
    When you use pkce, you send a codeChallenge to Keycloak on login and you receive authorization code that you need to send back with the codeVerifier (the original code transformed into a codeChallenge).

    The problem is… I don’t know how to make that !

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search