]>
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 { LOGIN_PASSWORD_VALIDATOR, LOGIN_USERNAME_VALIDATOR } from '@app/shared/form-validators/login-validators' | |
7 | import { FormReactive, FormValidatorService } from '@app/shared/shared-forms' | |
8 | import { NgbModal, NgbModalRef } from '@ng-bootstrap/ng-bootstrap' | |
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 authService: AuthService, | |
36 | private userService: UserService, | |
37 | private redirectService: RedirectService, | |
38 | private notifier: Notifier, | |
39 | private hooks: HooksService | |
40 | ) { | |
41 | super() | |
42 | } | |
43 | ||
44 | get signupAllowed () { | |
45 | return this.serverConfig.signup.allowed === true | |
46 | } | |
47 | ||
48 | isEmailDisabled () { | |
49 | return this.serverConfig.email.enabled === false | |
50 | } | |
51 | ||
52 | ngOnInit () { | |
53 | const snapshot = this.route.snapshot | |
54 | ||
55 | this.serverConfig = snapshot.data.serverConfig | |
56 | ||
57 | if (snapshot.queryParams.externalAuthToken) { | |
58 | this.loadExternalAuthToken(snapshot.queryParams.username, snapshot.queryParams.externalAuthToken) | |
59 | return | |
60 | } | |
61 | ||
62 | if (snapshot.queryParams.externalAuthError) { | |
63 | this.externalAuthError = true | |
64 | return | |
65 | } | |
66 | ||
67 | this.buildForm({ | |
68 | username: LOGIN_USERNAME_VALIDATOR, | |
69 | password: LOGIN_PASSWORD_VALIDATOR | |
70 | }) | |
71 | } | |
72 | ||
73 | ngAfterViewInit () { | |
74 | if (this.usernameInput) { | |
75 | this.usernameInput.nativeElement.focus() | |
76 | } | |
77 | ||
78 | this.hooks.runAction('action:login.init', 'login') | |
79 | } | |
80 | ||
81 | getExternalLogins () { | |
82 | return this.serverConfig.plugin.registeredExternalAuths | |
83 | } | |
84 | ||
85 | getAuthHref (auth: RegisteredExternalAuthConfig) { | |
86 | return environment.apiUrl + `/plugins/${auth.name}/${auth.version}/auth/${auth.authName}` | |
87 | } | |
88 | ||
89 | login () { | |
90 | this.error = null | |
91 | ||
92 | const { username, password } = this.form.value | |
93 | ||
94 | this.authService.login(username, password) | |
95 | .subscribe( | |
96 | () => this.redirectService.redirectToPreviousRoute(), | |
97 | ||
98 | err => this.handleError(err) | |
99 | ) | |
100 | } | |
101 | ||
102 | askResetPassword () { | |
103 | this.userService.askResetPassword(this.forgotPasswordEmail) | |
104 | .subscribe( | |
105 | () => { | |
106 | const message = $localize`An email with the reset password instructions will be sent to ${this.forgotPasswordEmail}. | |
107 | The link will expire within 1 hour.` | |
108 | ||
109 | this.notifier.success(message) | |
110 | this.hideForgotPasswordModal() | |
111 | }, | |
112 | ||
113 | err => this.notifier.error(err.message) | |
114 | ) | |
115 | } | |
116 | ||
117 | openForgotPasswordModal () { | |
118 | this.openedForgotPasswordModal = this.modalService.open(this.forgotPasswordModal) | |
119 | } | |
120 | ||
121 | hideForgotPasswordModal () { | |
122 | this.openedForgotPasswordModal.close() | |
123 | } | |
124 | ||
125 | private loadExternalAuthToken (username: string, token: string) { | |
126 | this.isAuthenticatedWithExternalAuth = true | |
127 | ||
128 | this.authService.login(username, null, token) | |
129 | .subscribe( | |
130 | () => this.redirectService.redirectToPreviousRoute(), | |
131 | ||
132 | err => { | |
133 | this.handleError(err) | |
134 | this.isAuthenticatedWithExternalAuth = false | |
135 | } | |
136 | ) | |
137 | } | |
138 | ||
139 | private handleError (err: any) { | |
140 | if (err.message.indexOf('credentials are invalid') !== -1) this.error = $localize`Incorrect username or password.` | |
141 | else if (err.message.indexOf('blocked') !== -1) this.error = $localize`Your account is blocked.` | |
142 | else this.error = err.message | |
143 | } | |
144 | } |