2 import { AfterViewInit, Component, ElementRef, OnInit, ViewChild } from '@angular/core'
3 import { ActivatedRoute, Router } from '@angular/router'
4 import { AuthService, Notifier, RedirectService, SessionStorageService, 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 private static SESSION_STORAGE_REDIRECT_URL_KEY = 'login-previous-url'
22 @ViewChild('forgotPasswordModal', { static: true }) forgotPasswordModal: ElementRef
24 accordion: NgbAccordion
26 forgotPasswordEmail = ''
28 isAuthenticatedWithExternalAuth = false
29 externalAuthError = false
30 externalLogins: string[] = []
32 instanceInformationPanels = {
34 administrators: false,
40 private openedForgotPasswordModal: NgbModalRef
41 private serverConfig: ServerConfig
44 protected formValidatorService: FormValidatorService,
45 private route: ActivatedRoute,
46 private modalService: NgbModal,
47 private authService: AuthService,
48 private userService: UserService,
49 private redirectService: RedirectService,
50 private notifier: Notifier,
51 private hooks: HooksService,
52 private storage: SessionStorageService,
53 private router: Router
58 get signupAllowed () {
59 return this.serverConfig.signup.allowed === true
62 onTermsClick (event: Event, instanceInformation: HTMLElement) {
63 event.preventDefault()
66 this.accordion.expand('terms')
67 instanceInformation.scrollIntoView({ behavior: 'smooth' })
72 return this.serverConfig.email.enabled === false
76 const snapshot = this.route.snapshot
78 // Avoid undefined errors when accessing form error properties
80 username: LOGIN_USERNAME_VALIDATOR,
81 password: LOGIN_PASSWORD_VALIDATOR
84 this.serverConfig = snapshot.data.serverConfig
86 if (snapshot.queryParams.externalAuthToken) {
87 this.loadExternalAuthToken(snapshot.queryParams.username, snapshot.queryParams.externalAuthToken)
91 if (snapshot.queryParams.externalAuthError) {
92 this.externalAuthError = true
96 const previousUrl = this.redirectService.getPreviousUrl()
97 if (previousUrl && previousUrl !== '/') {
98 this.storage.setItem(LoginComponent.SESSION_STORAGE_REDIRECT_URL_KEY, previousUrl)
103 this.hooks.runAction('action:login.init', 'login')
106 getExternalLogins () {
107 return this.serverConfig.plugin.registeredExternalAuths
110 getAuthHref (auth: RegisteredExternalAuthConfig) {
111 return PluginsManager.getExternalAuthHref(auth)
117 const { username, password } = this.form.value
119 this.authService.login(username, password)
121 next: () => this.redirectService.redirectToPreviousRoute(),
123 error: err => this.handleError(err)
127 askResetPassword () {
128 this.userService.askResetPassword(this.forgotPasswordEmail)
131 const message = $localize`An email with the reset password instructions will be sent to ${this.forgotPasswordEmail}.
132 The link will expire within 1 hour.`
134 this.notifier.success(message)
135 this.hideForgotPasswordModal()
138 error: err => this.notifier.error(err.message)
142 openForgotPasswordModal () {
143 this.openedForgotPasswordModal = this.modalService.open(this.forgotPasswordModal)
146 hideForgotPasswordModal () {
147 this.openedForgotPasswordModal.close()
150 onInstanceAboutAccordionInit (instanceAboutAccordion: InstanceAboutAccordionComponent) {
151 this.accordion = instanceAboutAccordion.accordion
154 hasUsernameUppercase () {
155 return this.form.value['username'].match(/[A-Z]/)
158 private loadExternalAuthToken (username: string, token: string) {
159 this.isAuthenticatedWithExternalAuth = true
161 this.authService.login(username, null, token)
164 const redirectUrl = this.storage.getItem(LoginComponent.SESSION_STORAGE_REDIRECT_URL_KEY)
166 this.storage.removeItem(LoginComponent.SESSION_STORAGE_REDIRECT_URL_KEY)
167 return this.router.navigateByUrl(redirectUrl)
170 this.redirectService.redirectToLatestSessionRoute()
174 this.handleError(err)
175 this.isAuthenticatedWithExternalAuth = false
180 private handleError (err: any) {
181 if (err.message.indexOf('credentials are invalid') !== -1) this.error = $localize`Incorrect username or password.`
182 else if (err.message.indexOf('blocked') !== -1) this.error = $localize`Your account is blocked.`
183 else this.error = err.message