1
1
import Component from '@ember/component' ;
2
2
import FormMixin from 'open-event-frontend/mixins/form' ;
3
+ import { action } from '@ember/object' ;
3
4
4
- export default Component . extend ( FormMixin , {
5
+ export default class extends Component . extend ( FormMixin ) {
5
6
6
- identification : '' ,
7
- password : '' ,
8
- isLoading : false ,
7
+ identification = '' ;
8
+ password = '' ;
9
+ isLoading = false ;
9
10
10
11
11
12
getValidationRules ( ) {
@@ -38,70 +39,73 @@ export default Component.extend(FormMixin, {
38
39
}
39
40
}
40
41
} ;
41
- } ,
42
+ }
42
43
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
+ ) ;
48
60
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
+ }
51
69
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
78
75
} ) ;
79
- } ) ;
80
- } ,
76
+ }
77
+ } ) ;
78
+ }
81
79
82
- async auth ( provider ) {
80
+ @action
81
+ async auth ( provider ) {
82
+ if ( provider === 'facebook' ) {
83
83
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' ) ) ;
89
91
}
90
- } catch ( error ) {
91
- this . notify . error ( this . l10n . t ( error . message ) ) ;
92
92
}
93
- } ,
94
-
95
- showPassword ( ) {
96
- this . toggleProperty ( 'showPass' ) ;
97
93
}
98
- } ,
94
+ }
95
+
96
+ @action
97
+ showPassword ( ) {
98
+ this . toggleProperty ( 'showPass' ) ;
99
+ }
100
+
99
101
100
102
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 ) ;
105
109
}
106
110
}
107
- } ) ;
111
+ }
0 commit comments