Skip to content

Events and onTokenReceived seemingly not working. #133

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

Closed
defmonk0 opened this issue Oct 19, 2017 · 9 comments
Closed

Events and onTokenReceived seemingly not working. #133

defmonk0 opened this issue Oct 19, 2017 · 9 comments

Comments

@defmonk0
Copy link

Hello! Recently been trying this out for a new project, and have gotten it to work (for the most part).

After setting everything up, I properly get implicit flow working, and a token appears in local storage which I can use. Additionally, I tried subscribing to OAuthService.events and logging some information in the onTokenReceived callback of OAuthService.tryLogin. Neither of these ever output anything to the console.

My main app component is quite simple still currently, so I'm not sure why these logs don't work.

app.component.ts:

@Component({
	selector: "app-root",
	templateUrl: "./app.component.html",
	styleUrls: ["./app.component.css"],
})
export class AppComponent implements OnInit {
	constructor(private oauthService: OAuthService) {
		this.oauthService.configure(AuthConfigInstance);

		this.oauthService.events.subscribe(function(e) {
			console.log(e);
		});

		this.oauthService.tryLogin({
			onTokenReceived: function(context) {
				console.log("logged in");
				console.log(context);
			},
		});
	}

	ngOnInit() {}
}

My main concern is that the SSO i'm using allows you to hit another URL to get some info about the token, at /oauth/verify. It's useful to get the user's name, id, etc. I didn't see a way to do this automatically in the documentation, so I was trying to do it in onTokenReceived. That's when I came across this issue. (Side note: if I'm going about this wrong, and there IS a way to do this automatically that I missed, please let me know. Lol.)

If you have any idea what's up, I'd love some help. If you need any additional info, let me know.

@atrifyllis
Copy link

It seems that callback and events are fired only for oidc. This library is supposed to support oauth2 without oidc no? What am I missing?

@defmonk0
Copy link
Author

defmonk0 commented Oct 23, 2017

@otinanism I looked at the code for where the events are fired and yea, it seems the function stops and returns before doing any of the callback/event logic when using oauth2 only (non-oidc).

I would consider trying to fix it myself, but this seems like a larger design question that @manfredsteyer should probably chime in on. Is this intended? If not, it looks like the function in question would need a bit of a refactor to handle the branching logic. Would it be better to switch to a guaranteed promise/observable for the whole function, regardless of type, instead of using a callback?

@manfredsteyer
Copy link
Owner

Thx for pointing this out. The lib is now also raising the token_received when we only get an access_token. This will published soon.

@federicojsequeira
Copy link

Hi guys, sorry that i reopen this bug but this is still not working for me. I have oidc: false and i can see that the return is being executed before calling callOnTokenReceivedIfExists. That oidc check block is returning without dealing with the onTokenReceived event.

if (!this.oidc) {
  this.eventsSubject.next(new OAuthSuccessEvent('token_received'));
  if (this.clearHashAfterLogin)
    location.hash = '';
  return Promise.resolve();
}

Am i missing something here? Does anyone know how can i deal with this? Thx!

@defmonk0
Copy link
Author

defmonk0 commented Feb 6, 2018

@federicojsequeira In my case, I subscribed to the "token_received" event using Angular's standard observable.subscribe. This works fine. If you're attempting to pass the onTokenReceived function in through the oauthService.tryLogin call (which, admittedly, is what is shown in the documentation), that may be the difference. Perhaps that function never gets executed?

For an example, here is my app component using this. The nesting is getting a bit out of hand since I haven't gone back to clean it up (it was just a prototype), but it should be simple enough to understand what is going on.

@federicojsequeira
Copy link

@defmonk0 thanks for your response. I already tried that and i don't see a way to get the redirect url that has been stored when initImplicitFlow(redirectUrl) is called. I mean, this way i can code inside the token received event but cannot see the info property that i need.
@manfredsteyer mention that the issue was fixed, maybe we must wait for a new release? Anyway, can someone help me with this workaround? Basically im trying to implement deeplinking inside an Angular app.

@federicojsequeira
Copy link

I already found a way, im doing this:

this.oauthService.events.subscribe(({ type }: OAuthEvent) => {
  switch (type) {
    case 'token_received':
      this.router.navigate([this.oauthService.state]);
      break;
    }
});

If someone see a possible issue here pls let me know. Thxs!

@alicehelen
Copy link

@federicojsequeira

I tried using the below code but it does not redirect to the original url after login. this.oauthService.events.subscribe(({ type }: OAuthEvent) => {
switch (type) {
case 'token_received':
this.router.navigate([this.oauthService.state]);
break;
}
});

This is the code I have in appcomponent and I am using implicit flow. Can you please verify if I am using the token_received event correctly

constructor(private oauthService: OAuthService, private router: Router) {
this.oauthService.clientId = '';
this.oauthService.scope = 'openid profile email';
this.oauthService.issuer = 'https://oktapreview.com/oauth2/default';
this.oauthService.tokenValidationHandler = new JwksValidationHandler();
this.oauthService.responseType = 'id_token';
this.oauthService.setStorage(localStorage);
this.oauthService.oidc = true;
this.oauthService.events.subscribe(({ type }: OAuthEvent) => {
switch (type) {
case 'token_received':
this.router.navigate([this.oauthService.state]);
break;
}
});
// Load Discovery Document and then try to login the user
this.oauthService.loadDiscoveryDocument().then(() => {
this.oauthService.tryLogin().then (() => {
this.oauthService.setupAutomaticSilentRefresh();
}
)
});
}

@lalinda74
Copy link

I already found a way, im doing this:

this.oauthService.events.subscribe(({ type }: OAuthEvent) => {
  switch (type) {
    case 'token_received':
      this.router.navigate([this.oauthService.state]);
      break;
    }
});

If someone see a possible issue here pls let me know. Thxs!

Let's say that we haven't received the token. How to capture that scenario here ?

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

No branches or pull requests

6 participants