Skip to content
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

Add resumeAuth method and autoParseHash flag #790

Merged
merged 4 commits into from
Jan 11, 2017
Merged

Add resumeAuth method and autoParseHash flag #790

merged 4 commits into from
Jan 11, 2017

Conversation

luisrudge
Copy link
Contributor

@luisrudge luisrudge commented Jan 10, 2017

When Lock redirects to the redirect url, it puts the params after a # (eg: #id_token=foo&access_token=bar. This causes some issues with client side routers that use # to handle urls. An example is auth0-samples/auth0-angularjs2-systemjs-sample#40.

This PR adds an option to prevent parsing the hash when you instantiate lock (autoParseHash) and adds the resumeAuth method to resume the authentication steps (including all the events)

Base.hasScheduledAuthCallback = true;
setTimeout(handleAuthCallback, 0);
if (!options.disableParseHashOnInitialization) {
if (!Base.hasScheduledAuthCallback) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just merge the ifs there is no need to nest them

@hzalaz hzalaz added this to the v10-Next milestone Jan 11, 2017
@hzalaz hzalaz changed the title adds resumeAuth method and disableParseHashOnInitialization option Add resumeAuth method and autoParseHash flag Jan 11, 2017
@stefanruijsenaars
Copy link

Are there any code snippets that show how can incorporate this into an Angular 2 application with useHash = true?

@luisrudge
Copy link
Contributor Author

@stefanruijsenaars I think @chenkie is updating our angular2 guides. For now, use this as a guide. Then replace handleRedirectWithHash with this:

private handleRedirectWithHash() {
  this.router.events.take(1).subscribe(event => {
    if (/access_token/.test(event.url) || /error/.test(event.url)) {
      this.lock.resumeAuth(window.location.hash);
    }
  });
}

I'm not sure this will work 100%, but it's something like that. Our angular2 guide will work 100% though, if you want to wait for it 😁

@chenkie
Copy link

chenkie commented Jan 21, 2017

@stefanruijsenaars the method that's going into the samples will pretty much look like this:

public handleAuthenticationWithHash(): void {
  this
    .router
    .events
    .filter(event => event.constructor.name === 'NavigationStart')
    .filter(event => (/access_token|id_token|error/).test(event.url))
    .subscribe(event => {
      this.lock.resumeAuth(window.location.hash, (error, authResult) => {
        if (error) return console.log(error);
        this.setUser(authResult);
        this.router.navigateByUrl('/');
      });
  });
}

private setUser(authResult): void {
  localStorage.setItem('access_token', authResult.accessToken);
  localStorage.setItem('id_token', authResult.idToken);
}

@mrahbar
Copy link

mrahbar commented Jan 25, 2017

@chenkie For someone how tries to get auth0 working in angular 1 as well as 2 I find unfortunately lot of different examples and configuration options and can't always tell which is the correct and most up to date one.

Having said that, your code snippet should be take as the current way to implement auth0-lock on angular 2. The method handleAuthCallback() path didn't work as expected because it was called after location.hash was empty. Probably because the problems mentioned here

Now resumeAuth() which was called anyways by handleAuthCallback() has to be called directly via the above provided implementation. Which in turn will lead to the emitting of the existing events called in parseHash() callback.

So my two cents:

I kept my previous implementation of this.lock.on("authenticated", (authResult) => { ...} ) ( and added also an event listener on 'authorization_error' just in case) and used the following snippet which is based on the above code:

 this._router.events
      .filter(event => event instanceof NavigationStart)
      .filter(event => (/access_token|id_token|error/).test(event.url))
      .subscribe(event => {
        this.lock.resumeAuth(window.location.hash, (error, authResult) => {});
      });

This lets existing code the way it is as well as existing examples and tutorial snippets. And remember to set auth: { autoParseHash: false} in the config options.

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