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'
15 templateUrl: './login.component.html',
16 styleUrls: [ './login.component.scss' ]
19 export class LoginComponent extends FormReactive implements OnInit, AfterViewInit {
20 @ViewChild('forgotPasswordModal', { static: true }) forgotPasswordModal: ElementRef
22 accordion: NgbAccordion
24 forgotPasswordEmail = ''
26 isAuthenticatedWithExternalAuth = false
27 externalAuthError = false
28 externalLogins: string[] = []
30 instanceInformationPanels = {
32 administrators: false,
38 private openedForgotPasswordModal: NgbModalRef
39 private serverConfig: ServerConfig
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
54 get signupAllowed () {
55 return this.serverConfig.signup.allowed === true
58 onTermsClick (event: Event, instanceInformation: HTMLElement) {
59 event.preventDefault()
62 this.accordion.expand('terms')
63 instanceInformation.scrollIntoView({ behavior: 'smooth' })
68 return this.serverConfig.email.enabled === false
72 const snapshot = this.route.snapshot
74 // Avoid undefined errors when accessing form error properties
76 username: LOGIN_USERNAME_VALIDATOR,
77 password: LOGIN_PASSWORD_VALIDATOR
80 this.serverConfig = snapshot.data.serverConfig
82 if (snapshot.queryParams.externalAuthToken) {
83 this.loadExternalAuthToken(snapshot.queryParams.username, snapshot.queryParams.externalAuthToken)
87 if (snapshot.queryParams.externalAuthError) {
88 this.externalAuthError = true
94 this.hooks.runAction('action:login.init', 'login')
97 getExternalLogins () {
98 return this.serverConfig.plugin.registeredExternalAuths
101 getAuthHref (auth: RegisteredExternalAuthConfig) {
102 return PluginsManager.getExternalAuthHref(auth)
108 const { username, password } = this.form.value
110 this.authService.login(username, password)
112 next: () => this.redirectService.redirectToPreviousRoute(),
114 error: err => this.handleError(err)
118 askResetPassword () {
119 this.userService.askResetPassword(this.forgotPasswordEmail)
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.`
125 this.notifier.success(message)
126 this.hideForgotPasswordModal()
129 error: err => this.notifier.error(err.message)
133 openForgotPasswordModal () {
134 this.openedForgotPasswordModal = this.modalService.open(this.forgotPasswordModal)
137 hideForgotPasswordModal () {
138 this.openedForgotPasswordModal.close()
141 onInstanceAboutAccordionInit (instanceAboutAccordion: InstanceAboutAccordionComponent) {
142 this.accordion = instanceAboutAccordion.accordion
145 hasUsernameUppercase () {
146 return this.form.value['username'].match(/[A-Z]/)
149 private loadExternalAuthToken (username: string, token: string) {
150 this.isAuthenticatedWithExternalAuth = true
152 this.authService.login(username, null, token)
154 next: () => this.redirectService.redirectToPreviousRoute(),
157 this.handleError(err)
158 this.isAuthenticatedWithExternalAuth = false
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