Skip to content
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

Fix IE default redirect url #1373

Merged
merged 3 commits into from
May 23, 2018
Merged

Fix IE default redirect url #1373

merged 3 commits into from
May 23, 2018

Conversation

luisrudge
Copy link
Contributor

fix #1361

Copy link
Contributor

@lbalmaceda lbalmaceda left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💯

@@ -37,6 +37,15 @@ describe('setup', () => {
const model = mock.calls[0][1].toJS();
expect(model.auth.redirectUrl).toBe('https://test.com/path/');
});
it('default redirectUrl should work when `window.location.origin` is not available', () => {
setURL('https://test.com/path/#not-this-part', { noOrigin: true });
const options = {};
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

any reason why you have the declaration separate from the usage, since you're hardcoding the rest of the parameters too?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

easy to copy/paste later if needed 😬

var url = 'https://test.com/example';
expect(getOriginFromUrl(url)).toBe('https://test.com');
});
it('should use add the `port` when available', function() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should use add

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use add include

const mapping = {
'https://localhost:3000/foo?id=1': {
href: 'https://localhost:3000/foo?id=1',
protocol: 'https:',
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

protocol should not include the :.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, the function does return the protocol with a colon:

// http://banana.com
host:"banana.com"
hostname:"banana.com"
href:"http://banana.com"
protocol:"http:"

Wikipedia says that protocol is:

A non-empty scheme component followed by a colon

... which tells me that protocol should not include it. That said, type this into your console:

> window.location
...
< protocol: "https:"

The built-in URL object (MDN) has the colon so I think this is the right implementation. Again, in the console:

> var url = new URL(window.location.toString()); url;
...
< protocol: "https:"

Copy link
Contributor Author

@luisrudge luisrudge May 14, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lucho is discovering all the greatness of javascript. our boy is growing 😝

protocol: 'https:',
host: 'auth0.com',
hostname: 'auth0.com',
port: undefined,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should you return undefined or 80 (default)?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's text parsing, silly! No port in the text === no port found

search: '',
hash: ''
},
'https://auth0.com#access_token=foo': {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If I enter manage.auth0.com, the # is put after the /. .e.g.: https://manage.auth0.com/#/applications

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

/# and # is the same thing

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried:

So as how I see it, I don't think they are the same. Maybe I'm wrong? Anyway, just add an extra case to test that scenario where the # is after the / such as https://manage.auth0.com/#/applications.

port: undefined,
pathname: '',
search: '',
hash: '#access_token=foo'
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why does hash includes the #? Same as port property, it shouldn't. It's already implied in the property name 😂

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as protocol above, browser parsing with URL includes the #

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah, it's what window.location.hash returns

@@ -273,7 +273,8 @@ function extractAuthOptions(options) {
// if responseType was not set and there is a redirectUrl, it defaults to code. Otherwise token.
responseType = typeof responseType === 'string' ? responseType : redirectUrl ? 'code' : 'token';
// now we set the default because we already did the validation
redirectUrl = redirectUrl || `${window.location.origin}${window.location.pathname}`;
redirectUrl =
redirectUrl || `${getOriginFromUrl(window.location.href)}${window.location.pathname}`;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

does this alternative of not using window.location.origin work in every scenario?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, if anything, it's way more reliable, since works across all browsers.

expect(getOriginFromUrl(null)).toBe(undefined);
});
it('should use an anchor to parse the url and return the origin', function() {
var url = 'https://test.com/example';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what about http URLs?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pushed some tests with http

@@ -1,5 +1,29 @@
export function parseUrl(str) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it breaking to remove this?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nope. internal use only

});
});
describe('getLocationFromUrl', function() {
const mapping = {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might want to add a subdomain test here

const mapping = {
'https://localhost:3000/foo?id=1': {
href: 'https://localhost:3000/foo?id=1',
protocol: 'https:',
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, the function does return the protocol with a colon:

// http://banana.com
host:"banana.com"
hostname:"banana.com"
href:"http://banana.com"
protocol:"http:"

Wikipedia says that protocol is:

A non-empty scheme component followed by a colon

... which tells me that protocol should not include it. That said, type this into your console:

> window.location
...
< protocol: "https:"

The built-in URL object (MDN) has the colon so I think this is the right implementation. Again, in the console:

> var url = new URL(window.location.toString()); url;
...
< protocol: "https:"

var url = 'https://test.com/example';
expect(getOriginFromUrl(url)).toBe('https://test.com');
});
it('should use add the `port` when available', function() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use add include

protocol: 'https:',
host: 'auth0.com',
hostname: 'auth0.com',
port: undefined,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's text parsing, silly! No port in the text === no port found

port: undefined,
pathname: '',
search: '',
hash: '#access_token=foo'
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as protocol above, browser parsing with URL includes the #

}

export function getOriginFromUrl(url) {
if (!url) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're catching an empty string here but ... 👇

return undefined;
}
var parsed = getLocationFromUrl(url);
var origin = parsed.protocol + '//' + parsed.hostname;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

... this is also a source of an error if you just pass in a string:

> getLocationFromUrl('banana')
< null

If there is no match then null is returned.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good catch. pushed a fix.

Copy link
Contributor

@lbalmaceda lbalmaceda left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🎉

@luisrudge luisrudge merged commit a28f31b into master May 23, 2018
@luisrudge luisrudge deleted the fix-origin-ie branch May 23, 2018 18:34
@luisrudge luisrudge added this to the v11.7.0 milestone May 28, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
3 participants