Skip to content

Commit 1804dce

Browse files
Merge pull request #1124 from pirminrehm/master
Preserve requested route for code flow
2 parents c201f82 + 767e2e6 commit 1804dce

File tree

2 files changed

+29
-2
lines changed

2 files changed

+29
-2
lines changed

projects/lib/src/auth.config.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,12 @@ export class AuthConfig {
257257
*/
258258
public disablePKCE? = false;
259259

260+
/**
261+
* Set this to true to preserve the requested route including query parameters after code flow login.
262+
* This setting enables deep linking for the code flow.
263+
*/
264+
public preserveRequestedRoute? = false;
265+
260266
constructor(json?: Partial<AuthConfig>) {
261267
if (json) {
262268
Object.assign(this, json);

projects/lib/src/oauth-service.ts

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1714,7 +1714,7 @@ export class OAuthService extends AuthConfig implements OnDestroy {
17141714
return this.urlHelper.parseQueryString(queryString);
17151715
}
17161716

1717-
public tryLoginCodeFlow(options: LoginOptions = null): Promise<void> {
1717+
public async tryLoginCodeFlow(options: LoginOptions = null): Promise<void> {
17181718
options = options || {};
17191719

17201720
const querySource = options.customHashFragment
@@ -1761,6 +1761,7 @@ export class OAuthService extends AuthConfig implements OnDestroy {
17611761

17621762
if (!options.disableNonceCheck) {
17631763
if (!nonceInState) {
1764+
this.saveRequestedRoute();
17641765
return Promise.resolve();
17651766
}
17661767

@@ -1776,11 +1777,31 @@ export class OAuthService extends AuthConfig implements OnDestroy {
17761777
this.storeSessionState(sessionState);
17771778

17781779
if (code) {
1779-
return this.getTokenFromCode(code, options).then((_) => null);
1780+
await this.getTokenFromCode(code, options);
1781+
this.restoreRequestedRoute();
1782+
return Promise.resolve();
17801783
} else {
17811784
return Promise.resolve();
17821785
}
17831786
}
1787+
1788+
return Promise.reject();
1789+
}
1790+
1791+
private saveRequestedRoute() {
1792+
if (this.config.preserveRequestedRoute) {
1793+
this._storage.setItem(
1794+
'requested_route',
1795+
window.location.pathname + window.location.search
1796+
);
1797+
}
1798+
}
1799+
1800+
private restoreRequestedRoute() {
1801+
const requestedRoute = this._storage.getItem('requested_route');
1802+
if (requestedRoute) {
1803+
history.replaceState(null, '', window.location.origin + requestedRoute);
1804+
}
17841805
}
17851806

17861807
/**

0 commit comments

Comments
 (0)