1 import { Component, ElementRef, OnInit, ViewChild, AfterViewInit } from '@angular/core'
2 import { Notifier, RedirectService } from '@app/core'
3 import { UserService } from '@app/shared'
4 import { AuthService } from '../core'
5 import { FormReactive } from '../shared'
6 import { I18n } from '@ngx-translate/i18n-polyfill'
7 import { FormValidatorService } from '@app/shared/forms/form-validators/form-validator.service'
8 import { LoginValidatorsService } from '@app/shared/forms/form-validators/login-validators.service'
9 import { NgbModal, NgbModalRef } from '@ng-bootstrap/ng-bootstrap'
10 import { ActivatedRoute } from '@angular/router'
11 import { ServerConfig, RegisteredExternalAuthConfig } from '@shared/models/server/server-config.model'
12 import { environment } from 'src/environments/environment'
16 templateUrl: './login.component.html',
17 styleUrls: [ './login.component.scss' ]
20 export class LoginComponent extends FormReactive implements OnInit, AfterViewInit {
21 @ViewChild('usernameInput', { static: false }) usernameInput: ElementRef
22 @ViewChild('forgotPasswordModal', { static: true }) forgotPasswordModal: ElementRef
25 forgotPasswordEmail = ''
27 isAuthenticatedWithExternalAuth = false
28 externalAuthError = false
29 externalLogins: string[] = []
31 private openedForgotPasswordModal: NgbModalRef
32 private serverConfig: ServerConfig
35 protected formValidatorService: FormValidatorService,
36 private route: ActivatedRoute,
37 private modalService: NgbModal,
38 private loginValidatorsService: LoginValidatorsService,
39 private authService: AuthService,
40 private userService: UserService,
41 private redirectService: RedirectService,
42 private notifier: Notifier,
48 get signupAllowed () {
49 return this.serverConfig.signup.allowed === true
53 return this.serverConfig.email.enabled === false
57 const snapshot = this.route.snapshot
59 this.serverConfig = snapshot.data.serverConfig
61 if (snapshot.queryParams.externalAuthToken) {
62 this.loadExternalAuthToken(snapshot.queryParams.username, snapshot.queryParams.externalAuthToken)
66 if (snapshot.queryParams.externalAuthError) {
67 this.externalAuthError = true
72 username: this.loginValidatorsService.LOGIN_USERNAME,
73 password: this.loginValidatorsService.LOGIN_PASSWORD
78 if (this.usernameInput) {
79 this.usernameInput.nativeElement.focus()
83 getExternalLogins () {
84 return this.serverConfig.plugin.registeredExternalAuths
87 getAuthHref (auth: RegisteredExternalAuthConfig) {
88 return environment.apiUrl + `/plugins/${auth.name}/${auth.version}/auth/${auth.authName}`
94 const { username, password } = this.form.value
96 this.authService.login(username, password)
98 () => this.redirectService.redirectToPreviousRoute(),
100 err => this.handleError(err)
104 askResetPassword () {
105 this.userService.askResetPassword(this.forgotPasswordEmail)
108 const message = this.i18n(
109 'An email with the reset password instructions will be sent to {{email}}. The link will expire within 1 hour.',
110 { email: this.forgotPasswordEmail }
112 this.notifier.success(message)
113 this.hideForgotPasswordModal()
116 err => this.notifier.error(err.message)
120 openForgotPasswordModal () {
121 this.openedForgotPasswordModal = this.modalService.open(this.forgotPasswordModal)
124 hideForgotPasswordModal () {
125 this.openedForgotPasswordModal.close()
128 private loadExternalAuthToken (username: string, token: string) {
129 this.isAuthenticatedWithExternalAuth = true
131 this.authService.login(username, null, token)
133 () => this.redirectService.redirectToPreviousRoute(),
136 this.handleError(err)
137 this.isAuthenticatedWithExternalAuth = false
142 private handleError (err: any) {
143 if (err.message.indexOf('credentials are invalid') !== -1) this.error = this.i18n('Incorrect username or password.')
144 else if (err.message.indexOf('blocked') !== -1) this.error = this.i18n('You account is blocked.')
145 else this.error = err.message