Skip to content

Commit 3f02a78

Browse files
Merge pull request #648 from jonyeezs/instructions/redirect-when-no-tokens
Add more guides on another way to use loadDiscoveryDocumentAndTryLogin
2 parents 13c1675 + 3226bf1 commit 3f02a78

File tree

1 file changed

+40
-1
lines changed

1 file changed

+40
-1
lines changed

README.md

+40-1
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,46 @@ The following snippet contains the template for the login page:
222222

223223
If you don't want to display a login form that tells the user that they are redirected to the identity server, you can use the convenience function ``this.oauthService.loadDiscoveryDocumentAndLogin();`` instead of ``this.oauthService.loadDiscoveryDocumentAndTryLogin();`` when setting up the library.
224224

225-
This directly redirects the user to the identity server if there are no valid tokens.
225+
This directly redirects the user to the identity server if there are no valid tokens. Ensure you have your `issuer` set to your discovery document endpoint!
226+
227+
228+
#### Manually skipping
229+
230+
This is sort of what ``this.oauthService.loadDiscoveryDocumentAndLogin();`` is doing under the hood. But this gives you a fair bit more control
231+
232+
```TypeScript
233+
this.oauthService
234+
.loadDiscoveryDocumentAndTryLogin(/* { your LoginOptions }*/) // checks to see if the current url contains id token and access token
235+
.(hasReceivedTokens => {
236+
// this would have stored all the tokens needed
237+
if (hasReceivedTokens) {
238+
// carry on with your app
239+
return Promise.resolve();
240+
241+
/* if you wish to do something when the user receives tokens from the identity server,
242+
* use the event stream or the `onTokenReceived` callback in LoginOptions.
243+
*
244+
* this.oauthService.events(filter(e => e.type === 'token_received')).subscribe()
245+
*/
246+
} else {
247+
// may want to check if you were previously authenticated
248+
if (this.oauthService.hasValidAccessToken() && this.oauthService.hasValidIdToken()) {
249+
return Promise.resolve();
250+
} else {
251+
// to safe guard this from progressing through the calling promise,
252+
// resolve it when it directed to the sign up page
253+
return new Promise(resolve => {
254+
this.oauthService.initLoginFlow();
255+
// example if you are using explicit flow
256+
this.window.addEventListener('unload', () => {
257+
resolve(true);
258+
});
259+
});
260+
}
261+
}
262+
})
263+
```
264+
226265

227266
### Calling a Web API with an Access Token
228267

0 commit comments

Comments
 (0)