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
Browse files Browse the repository at this point in the history
  • Loading branch information
jmelberg-okta committed Mar 6, 2018
1 parent f47170b commit fedb7bc
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 6 deletions.
2 changes: 1 addition & 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,7 @@ export class OktaAuthGuard implements CanActivate {
/**
* Store the current path
*/
this.oktaAuth.setFromUri(state.url);
this.oktaAuth.setFromUri(route.url[0].path, route.queryParams);

/**
* Redirect to the given path or
Expand Down
19 changes: 14 additions & 5 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 @@ -111,10 +111,15 @@ export class OktaAuthService {

/**
* Stores the intended path to redirect after successful login.
* @param uri
* @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 @@ -143,7 +148,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 @@ -55,6 +55,25 @@ describe('Angular + Okta App', () => {
expect(protectedPage.getLoginButton().isPresent()).toBeTruthy();
});

it('should redirect to Okta for login when trying to access a protected page with query params', () => {
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?state=foo');
}

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

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

getLogoutButton() {
return element(by.id('logout-button'));
}
Expand Down

0 comments on commit fedb7bc

Please sign in to comment.