mauriciovigolo / keycloak-angular

Easy Keycloak setup for Angular applications.
MIT License
730 stars 280 forks source link

How to pass external login (from native app) JWT to keycloak-angular in webview? #511

Open at2software opened 1 year ago

at2software commented 1 year ago

Hi. We're currently struggling with passing a valid JWT token that we obtained from keycloak through a native login mask (iOS and Android) to a WebView Container with the angular application. Is there any way how we can open the angular application and pass the JWT to the keycloak module, so it will use it for further communication?

at2software commented 1 year ago

I might have figured it out. Change the provider in app.module.ts as follows:

useFactory: (keycloak: KeycloakService, locationStrategy: LocationStrategy) => {
        return () => {
            let options: KeycloakOptions = {
                config: KeycloakProviderConfig,
                initOptions: {
                    onLoad: 'check-sso',
                    silentCheckSsoRedirectUri: `${window.location.origin}${locationStrategy.prepareExternalUrl('/assets/silent-check-sso.html')}`
                },
                loadUserProfileAtStartUp: true,
            }
            let params = new URLSearchParams(window.location.search)
            if (params.get('access_token') && params.get('refresh_token')) {
                options.initOptions!.adapter      = 'cordova-native'
                options.initOptions!.token        = params.get('access_token')!
                options.initOptions!.refreshToken = params.get('refresh_token')!
            }
            return keycloak.init(options)
        }
    }

then you can pass access_token and refresh_token as GET params to any angular URL and keycloak-angular seems to accept it. No idea, if token refresh will work with it as well, but for now it seems to do the trick