How to make authentication process using code?

25 views
Skip to first unread message

Ranieri Mazili de Oliveira

unread,
Oct 18, 2021, 9:39:34 AM10/18/21
to Keycloak Dev
Hello,

when executing the authentication flow through the /auth endpoint, the user input their username and password... as the option Consent Required is turned on for my scenario, the user will need to Accept or Reject the consent, then if it accept, the URL callcack will be called with the code and id_token.
This is all executed by the enduser using keycloak frontend, I would like to know how to do it programatically (preferably in Node JS).

Does anyone has an example, an article or a tip of what should I use to make it work?

Thanks

lokesh ravichandhu

unread,
Oct 18, 2021, 9:50:50 AM10/18/21
to Ranieri Mazili de Oliveira, Keycloak Dev
Hello Ranieri,

Take a look at the follow sample and let me know if that helps you.

Thanks,
Lokesh

--
You received this message because you are subscribed to the Google Groups "Keycloak Dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to keycloak-dev...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/keycloak-dev/e143a815-7e0e-4dca-9db5-e55cb860b31bn%40googlegroups.com.

Ranieri Mazili de Oliveira

unread,
Oct 18, 2021, 12:40:57 PM10/18/21
to Keycloak Dev
Not exactly... at this example you're redirecting to keycloak page do authenticate the user... I have the username and password on my backend services and would like to authenticate the user using code instead of keycloak screens.  Couldn't find any example showing how to do that.

lokesh ravichandhu

unread,
Oct 18, 2021, 12:45:07 PM10/18/21
to Ranieri Mazili de Oliveira, Keycloak Dev

Oky you are looking for direct grant support, try the following code snippet.

 

```

var axios = require('axios');

var qs = require('qs');

var data = qs.stringify({

  'client_id': '@clientId',

  'grant_type': 'password',

  'username': '@your_username',

  'password': '@your_password'

});

var config = {

  method: 'post',

  url: 'https://{yourhost}/auth/realms/master/protocol/openid-connect/token',

  headers: {

    'Content-Type': 'application/x-www-form-urlencoded'

  },

  data : data

};

 

axios(config)

.then(function (response) {

  console.log(JSON.stringify(response.data));

})

.catch(function (error) {

  console.log(error);

});

 

```

Thanks,

Lokesh

Reply all
Reply to author
Forward
0 new messages