]>
Commit | Line | Data |
---|---|---|
67ed6552 | 1 | import { AfterViewInit, Component, ElementRef, OnInit, ViewChild } from '@angular/core' |
13e7c3b0 | 2 | import { ActivatedRoute, Router } from '@angular/router' |
cb28bb92 | 3 | import { AuthService, Notifier, RedirectService, SessionStorageService, UserService } from '@app/core' |
f375bb3d | 4 | import { HooksService } from '@app/core/plugins/hooks.service' |
7ed1edbb | 5 | import { LOGIN_PASSWORD_VALIDATOR, LOGIN_USERNAME_VALIDATOR } from '@app/shared/form-validators/login-validators' |
d12b40fb | 6 | import { USER_OTP_TOKEN_VALIDATOR } from '@app/shared/form-validators/user-validators' |
5c5bcea2 | 7 | import { FormReactive, FormReactiveService, InputTextComponent } from '@app/shared/shared-forms' |
a3664dfd | 8 | import { InstanceAboutAccordionComponent } from '@app/shared/shared-instance' |
40360c17 | 9 | import { NgbAccordion, NgbModal, NgbModalRef } from '@ng-bootstrap/ng-bootstrap' |
0bc53e20 | 10 | import { PluginsManager } from '@root-helpers/plugins-manager' |
67ed6552 | 11 | import { RegisteredExternalAuthConfig, ServerConfig } from '@shared/models' |
b1794c53 C |
12 | |
13 | @Component({ | |
a840d396 | 14 | selector: 'my-login', |
d235f6b0 C |
15 | templateUrl: './login.component.html', |
16 | styleUrls: [ './login.component.scss' ] | |
b1794c53 C |
17 | }) |
18 | ||
ebefc902 | 19 | export class LoginComponent extends FormReactive implements OnInit, AfterViewInit { |
cb28bb92 C |
20 | private static SESSION_STORAGE_REDIRECT_URL_KEY = 'login-previous-url' |
21 | ||
f36da21e | 22 | @ViewChild('forgotPasswordModal', { static: true }) forgotPasswordModal: ElementRef |
d12b40fb | 23 | @ViewChild('otpTokenInput') otpTokenInput: InputTextComponent |
ecb4e35f | 24 | |
40360c17 | 25 | accordion: NgbAccordion |
df98563e | 26 | error: string = null |
ecb4e35f | 27 | forgotPasswordEmail = '' |
bc90883f | 28 | |
4a8d113b | 29 | isAuthenticatedWithExternalAuth = false |
bc90883f | 30 | externalAuthError = false |
ebefc902 | 31 | externalLogins: string[] = [] |
192ea60b | 32 | |
40360c17 K |
33 | instanceInformationPanels = { |
34 | terms: true, | |
35 | administrators: false, | |
36 | features: false, | |
37 | moderation: false, | |
38 | codeOfConduct: false | |
39 | } | |
40 | ||
d12b40fb C |
41 | otpStep = false |
42 | ||
63347a0f | 43 | private openedForgotPasswordModal: NgbModalRef |
ba430d75 | 44 | private serverConfig: ServerConfig |
63347a0f | 45 | |
b1d40cff | 46 | constructor ( |
5c5bcea2 | 47 | protected formReactiveService: FormReactiveService, |
ba430d75 | 48 | private route: ActivatedRoute, |
63347a0f | 49 | private modalService: NgbModal, |
b1d40cff C |
50 | private authService: AuthService, |
51 | private userService: UserService, | |
b1d40cff | 52 | private redirectService: RedirectService, |
f8b2c1b4 | 53 | private notifier: Notifier, |
cb28bb92 | 54 | private hooks: HooksService, |
13e7c3b0 C |
55 | private storage: SessionStorageService, |
56 | private router: Router | |
9df52d66 | 57 | ) { |
df98563e | 58 | super() |
4b2f33f3 | 59 | } |
b1794c53 | 60 | |
2b084d70 | 61 | get signupAllowed () { |
ba430d75 | 62 | return this.serverConfig.signup.allowed === true |
2b084d70 C |
63 | } |
64 | ||
11056966 C |
65 | get instanceName () { |
66 | return this.serverConfig.instance.name | |
67 | } | |
68 | ||
40360c17 K |
69 | onTermsClick (event: Event, instanceInformation: HTMLElement) { |
70 | event.preventDefault() | |
71 | ||
72 | if (this.accordion) { | |
73 | this.accordion.expand('terms') | |
74 | instanceInformation.scrollIntoView({ behavior: 'smooth' }) | |
75 | } | |
76 | } | |
77 | ||
3b3b1820 | 78 | isEmailDisabled () { |
ba430d75 | 79 | return this.serverConfig.email.enabled === false |
3b3b1820 C |
80 | } |
81 | ||
df98563e | 82 | ngOnInit () { |
4a8d113b C |
83 | const snapshot = this.route.snapshot |
84 | ||
2345e138 C |
85 | // Avoid undefined errors when accessing form error properties |
86 | this.buildForm({ | |
87 | username: LOGIN_USERNAME_VALIDATOR, | |
d12b40fb C |
88 | password: LOGIN_PASSWORD_VALIDATOR, |
89 | 'otp-token': { | |
90 | VALIDATORS: [], // Will be set dynamically | |
91 | MESSAGES: USER_OTP_TOKEN_VALIDATOR.MESSAGES | |
92 | } | |
2345e138 C |
93 | }) |
94 | ||
4a8d113b C |
95 | this.serverConfig = snapshot.data.serverConfig |
96 | ||
97 | if (snapshot.queryParams.externalAuthToken) { | |
98 | this.loadExternalAuthToken(snapshot.queryParams.username, snapshot.queryParams.externalAuthToken) | |
99 | return | |
100 | } | |
ba430d75 | 101 | |
bc90883f C |
102 | if (snapshot.queryParams.externalAuthError) { |
103 | this.externalAuthError = true | |
104 | return | |
105 | } | |
cb28bb92 | 106 | |
13e7c3b0 C |
107 | const previousUrl = this.redirectService.getPreviousUrl() |
108 | if (previousUrl && previousUrl !== '/') { | |
109 | this.storage.setItem(LoginComponent.SESSION_STORAGE_REDIRECT_URL_KEY, previousUrl) | |
110 | } | |
ebefc902 C |
111 | } |
112 | ||
113 | ngAfterViewInit () { | |
f375bb3d | 114 | this.hooks.runAction('action:login.init', 'login') |
ebefc902 C |
115 | } |
116 | ||
117 | getExternalLogins () { | |
118 | return this.serverConfig.plugin.registeredExternalAuths | |
119 | } | |
9fe44067 | 120 | |
ebefc902 | 121 | getAuthHref (auth: RegisteredExternalAuthConfig) { |
0bc53e20 | 122 | return PluginsManager.getExternalAuthHref(auth) |
0f6da32b C |
123 | } |
124 | ||
df98563e C |
125 | login () { |
126 | this.error = null | |
4b2f33f3 | 127 | |
d12b40fb C |
128 | const options = { |
129 | username: this.form.value['username'], | |
130 | password: this.form.value['password'], | |
131 | otpToken: this.form.value['otp-token'] | |
132 | } | |
4b2f33f3 | 133 | |
d12b40fb C |
134 | this.authService.login(options) |
135 | .pipe() | |
1378c0d3 C |
136 | .subscribe({ |
137 | next: () => this.redirectService.redirectToPreviousRoute(), | |
192ea60b | 138 | |
d12b40fb C |
139 | error: err => { |
140 | this.handleError(err) | |
141 | } | |
1378c0d3 | 142 | }) |
b1794c53 | 143 | } |
ecb4e35f C |
144 | |
145 | askResetPassword () { | |
146 | this.userService.askResetPassword(this.forgotPasswordEmail) | |
1378c0d3 C |
147 | .subscribe({ |
148 | next: () => { | |
66357162 C |
149 | const message = $localize`An email with the reset password instructions will be sent to ${this.forgotPasswordEmail}. |
150 | The link will expire within 1 hour.` | |
151 | ||
f8b2c1b4 | 152 | this.notifier.success(message) |
ecb4e35f C |
153 | this.hideForgotPasswordModal() |
154 | }, | |
155 | ||
1378c0d3 C |
156 | error: err => this.notifier.error(err.message) |
157 | }) | |
ecb4e35f C |
158 | } |
159 | ||
ecb4e35f | 160 | openForgotPasswordModal () { |
63347a0f | 161 | this.openedForgotPasswordModal = this.modalService.open(this.forgotPasswordModal) |
ecb4e35f C |
162 | } |
163 | ||
164 | hideForgotPasswordModal () { | |
63347a0f | 165 | this.openedForgotPasswordModal.close() |
ecb4e35f | 166 | } |
4a8d113b | 167 | |
40360c17 K |
168 | onInstanceAboutAccordionInit (instanceAboutAccordion: InstanceAboutAccordionComponent) { |
169 | this.accordion = instanceAboutAccordion.accordion | |
170 | } | |
171 | ||
7f28f2dd C |
172 | hasUsernameUppercase () { |
173 | return this.form.value['username'].match(/[A-Z]/) | |
174 | } | |
175 | ||
4a8d113b C |
176 | private loadExternalAuthToken (username: string, token: string) { |
177 | this.isAuthenticatedWithExternalAuth = true | |
178 | ||
d12b40fb | 179 | this.authService.login({ username, password: null, token }) |
1378c0d3 | 180 | .subscribe({ |
cb28bb92 | 181 | next: () => { |
13e7c3b0 C |
182 | const redirectUrl = this.storage.getItem(LoginComponent.SESSION_STORAGE_REDIRECT_URL_KEY) |
183 | if (redirectUrl) { | |
184 | this.storage.removeItem(LoginComponent.SESSION_STORAGE_REDIRECT_URL_KEY) | |
185 | return this.router.navigateByUrl(redirectUrl) | |
186 | } | |
187 | ||
188 | this.redirectService.redirectToLatestSessionRoute() | |
cb28bb92 | 189 | }, |
1378c0d3 C |
190 | |
191 | error: err => { | |
192 | this.handleError(err) | |
193 | this.isAuthenticatedWithExternalAuth = false | |
194 | } | |
195 | }) | |
4a8d113b C |
196 | } |
197 | ||
198 | private handleError (err: any) { | |
d12b40fb C |
199 | if (this.authService.isOTPMissingError(err)) { |
200 | this.otpStep = true | |
201 | ||
202 | setTimeout(() => { | |
203 | this.form.get('otp-token').setValidators(USER_OTP_TOKEN_VALIDATOR.VALIDATORS) | |
204 | this.otpTokenInput.focus() | |
205 | }) | |
206 | ||
207 | return | |
208 | } | |
209 | ||
66357162 C |
210 | if (err.message.indexOf('credentials are invalid') !== -1) this.error = $localize`Incorrect username or password.` |
211 | else if (err.message.indexOf('blocked') !== -1) this.error = $localize`Your account is blocked.` | |
4a8d113b C |
212 | else this.error = err.message |
213 | } | |
b1794c53 | 214 | } |