Skip to content

Commit ae7ad3b

Browse files
authored
fix: Refactor login-form according to flask jwt extended changes (#3301)
1 parent 665e870 commit ae7ad3b

File tree

2 files changed

+63
-59
lines changed

2 files changed

+63
-59
lines changed

app/components/forms/login-form.js

Lines changed: 62 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
import Component from '@ember/component';
22
import FormMixin from 'open-event-frontend/mixins/form';
3+
import { action } from '@ember/object';
34

4-
export default Component.extend(FormMixin, {
5+
export default class extends Component.extend(FormMixin) {
56

6-
identification : '',
7-
password : '',
8-
isLoading : false,
7+
identification = '';
8+
password = '';
9+
isLoading = false;
910

1011

1112
getValidationRules() {
@@ -38,70 +39,73 @@ export default Component.extend(FormMixin, {
3839
}
3940
}
4041
};
41-
},
42+
}
4243

43-
actions: {
44-
submit() {
45-
this.onValid(() => {
46-
let credentials = this.getProperties('identification', 'password'),
47-
authenticator = 'authenticator:jwt';
44+
@action
45+
async submit() {
46+
this.onValid(async() => {
47+
let credentials = this.getProperties('identification', 'password'),
48+
authenticator = 'authenticator:jwt';
49+
this.setProperties({
50+
errorMessage : null,
51+
isLoading : true
52+
});
53+
try {
54+
await this.session.authenticate(authenticator, credentials);
55+
const tokenPayload = this.authManager.getTokenPayload();
56+
if (tokenPayload) {
57+
this.authManager.persistCurrentUser(
58+
await this.store.findRecord('user', tokenPayload.identity)
59+
);
4860

49-
this.set('errorMessage', null);
50-
this.set('isLoading', true);
61+
}
62+
} catch (e) {
63+
if (e.error) {
64+
this.set('errorMessage', this.l10n.tVar(e.error));
65+
} else {
66+
this.set('errorMessage', this.l10n.t('An unexpected error occurred.'));
67+
}
68+
}
5169

52-
this.session
53-
.authenticate(authenticator, credentials)
54-
.then(async() => {
55-
const tokenPayload = this.authManager.getTokenPayload();
56-
if (tokenPayload) {
57-
this.authManager.persistCurrentUser(
58-
await this.store.findRecord('user', tokenPayload.identity)
59-
);
60-
}
61-
})
62-
.catch(reason => {
63-
if (!(this.isDestroyed || this.isDestroying)) {
64-
if (reason && reason.hasOwnProperty('status_code') && reason.status_code === 401) {
65-
this.set('errorMessage', this.l10n.t('Your credentials were incorrect.'));
66-
} else {
67-
this.set('errorMessage', this.l10n.t('An unexpected error occurred.'));
68-
}
69-
this.set('isLoading', false);
70-
} else {
71-
console.warn(reason);
72-
}
73-
})
74-
.finally(() => {
75-
if (!(this.isDestroyed || this.isDestroying)) {
76-
this.set('password', '');
77-
}
70+
if (!(this.isDestroyed || this.isDestroying)) {
71+
this.setProperties(
72+
{
73+
password : '',
74+
isLoading : false
7875
});
79-
});
80-
},
76+
}
77+
});
78+
}
8179

82-
async auth(provider) {
80+
@action
81+
async auth(provider) {
82+
if (provider === 'facebook') {
8383
try {
84-
if (provider === 'facebook') {
85-
this.loader.load('/auth/oauth/facebook')
86-
.then(async response => {
87-
window.location.replace(response.url);
88-
});
84+
let response = await this.loader.load('/auth/oauth/facebook');
85+
window.location.replace(response.url);
86+
} catch (e) {
87+
if (e.message) {
88+
this.notify.error(this.l10n.tVar(e.message));
89+
} else {
90+
this.notify.error(this.l10n.t('An unexpected error has occurred'));
8991
}
90-
} catch (error) {
91-
this.notify.error(this.l10n.t(error.message));
9292
}
93-
},
94-
95-
showPassword() {
96-
this.toggleProperty('showPass');
9793
}
98-
},
94+
}
95+
96+
@action
97+
showPassword() {
98+
this.toggleProperty('showPass');
99+
}
100+
99101

100102
didInsertElement() {
101-
if (this.get('session.newUser')) {
102-
this.set('newUser', this.get('session.newUser'));
103-
this.set('identification', this.get('session.newUser'));
104-
this.set('session.newUser', null);
103+
if (this.session.newUser) {
104+
this.setProperties({
105+
newUser : this.session.newUser,
106+
identification : this.session.newUser
107+
});
108+
this.session.set('newUser', null);
105109
}
106110
}
107-
});
111+
}

tests/acceptance/login-test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ module('Acceptance | login', function(hooks) {
2121
await login(assert, '[email protected]', 'wrong_password');
2222
assert.equal(currentURL(), '/login');
2323
assert.dom('.ui.negative.message').exists();
24-
assert.dom('.ui.negative.message').hasText('Your credentials were incorrect.', 'Error message displayed');
24+
assert.dom('.ui.negative.message').hasText('Invalid Credentials', 'Error message displayed');
2525
});
2626

2727
test('logout at /logout', async function(assert) {

0 commit comments

Comments
 (0)