]>
Commit | Line | Data |
---|---|---|
1 | ||
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 { InstanceAboutAccordionComponent } from '@app/shared/shared-instance' | |
9 | import { NgbAccordion, NgbModal, NgbModalRef } from '@ng-bootstrap/ng-bootstrap' | |
10 | import { PluginsManager } from '@root-helpers/plugins-manager' | |
11 | import { RegisteredExternalAuthConfig, ServerConfig } from '@shared/models' | |
12 | ||
13 | @Component({ | |
14 | selector: 'my-login', | |
15 | templateUrl: './login.component.html', | |
16 | styleUrls: [ './login.component.scss' ] | |
17 | }) | |
18 | ||
19 | export class LoginComponent extends FormReactive implements OnInit, AfterViewInit { | |
20 | @ViewChild('forgotPasswordModal', { static: true }) forgotPasswordModal: ElementRef | |
21 | ||
22 | accordion: NgbAccordion | |
23 | error: string = null | |
24 | forgotPasswordEmail = '' | |
25 | ||
26 | isAuthenticatedWithExternalAuth = false | |
27 | externalAuthError = false | |
28 | externalLogins: string[] = [] | |
29 | ||
30 | instanceInformationPanels = { | |
31 | terms: true, | |
32 | administrators: false, | |
33 | features: false, | |
34 | moderation: false, | |
35 | codeOfConduct: false | |
36 | } | |
37 | ||
38 | private openedForgotPasswordModal: NgbModalRef | |
39 | private serverConfig: ServerConfig | |
40 | ||
41 | constructor ( | |
42 | protected formValidatorService: FormValidatorService, | |
43 | private route: ActivatedRoute, | |
44 | private modalService: NgbModal, | |
45 | private authService: AuthService, | |
46 | private userService: UserService, | |
47 | private redirectService: RedirectService, | |
48 | private notifier: Notifier, | |
49 | private hooks: HooksService | |
50 | ) { | |
51 | super() | |
52 | } | |
53 | ||
54 | get signupAllowed () { | |
55 | return this.serverConfig.signup.allowed === true | |
56 | } | |
57 | ||
58 | onTermsClick (event: Event, instanceInformation: HTMLElement) { | |
59 | event.preventDefault() | |
60 | ||
61 | if (this.accordion) { | |
62 | this.accordion.expand('terms') | |
63 | instanceInformation.scrollIntoView({ behavior: 'smooth' }) | |
64 | } | |
65 | } | |
66 | ||
67 | isEmailDisabled () { | |
68 | return this.serverConfig.email.enabled === false | |
69 | } | |
70 | ||
71 | ngOnInit () { | |
72 | const snapshot = this.route.snapshot | |
73 | ||
74 | // Avoid undefined errors when accessing form error properties | |
75 | this.buildForm({ | |
76 | username: LOGIN_USERNAME_VALIDATOR, | |
77 | password: LOGIN_PASSWORD_VALIDATOR | |
78 | }) | |
79 | ||
80 | this.serverConfig = snapshot.data.serverConfig | |
81 | ||
82 | if (snapshot.queryParams.externalAuthToken) { | |
83 | this.loadExternalAuthToken(snapshot.queryParams.username, snapshot.queryParams.externalAuthToken) | |
84 | return | |
85 | } | |
86 | ||
87 | if (snapshot.queryParams.externalAuthError) { | |
88 | this.externalAuthError = true | |
89 | return | |
90 | } | |
91 | } | |
92 | ||
93 | ngAfterViewInit () { | |
94 | this.hooks.runAction('action:login.init', 'login') | |
95 | } | |
96 | ||
97 | getExternalLogins () { | |
98 | return this.serverConfig.plugin.registeredExternalAuths | |
99 | } | |
100 | ||
101 | getAuthHref (auth: RegisteredExternalAuthConfig) { | |
102 | return PluginsManager.getExternalAuthHref(auth) | |
103 | } | |
104 | ||
105 | login () { | |
106 | this.error = null | |
107 | ||
108 | const { username, password } = this.form.value | |
109 | ||
110 | this.authService.login(username, password) | |
111 | .subscribe({ | |
112 | next: () => this.redirectService.redirectToPreviousRoute(), | |
113 | ||
114 | error: err => this.handleError(err) | |
115 | }) | |
116 | } | |
117 | ||
118 | askResetPassword () { | |
119 | this.userService.askResetPassword(this.forgotPasswordEmail) | |
120 | .subscribe({ | |
121 | next: () => { | |
122 | const message = $localize`An email with the reset password instructions will be sent to ${this.forgotPasswordEmail}. | |
123 | The link will expire within 1 hour.` | |
124 | ||
125 | this.notifier.success(message) | |
126 | this.hideForgotPasswordModal() | |
127 | }, | |
128 | ||
129 | error: err => this.notifier.error(err.message) | |
130 | }) | |
131 | } | |
132 | ||
133 | openForgotPasswordModal () { | |
134 | this.openedForgotPasswordModal = this.modalService.open(this.forgotPasswordModal) | |
135 | } | |
136 | ||
137 | hideForgotPasswordModal () { | |
138 | this.openedForgotPasswordModal.close() | |
139 | } | |
140 | ||
141 | onInstanceAboutAccordionInit (instanceAboutAccordion: InstanceAboutAccordionComponent) { | |
142 | this.accordion = instanceAboutAccordion.accordion | |
143 | } | |
144 | ||
145 | hasUsernameUppercase () { | |
146 | return this.form.value['username'].match(/[A-Z]/) | |
147 | } | |
148 | ||
149 | private loadExternalAuthToken (username: string, token: string) { | |
150 | this.isAuthenticatedWithExternalAuth = true | |
151 | ||
152 | this.authService.login(username, null, token) | |
153 | .subscribe({ | |
154 | next: () => this.redirectService.redirectToPreviousRoute(), | |
155 | ||
156 | error: err => { | |
157 | this.handleError(err) | |
158 | this.isAuthenticatedWithExternalAuth = false | |
159 | } | |
160 | }) | |
161 | } | |
162 | ||
163 | private handleError (err: any) { | |
164 | if (err.message.indexOf('credentials are invalid') !== -1) this.error = $localize`Incorrect username or password.` | |
165 | else if (err.message.indexOf('blocked') !== -1) this.error = $localize`Your account is blocked.` | |
166 | else this.error = err.message | |
167 | } | |
168 | } |