Hi,
I am trying to integrate a Jmix 2.0 (FlowUI) application with a third party React solution built on next.js framework but I am constantly getting the following error when I am trying to get the access token:
Access to fetch at 'http://localhost:8080/oauth2/token' from origin 'http://localhost:3000' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: Redirect is not allowed for a preflight request.
_app.tsx:141
POST http://localhost:8080/oauth2/token net::ERR_FAILED
login @ _app.tsx:141
handleLogin @ index.js:1
fn @ mutation.mjs:87
run @ retryer.mjs:101
createRetryer @ retryer.mjs:149
executeMutation @ mutation.mjs:81
In the React app, I am using the following code to call the API endpoint:
const clientID = "sahjkjjzgj";
const clientSecret = "wqvyuVPCzg"
const clientKey = `${clientID}:${clientSecret}`;
const base64ClientKey = btoa(clientKey);
console.log(base64ClientKey);
fetch('http://localhost:8080/oauth2/token', {
method: 'POST',
body: 'grant_type=client_credentials',
headers: {
'Authorization': 'Basic ' + base64ClientKey
}
}).then(function (resp) {
console.log(resp);
// Return the response as JSON
return resp.json();
})
You can also find below the existing setup in the Jmix application where the REST add-on is installed:
spring.security.oauth2.authorizationserver.client.myclient.registration.client-id=sahjkjjzgj
spring.security.oauth2.authorizationserver.client.myclient.registration.client-secret={noop}wqvyuVPCzg
spring.security.oauth2.authorizationserver.client.myclient.registration.authorization-grant-types=client_credentials
spring.security.oauth2.authorizationserver.client.myclient.registration.client-authentication_methods=client_secret_basic
spring.security.oauth2.authorizationserver.client.myclient.token.access-token-format=reference
#Set the following params for Authorization server
jmix.authserver.client.myclient.client-id = sahjkjjzgj
jmix.authserver.client.myclient.resource-roles = system-full-access,rest-minimal
jmix.cors.allowed-origins=*
jmix.cors.allowed-headers=*
jmix.cors.allowed-methods=*
jmix.cors.url-patterns=/**
jmix.cors.allow-credentials= true
I have also tried to connect in a Jmix 1.5 application from the React app with a similar approach -since the authentication process is different- and it seems to be working properly.
Regards,
George