]>
Commit | Line | Data |
---|---|---|
1 | import { environment } from 'src/environments/environment' | |
2 | import { AfterViewInit, Component, ElementRef, OnInit, ViewChild } from '@angular/core' | |
3 | import { ActivatedRoute } from '@angular/router' | |
4 | import { AuthService, Notifier, RedirectService, UserService } from '@app/core' | |
5 | import { HooksService } from '@app/core/plugins/hooks.service' | |
6 | import { FormReactive, FormValidatorService, LoginValidatorsService } from '@app/shared/shared-forms' | |
7 | import { NgbModal, NgbModalRef } from '@ng-bootstrap/ng-bootstrap' | |
8 | import { I18n } from '@ngx-translate/i18n-polyfill' | |
9 | import { RegisteredExternalAuthConfig, ServerConfig } from '@shared/models' | |
10 | ||
11 | @Component({ | |
12 | selector: 'my-login', | |
13 | templateUrl: './login.component.html', | |
14 | styleUrls: [ './login.component.scss' ] | |
15 | }) | |
16 | ||
17 | export class LoginComponent extends FormReactive implements OnInit, AfterViewInit { | |
18 | @ViewChild('usernameInput', { static: false }) usernameInput: ElementRef | |
19 | @ViewChild('forgotPasswordModal', { static: true }) forgotPasswordModal: ElementRef | |
20 | ||
21 | error: string = null | |
22 | forgotPasswordEmail = '' | |
23 | ||
24 | isAuthenticatedWithExternalAuth = false | |
25 | externalAuthError = false | |
26 | externalLogins: string[] = [] | |
27 | ||
28 | private openedForgotPasswordModal: NgbModalRef | |
29 | private serverConfig: ServerConfig | |
30 | ||
31 | constructor ( | |
32 | protected formValidatorService: FormValidatorService, | |
33 | private route: ActivatedRoute, | |
34 | private modalService: NgbModal, | |
35 | private loginValidatorsService: LoginValidatorsService, | |
36 | private authService: AuthService, | |
37 | private userService: UserService, | |
38 | private redirectService: RedirectService, | |
39 | private notifier: Notifier, | |
40 | private hooks: HooksService, | |
41 | private i18n: I18n | |
42 | ) { | |
43 | super() | |
44 | } | |
45 | ||
46 | get signupAllowed () { | |
47 | return this.serverConfig.signup.allowed === true | |
48 | } | |
49 | ||
50 | isEmailDisabled () { | |
51 | return this.serverConfig.email.enabled === false | |
52 | } | |
53 | ||
54 | ngOnInit () { | |
55 | const snapshot = this.route.snapshot | |
56 | ||
57 | this.serverConfig = snapshot.data.serverConfig | |
58 | ||
59 | if (snapshot.queryParams.externalAuthToken) { | |
60 | this.loadExternalAuthToken(snapshot.queryParams.username, snapshot.queryParams.externalAuthToken) | |
61 | return | |
62 | } | |
63 | ||
64 | if (snapshot.queryParams.externalAuthError) { | |
65 | this.externalAuthError = true | |
66 | return | |
67 | } | |
68 | ||
69 | this.buildForm({ | |
70 | username: this.loginValidatorsService.LOGIN_USERNAME, | |
71 | password: this.loginValidatorsService.LOGIN_PASSWORD | |
72 | }) | |
73 | } | |
74 | ||
75 | ngAfterViewInit () { | |
76 | if (this.usernameInput) { | |
77 | this.usernameInput.nativeElement.focus() | |
78 | } | |
79 | ||
80 | this.hooks.runAction('action:login.init', 'login') | |
81 | } | |
82 | ||
83 | getExternalLogins () { | |
84 | return this.serverConfig.plugin.registeredExternalAuths | |
85 | } | |
86 | ||
87 | getAuthHref (auth: RegisteredExternalAuthConfig) { | |
88 | return environment.apiUrl + `/plugins/${auth.name}/${auth.version}/auth/${auth.authName}` | |
89 | } | |
90 | ||
91 | login () { | |
92 | this.error = null | |
93 | ||
94 | const { username, password } = this.form.value | |
95 | ||
96 | this.authService.login(username, password) | |
97 | .subscribe( | |
98 | () => this.redirectService.redirectToPreviousRoute(), | |
99 | ||
100 | err => this.handleError(err) | |
101 | ) | |
102 | } | |
103 | ||
104 | askResetPassword () { | |
105 | this.userService.askResetPassword(this.forgotPasswordEmail) | |
106 | .subscribe( | |
107 | () => { | |
108 | const message = this.i18n( | |
109 | 'An email with the reset password instructions will be sent to {{email}}. The link will expire within 1 hour.', | |
110 | { email: this.forgotPasswordEmail } | |
111 | ) | |
112 | this.notifier.success(message) | |
113 | this.hideForgotPasswordModal() | |
114 | }, | |
115 | ||
116 | err => this.notifier.error(err.message) | |
117 | ) | |
118 | } | |
119 | ||
120 | openForgotPasswordModal () { | |
121 | this.openedForgotPasswordModal = this.modalService.open(this.forgotPasswordModal) | |
122 | } | |
123 | ||
124 | hideForgotPasswordModal () { | |
125 | this.openedForgotPasswordModal.close() | |
126 | } | |
127 | ||
128 | private loadExternalAuthToken (username: string, token: string) { | |
129 | this.isAuthenticatedWithExternalAuth = true | |
130 | ||
131 | this.authService.login(username, null, token) | |
132 | .subscribe( | |
133 | () => this.redirectService.redirectToPreviousRoute(), | |
134 | ||
135 | err => { | |
136 | this.handleError(err) | |
137 | this.isAuthenticatedWithExternalAuth = false | |
138 | } | |
139 | ) | |
140 | } | |
141 | ||
142 | private handleError (err: any) { | |
143 | if (err.message.indexOf('credentials are invalid') !== -1) this.error = this.i18n('Incorrect username or password.') | |
144 | else if (err.message.indexOf('blocked') !== -1) this.error = this.i18n('You account is blocked.') | |
145 | else this.error = err.message | |
146 | } | |
147 | } |