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 { InstanceAboutAccordionComponent } from '@app/shared/shared-instance'
7 import { LOGIN_PASSWORD_VALIDATOR, LOGIN_USERNAME_VALIDATOR } from '@app/shared/form-validators/login-validators'
8 import { FormReactive, FormValidatorService } from '@app/shared/shared-forms'
9 import { NgbAccordion, NgbModal, NgbModalRef } from '@ng-bootstrap/ng-bootstrap'
10 import { RegisteredExternalAuthConfig, ServerConfig } from '@shared/models'
14 templateUrl: './login.component.html',
15 styleUrls: [ './login.component.scss' ]
18 export class LoginComponent extends FormReactive implements OnInit, AfterViewInit {
19 @ViewChild('usernameInput', { static: false }) usernameInput: ElementRef
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 if (this.usernameInput) {
95 this.usernameInput.nativeElement.focus()
98 this.hooks.runAction('action:login.init', 'login')
101 getExternalLogins () {
102 return this.serverConfig.plugin.registeredExternalAuths
105 getAuthHref (auth: RegisteredExternalAuthConfig) {
106 return environment.apiUrl + `/plugins/${auth.name}/${auth.version}/auth/${auth.authName}`
112 const { username, password } = this.form.value
114 this.authService.login(username, password)
116 () => this.redirectService.redirectToPreviousRoute(),
118 err => this.handleError(err)
122 askResetPassword () {
123 this.userService.askResetPassword(this.forgotPasswordEmail)
126 const message = $localize`An email with the reset password instructions will be sent to ${this.forgotPasswordEmail}.
127 The link will expire within 1 hour.`
129 this.notifier.success(message)
130 this.hideForgotPasswordModal()
133 err => this.notifier.error(err.message)
137 openForgotPasswordModal () {
138 this.openedForgotPasswordModal = this.modalService.open(this.forgotPasswordModal)
141 hideForgotPasswordModal () {
142 this.openedForgotPasswordModal.close()
145 onInstanceAboutAccordionInit (instanceAboutAccordion: InstanceAboutAccordionComponent) {
146 this.accordion = instanceAboutAccordion.accordion
149 private loadExternalAuthToken (username: string, token: string) {
150 this.isAuthenticatedWithExternalAuth = true
152 this.authService.login(username, null, token)
154 () => 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