ionic-appauth icon indicating copy to clipboard operation
ionic-appauth copied to clipboard

Unclear how to use browser (close) event

Open martinmitteregger opened this issue 4 years ago • 3 comments

In a previous issue #19 you mentioned that the Browser class has a method called browserCloseListener. The problem is that i only see the addCloseBrowserEvent method and I'm not sure how to properly use that method to get SignInFailed or similar errors.

Maybe you can help me and clearify how i could use that method appropriate.

martinmitteregger avatar Aug 31 '20 09:08 martinmitteregger

Still not able to add a closeBrowser event or to intercept a login failed action.

martinmitteregger avatar Sep 22 '20 11:09 martinmitteregger

I'm having the same problem. I don't know how to handle a login cancel button in the browser...

CarlosTorrecillas avatar Jan 14 '21 10:01 CarlosTorrecillas

Actually I just tested that hooking up to:

browser.browserCloseListener(() => {
          App.exitApp();
});

does the trick. Actually my full auth.factory is (in case it helps you:

import { Platform } from '@ionic/angular';
import { StorageBackend, Requestor } from '@openid/appauth';
import { AuthService, Browser, ConsoleLogObserver } from 'ionic-appauth';
import { Plugins } from '@capacitor/core';
import { environment } from 'src/environments/environment';
import { NgZone } from '@angular/core';

const { App } = Plugins;

export let authFactory = (platform: Platform, ngZone: NgZone,
    requestor: Requestor, browser: Browser,  storage: StorageBackend) => {

    const authService = new AuthService(browser, storage, requestor);
    authService.authConfig = environment.auth_config;

    if (!platform.is('android')) {
        authService.authConfig.server_host = environment.browserAuthorityUrl;
        authService.authConfig.redirect_url = window.location.origin + '/auth-callback';
        authService.authConfig.end_session_redirect_url = window.location.origin + '/end-session';
    }

    if (platform.is('capacitor')) {
        App.addListener('appUrlOpen', (data: any) => {
            if (data.url !== undefined) {
                ngZone.run(() => {
                    if (data.url.indexOf(environment.auth_config.redirect_url) === 0) {
                        authService.authorizationCallback(data.url);
                    } else if (data.url.indexOf(environment.auth_config.end_session_redirect_url) === 0) {
                        authService.endSessionCallback();
                    }
                });
            }
        });

        browser.browserCloseListener(() => {
            App.exitApp();
        });
    }

    console.log('Using IdentityServer URL', authService.authConfig.server_host);
    authService.addActionObserver(new ConsoleLogObserver());
    return authService;
};

CarlosTorrecillas avatar Jan 14 '21 10:01 CarlosTorrecillas