1 import { AfterViewInit, Component, ElementRef, OnInit, ViewChild } from '@angular/core'
2 import { ActivatedRoute, Router } from '@angular/router'
3 import { AuthService, Notifier, RedirectService, SessionStorageService, UserService } from '@app/core'
4 import { HooksService } from '@app/core/plugins/hooks.service'
5 import { LOGIN_PASSWORD_VALIDATOR, LOGIN_USERNAME_VALIDATOR } from '@app/shared/form-validators/login-validators'
6 import { USER_OTP_TOKEN_VALIDATOR } from '@app/shared/form-validators/user-validators'
7 import { FormReactive, FormReactiveService, InputTextComponent } 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'
15 templateUrl: './login.component.html',
16 styleUrls: [ './login.component.scss' ]
19 export class LoginComponent extends FormReactive implements OnInit, AfterViewInit {
20 private static SESSION_STORAGE_REDIRECT_URL_KEY = 'login-previous-url'
22 @ViewChild('forgotPasswordModal', { static: true }) forgotPasswordModal: ElementRef
23 @ViewChild('otpTokenInput') otpTokenInput: InputTextComponent
25 accordion: NgbAccordion
27 forgotPasswordEmail = ''
29 isAuthenticatedWithExternalAuth = false
30 externalAuthError = false
31 externalLogins: string[] = []
33 instanceInformationPanels = {
35 administrators: false,
43 private openedForgotPasswordModal: NgbModalRef
44 private serverConfig: ServerConfig
47 protected formReactiveService: FormReactiveService,
48 private route: ActivatedRoute,
49 private modalService: NgbModal,
50 private authService: AuthService,
51 private userService: UserService,
52 private redirectService: RedirectService,
53 private notifier: Notifier,
54 private hooks: HooksService,
55 private storage: SessionStorageService,
56 private router: Router
61 get signupAllowed () {
62 return this.serverConfig.signup.allowed === true
66 return this.serverConfig.instance.name
69 onTermsClick (event: Event, instanceInformation: HTMLElement) {
70 event.preventDefault()
73 this.accordion.expand('terms')
74 instanceInformation.scrollIntoView({ behavior: 'smooth' })
79 return this.serverConfig.email.enabled === false
83 const snapshot = this.route.snapshot
85 // Avoid undefined errors when accessing form error properties
87 username: LOGIN_USERNAME_VALIDATOR,
88 password: LOGIN_PASSWORD_VALIDATOR,
90 VALIDATORS: [], // Will be set dynamically
91 MESSAGES: USER_OTP_TOKEN_VALIDATOR.MESSAGES
95 this.serverConfig = snapshot.data.serverConfig
97 if (snapshot.queryParams.externalAuthToken) {
98 this.loadExternalAuthToken(snapshot.queryParams.username, snapshot.queryParams.externalAuthToken)
102 if (snapshot.queryParams.externalAuthError) {
103 this.externalAuthError = true
107 const previousUrl = this.redirectService.getPreviousUrl()
108 if (previousUrl && previousUrl !== '/') {
109 this.storage.setItem(LoginComponent.SESSION_STORAGE_REDIRECT_URL_KEY, previousUrl)
114 this.hooks.runAction('action:login.init', 'login')
117 getExternalLogins () {
118 return this.serverConfig.plugin.registeredExternalAuths
121 getAuthHref (auth: RegisteredExternalAuthConfig) {
122 return PluginsManager.getExternalAuthHref(auth)
129 username: this.form.value['username'],
130 password: this.form.value['password'],
131 otpToken: this.form.value['otp-token']
134 this.authService.login(options)
137 next: () => this.redirectService.redirectToPreviousRoute(),
140 this.handleError(err)
145 askResetPassword () {
146 this.userService.askResetPassword(this.forgotPasswordEmail)
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.`
152 this.notifier.success(message)
153 this.hideForgotPasswordModal()
156 error: err => this.notifier.error(err.message)
160 openForgotPasswordModal () {
161 this.openedForgotPasswordModal = this.modalService.open(this.forgotPasswordModal)
164 hideForgotPasswordModal () {
165 this.openedForgotPasswordModal.close()
168 onInstanceAboutAccordionInit (instanceAboutAccordion: InstanceAboutAccordionComponent) {
169 this.accordion = instanceAboutAccordion.accordion
172 hasUsernameUppercase () {
173 return this.form.value['username'].match(/[A-Z]/)
176 private loadExternalAuthToken (username: string, token: string) {
177 this.isAuthenticatedWithExternalAuth = true
179 this.authService.login({ username, password: null, token })
182 const redirectUrl = this.storage.getItem(LoginComponent.SESSION_STORAGE_REDIRECT_URL_KEY)
184 this.storage.removeItem(LoginComponent.SESSION_STORAGE_REDIRECT_URL_KEY)
185 return this.router.navigateByUrl(redirectUrl)
188 this.redirectService.redirectToLatestSessionRoute()
192 this.handleError(err)
193 this.isAuthenticatedWithExternalAuth = false
198 private handleError (err: any) {
199 if (this.authService.isOTPMissingError(err)) {
203 this.form.get('otp-token').setValidators(USER_OTP_TOKEN_VALIDATOR.VALIDATORS)
204 this.otpTokenInput.focus()
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.`
212 else this.error = err.message