Skip to content

Adds a new configuration option to allow external control over how the login page is opened #235

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Conversation

nhance
Copy link
Contributor

@nhance nhance commented Feb 7, 2018

In my implementation I was having trouble getting this to work on device due to the need to use an inappbrowser to open and catch the redirect_uri. This method allows implementations to configure a function for opening the tryLogin url.

New AuthConfig option:

  • openUri: (uri: string) => void) This method will be called when attempting to login via implicit flow.

@GFoley83
Copy link

GFoley83 commented Mar 5, 2018

Currently have the same problem in an application I'm working on: We're using Ionic 3 with inAppBrowser to log people in. Would obviously prefer not to use inAppBrowser but given there's no known work-around, there's not much choice.

I think returning the createLoginUrl promise and having an optional noRedirectToLoginUrl, which the logout function also has, is good enough to solve this problem, without needing to extend the API.

    /**
    * Starts the implicit flow and redirects to user to
    * the auth servers login url.
    *
    * @param additionalState Optinal state that is passes around.
    *  You find this state in the property ``state`` after ``tryLogin`` logged in the user.
    * @param params Hash with additional parameter. If it is a string, it is used for the 
    *               parameter loginHint (for the sake of compatibility with former versions)
    * @param noRedirectToLoginUrl If a login url is configured, the user is redirected to it.
    */
    initImplicitFlowInternal(additionalState = '', params: string | object = '', noRedirectToLoginUrl = false): Promise<string> {

        if (this.inImplicitFlow) {
            return;
        }

        this.inImplicitFlow = true;

        if (!this.validateUrlForHttps(this.loginUrl)) {
            throw new Error('loginUrl must use Http. Also check property requireHttps.');
        }

        let addParams: object = {};
        let loginHint: string = null;

        if (typeof params === 'string') {
            loginHint = params;
        }
        else if (typeof params === 'object') {
            addParams = params;
        }

        return this.createLoginUrl(additionalState, loginHint, null, false, addParams).then((url) => {
            if (noRedirectToLoginUrl) {
                return url;
            }

            location.href = url;
        })
            .catch(error => {
                console.error('Error in initImplicitFlow');
                console.error(error);
                this.inImplicitFlow = false;
                return null;
            });
    };

@neilsb
Copy link

neilsb commented Apr 2, 2018

@GFoley83 Just a note on the parameter name. I'd be more inclined to have the parameter the other way round. i.e. redirectToLoginUrl and default it to true.

I was looking at implementing this as an ImplicitFlowMethod config option, to allow options redirect, none, iframe and newWindow, and baking the code the iframe and new window into the library, but it probably makes more sense to just allow returning the url and letting the user handle it as desired.

@manfredsteyer manfredsteyer merged commit aa421ea into manfredsteyer:master May 9, 2018
@manfredsteyer
Copy link
Owner

thx

@mraible
Copy link
Contributor

mraible commented May 14, 2018

@nhance Can you please provide an example of how you're using this with inappbrowser? I have a similar use case.

@manfredsteyer
Copy link
Owner

If I understand it right, he is calling initImplicitFlow with this new flag. This causes the method to just return a promise with an url. This url can be set manually in the inappbrowser.

@mraible
Copy link
Contributor

mraible commented May 16, 2018 via email

@nhance
Copy link
Contributor Author

nhance commented Jun 27, 2018

@mraible You can now provide openUri as a configuration option on AuthConfig.

It is expected to be a function with a single parameter uri. The openUri function is called anytime a uri is needed to be opened, so for example on a mobile device you can open it in an inappbrowser.

Here's an example:

        authConfig.openUri = (uri) => {
          let browser = this.iab.create(uri, '_blank', 'location=no');

          browser.on('close').subscribe((event) => {
            if (window['oAuthService']) {
              window['oAuthService'].inImplicitFlow = false;
            }
          });

          browser.on('loadstart').subscribe((event) => {
            let url = event.url;

            if (url.startsWith(ENV.oauth.redirect_uri)) {
              const hashFragment = url.substr(ENV.oauth.redirect_uri.length);

              console.log("login from url scheme with token " + hashFragment);

              this.oauthService.tryLogin({
                customHashFragment: hashFragment,
                onTokenReceived: () => {
                  console.log("Login complete, should go to list page");
                  browser.close();
                  this.initializeLoggedIn();
                },
                onLoginError: () => {
                  console.log("Login error");
                  browser.close();
                  this.initializeUnauthenticated();
                }
              });

              return false;
            } else {
              console.log(url, ' did not start with ', ENV.oauth.redirect_uri);
            }
          });

@nhance
Copy link
Contributor Author

nhance commented Jun 27, 2018

It appears this is not actually merged in. We will create a new pull request to actually implement this

@naftalivanderloon
Copy link

Calling openUri here seems to work, @manfredsteyer could you change this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants