I am trying to use the Keycloak-js(from 4.4.0.Final) library in my ionic(4) cordova application.
I have followed the example and instructions from the documentation.
I have installed cordova-plugin-browsertab
, cordova-plugin-deeplinks
, cordova-plugin-inappbrowser
.
Added <preference name="AndroidLaunchMode" value="singleTask" />
in my config.xml
And this is how my modifications to config.xml looks like.
<widget id="org.phidatalab.radar_armt"....>
<plugin name="cordova-plugin-browsertab" spec="0.2.0" />
<plugin name="cordova-plugin-inappbrowser" spec="3.0.0" />
<plugin name="cordova-plugin-deeplinks" spec="1.1.0" />
<preference name="AndroidLaunchMode" value="singleTask" />
<allow-intent href="http://*/*" />
<allow-intent href="https://*/*" />
<universal-links>
<host name="keycloak-cordova-example.exampledomain.net" scheme="https">
<path event="keycloak" url="/login" />
</host>
</universal-links>
</widget>
and my service which uses Keycloak-js
looks like below.
static init(): Promise<any> {
// Create a new Keycloak Client Instance
let keycloakAuth: any = new Keycloak({
url: 'https://exampledomain.net/auth/',
realm: 'mighealth',
clientId: 'armt',
});
return new Promise((resolve, reject) => {
keycloakAuth.init({
onLoad: 'login-required',
adapter: 'cordova-native',
responseMode: 'query',
redirectUri: 'android-app://org.phidatalab.radar_armt/https/keycloak-cordova-example.github.io/login'
}).success(() => {
console.log("Success")
resolve();
}).error((err) => {
reject(err);
});
});
}
I can successfully build and run the application for Android
. However, it doesn't work.
From adb
logs I get ( For both cordova
and cordova-native
adapters)
12-04 19:07:35.911 32578-32578/org.phidatalab.radar_armt D/SystemWebChromeClient: ng:///AuthModule/EnrolmentPageComponent.ngfactory.js: Line 457 : ERROR
12-04 19:07:35.911 32578-32578/org.phidatalab.radar_armt I/chromium: [INFO:CONSOLE(457)] "ERROR", source: ng:///AuthModule/EnrolmentPageComponent.ngfactory.js (457)
12-04 19:07:35.918 32578-32578/org.phidatalab.radar_armt D/SystemWebChromeClient: ng:///AuthModule/EnrolmentPageComponent.ngfactory.js: Line 457 : ERROR CONTEXT
12-04 19:07:35.919 32578-32578/org.phidatalab.radar_armt I/chromium: [INFO:CONSOLE(457)] "ERROR CONTEXT", source: ng:///AuthModule/EnrolmentPageComponent.ngfactory.js (457)
If I try to run it on browser, I get "universalLink is undefined"
.
I would really like some help to get this working. What am I missing? Any kind of help is much appreciated. Or is there a workaround/examples to get keycloak working for an ionic (public) client?
I am posting my solution here, since I wasted a lot of time getting available plugin working for my environments. The implementation provided by
keycloak-js
is fairly outdated. So if you try to use it for an ionic-3 app, it just doesn't work.My solution to get this working is using
InAppBrowser
plugin (similar tocordova
approach ofkeycloak-js
) and follow standard Oauth2authorization_code
procedure. I had a look into the code ofkeycloak-js
and implemented solution based on it. Thanks tokeycloak-js
too.Here it is. Step1: Install
[cordova-inapp-browser][1]
.Step2: A sample
keycloak-auth.service.ts
could look like below. This could potentially replace keycloak-js, but only forcordova
option.Step 3: Then you can use this service in your components to what is necessary.
Note: keycloakLogin(true) takes you to login page or keycloakLogin(false) takes you to registration page of keycloak.
I hope this helps you solve it more or less.
For those having a similar issue, I've written a library about it, which only works on Android and IOS platforms, to make the login using Keycloak. This should work fine.
This also maintains the user session. Just give it a try.
@cmotion/ionic-keycloak-auth