Skip to content
This repository has been archived by the owner on Apr 15, 2019. It is now read-only.

Commit

Permalink
Merge pull request #419 from LiskHQ/417-normalize-url
Browse files Browse the repository at this point in the history
Use Javascript URL method to normalize URL before setting active peer - Closes #417
  • Loading branch information
reyraa authored Jun 16, 2017
2 parents 6a003d5 + b439a6a commit 06ce21e
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 9 deletions.
15 changes: 14 additions & 1 deletion src/components/login/login.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,20 @@ app.component('login', {
this.$cookies.remove('network');
}

this.$scope.$watch('$ctrl.input_passphrase', val => this.valid = this.Passphrase.isValidPassphrase(val));
this.validity = {
url: true,
};

this.$scope.$watch('$ctrl.input_passphrase', val => this.validity.passphrase = this.Passphrase.isValidPassphrase(val));
this.$scope.$watch('$ctrl.network.address', (val) => {
try {
const url = new URL(val);
this.validity.url = url.port !== '';
} catch (e) {
this.validity.url = false;
}
});

this.$timeout(this.devTestAccount.bind(this), 200);

/**
Expand Down
8 changes: 4 additions & 4 deletions src/components/login/login.pug
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,17 @@ md-card
md-select.network(ng-model='$ctrl.network', aria-label='Network')
md-option(ng-repeat='network in $ctrl.networks', ng-value='network') {{ network.name }}
div(ng-if='$ctrl.network.custom')
md-input-container.md-block
md-input-container.md-block(md-is-error='!$ctrl.validity.url')
label.pass Node address
input(type="text", ng-model="$ctrl.network.address")
md-input-container.md-block(md-is-error='$ctrl.valid === 0')
md-input-container.md-block(md-is-error='$ctrl.validity.passphrase === 0')
label.pass Enter your passphrase
input.passphrase(type="{{ $ctrl.show_passphrase ? 'text' : 'password' }}", ng-model='$ctrl.input_passphrase', ng-disabled='$ctrl.generatingNewPassphrase', autofocus)
md-input-container.md-block
md-checkbox.md-primary(ng-model="$ctrl.show_passphrase", aria-label="Show passphrase") Show passphrase
md-content(layout='row', layout-align='center center')
// md-button(ng-disabled='$ctrl.generatingNewPassphrase', ng-click='$ctrl.devTestAccount()') Dev Test Account
md-button.md-primary.new-account-button(ng-disabled='$ctrl.random || $ctrl.generatingNewPassphrase',
md-button.md-primary.new-account-button(ng-disabled='$ctrl.random || $ctrl.generatingNewPassphrase || !$ctrl.validity.url',
data-open-dialog='new-account', data-options='{network: $ctrl.network}') NEW ACCOUNT
md-button.md-raised.md-primary.login-button(md-autofocus, ng-disabled='($ctrl.valid != undefined && $ctrl.valid !== 1) || $root.loggingIn', type='submit') Login
md-button.md-raised.md-primary.login-button(md-autofocus, ng-disabled='($ctrl.validity.passphrase !== 1 || !$ctrl.validity.url) || $root.loggingIn', type='submit') Login
passphrase(ng-if='$ctrl.generatingNewPassphrase', data-on-save='onSave', data-target='primary-pass', data-ok-button-label='Login')
13 changes: 10 additions & 3 deletions src/services/api/peers.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,21 @@ app.factory('Peers', ($timeout, $cookies, $location, $q, $rootScope, dialog) =>
* @method setActive
*/
setActive(network) {
const addHttp = (url) => {
const reg = /^(?:f|ht)tps?:\/\//i;
return reg.test(url) ? url : `http://${url}`;
};

this.network = network;
let conf = { };
if (network) {
conf = network;
if (network.address) {
conf.node = network.address.split(':')[1].replace('//', '');
conf.port = network.address.match(/:([0-9]{1,5})\/?$/) && network.address.match(/:([0-9]{1,5})\/?$/)[1];
conf.ssl = network.address.split(':')[0] === 'https';
const normalizedUrl = new URL(addHttp(network.address));

conf.node = normalizedUrl.hostname;
conf.port = normalizedUrl.port;
conf.ssl = normalizedUrl.protocol === 'https';
}
if (conf.testnet === undefined && conf.port !== undefined) {
conf.testnet = conf.port === '7000';
Expand Down
2 changes: 1 addition & 1 deletion test/components/login/login.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ describe('Login controller', () => {
expect(controller.valid).to.not.equal(1);
controller.input_passphrase = VALID_PASSPHRASE;
$scope.$apply();
expect(controller.valid).to.equal(1);
expect(controller.validity.passphrase).to.equal(1);
expect(spy).to.have.been.calledWith();
});
});
Expand Down

0 comments on commit 06ce21e

Please sign in to comment.