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 { 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 { 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('forgotPasswordModal', { static: true }) forgotPasswordModal: ElementRef
21 accordion: NgbAccordion
23 forgotPasswordEmail = ''
25 isAuthenticatedWithExternalAuth = false
26 externalAuthError = false
27 externalLogins: string[] = []
29 instanceInformationPanels = {
31 administrators: false,
37 private openedForgotPasswordModal: NgbModalRef
38 private serverConfig: ServerConfig
41 protected formValidatorService: FormValidatorService,
42 private route: ActivatedRoute,
43 private modalService: NgbModal,
44 private authService: AuthService,
45 private userService: UserService,
46 private redirectService: RedirectService,
47 private notifier: Notifier,
48 private hooks: HooksService
53 get signupAllowed () {
54 return this.serverConfig.signup.allowed === true
57 onTermsClick (event: Event, instanceInformation: HTMLElement) {
58 event.preventDefault()
61 this.accordion.expand('terms')
62 instanceInformation.scrollIntoView({ behavior: 'smooth' })
67 return this.serverConfig.email.enabled === false
71 const snapshot = this.route.snapshot
73 // Avoid undefined errors when accessing form error properties
75 username: LOGIN_USERNAME_VALIDATOR,
76 password: LOGIN_PASSWORD_VALIDATOR
79 this.serverConfig = snapshot.data.serverConfig
81 if (snapshot.queryParams.externalAuthToken) {
82 this.loadExternalAuthToken(snapshot.queryParams.username, snapshot.queryParams.externalAuthToken)
86 if (snapshot.queryParams.externalAuthError) {
87 this.externalAuthError = true
93 this.hooks.runAction('action:login.init', 'login')
96 getExternalLogins () {
97 return this.serverConfig.plugin.registeredExternalAuths
100 getAuthHref (auth: RegisteredExternalAuthConfig) {
101 return environment.apiUrl + `/plugins/${auth.name}/${auth.version}/auth/${auth.authName}`
107 const { username, password } = this.form.value
109 this.authService.login(username, password)
111 next: () => this.redirectService.redirectToPreviousRoute(),
113 error: err => this.handleError(err)
117 askResetPassword () {
118 this.userService.askResetPassword(this.forgotPasswordEmail)
121 const message = $localize`An email with the reset password instructions will be sent to ${this.forgotPasswordEmail}.
122 The link will expire within 1 hour.`
124 this.notifier.success(message)
125 this.hideForgotPasswordModal()
128 error: err => this.notifier.error(err.message)
132 openForgotPasswordModal () {
133 this.openedForgotPasswordModal = this.modalService.open(this.forgotPasswordModal)
136 hideForgotPasswordModal () {
137 this.openedForgotPasswordModal.close()
140 onInstanceAboutAccordionInit (instanceAboutAccordion: InstanceAboutAccordionComponent) {
141 this.accordion = instanceAboutAccordion.accordion
144 hasUsernameUppercase () {
145 return this.form.value['username'].match(/[A-Z]/)
148 private loadExternalAuthToken (username: string, token: string) {
149 this.isAuthenticatedWithExternalAuth = true
151 this.authService.login(username, null, token)
153 next: () => this.redirectService.redirectToPreviousRoute(),
156 this.handleError(err)
157 this.isAuthenticatedWithExternalAuth = false
162 private handleError (err: any) {
163 if (err.message.indexOf('credentials are invalid') !== -1) this.error = $localize`Incorrect username or password.`
164 else if (err.message.indexOf('blocked') !== -1) this.error = $localize`Your account is blocked.`
165 else this.error = err.message