Skip to content
This repository has been archived by the owner on Jan 26, 2025. It is now read-only.

Commit

Permalink
🐛 Fixes redirect back to protected page (#137)
Browse files Browse the repository at this point in the history
  • Loading branch information
jmelberg-okta committed Mar 19, 2018
1 parent c7248b3 commit 79280a7
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 6 deletions.
3 changes: 2 additions & 1 deletion packages/okta-angular/src/okta/okta.guard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ export class OktaAuthGuard implements CanActivate {
/**
* Store the current path
*/
this.oktaAuth.setFromUri(state.url);
const path = state.url.split(/[?#]/)[0];
this.oktaAuth.setFromUri(path, route.queryParams);

/**
* Redirect to the given path or
Expand Down
17 changes: 13 additions & 4 deletions packages/okta-angular/src/okta/okta.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
*/

import { Inject, Injectable, Optional } from '@angular/core';
import { Router } from '@angular/router';
import { Router, NavigationExtras } from '@angular/router';

import { OKTA_CONFIG } from './okta.config';

Expand Down Expand Up @@ -134,9 +134,14 @@ export class OktaAuthService {
/**
* Stores the intended path to redirect after successful login.
* @param uri
* @param queryParams
*/
setFromUri(uri) {
localStorage.setItem('referrerPath', uri);
setFromUri(uri, queryParams) {
const json = JSON.stringify({
uri: uri,
params: queryParams
});
localStorage.setItem('referrerPath', json);
}

/**
Expand Down Expand Up @@ -165,7 +170,11 @@ export class OktaAuthService {
/**
* Navigate back to the initial view or root of application.
*/
this.router.navigate([this.getFromUri()]);
const path = JSON.parse(this.getFromUri());
const navigationExtras: NavigationExtras = {
queryParams: path.params
};
this.router.navigate([path.uri], navigationExtras);
}

/**
Expand Down
19 changes: 19 additions & 0 deletions packages/okta-angular/test/e2e/harness/e2e/app.e2e-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,25 @@ describe('Angular + Okta App', () => {
expect(protectedPage.getLoginButton().isPresent()).toBeTruthy();
});

it('should preserve query paramaters after redirecting to Okta', () => {
protectedPage.navigateToWithQuery();

oktaLoginPage.waitUntilVisible();
oktaLoginPage.signIn({
username: environment.USERNAME,
password: environment.PASSWORD
});

protectedPage.waitUntilQueryVisible();
expect(protectedPage.getLogoutButton().isPresent()).toBeTruthy();

/**
* Logout
*/
protectedPage.getLogoutButton().click();
expect(protectedPage.getLoginButton().isPresent()).toBeTruthy();
});

/**
* Hack to slowdown the tests due to the Okta session
* not being removed in time for the second login call.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,18 @@ export class ProtectedPage {
return browser.get('/protected');
}

navigateToWithQuery() {
return browser.get('/protected/foo?state=bar');
}

waitUntilVisible() {
browser.wait(ExpectedConditions.urlContains('/protected'), 5000);
}

waitUntilQueryVisible() {
browser.wait(ExpectedConditions.urlContains('/protected/foo?state=bar'), 5000);
}

getLogoutButton() {
return element(by.id('logout-button'));
}
Expand Down
9 changes: 8 additions & 1 deletion packages/okta-angular/test/e2e/harness/src/app/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,14 @@ const appRoutes: Routes = [
{
path: 'protected',
component: ProtectedComponent,
canActivate: [ OktaAuthGuard ]
canActivate: [ OktaAuthGuard ],
children: [
{
path: 'foo',
component: ProtectedComponent,
canActivate: [ OktaAuthGuard ]
}
]
},
{
path: 'protected-with-data',
Expand Down

0 comments on commit 79280a7

Please sign in to comment.