1 import { environment } from 'src/environments/environment'
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 { USER_OTP_TOKEN_VALIDATOR } from '@app/shared/form-validators/user-validators'
8 import { FormReactive, FormReactiveService, InputTextComponent } from '@app/shared/shared-forms'
9 import { InstanceAboutAccordionComponent } from '@app/shared/shared-instance'
10 import { NgbAccordion, NgbModal, NgbModalRef } from '@ng-bootstrap/ng-bootstrap'
11 import { getExternalAuthHref } from '@shared/core-utils'
12 import { RegisteredExternalAuthConfig, ServerConfig, ServerErrorCode } from '@shared/models'
16 templateUrl: './login.component.html',
17 styleUrls: [ './login.component.scss' ]
20 export class LoginComponent extends FormReactive implements OnInit, AfterViewInit {
21 private static SESSION_STORAGE_REDIRECT_URL_KEY = 'login-previous-url'
23 @ViewChild('forgotPasswordModal', { static: true }) forgotPasswordModal: ElementRef
24 @ViewChild('otpTokenInput') otpTokenInput: InputTextComponent
26 accordion: NgbAccordion
28 forgotPasswordEmail = ''
30 isAuthenticatedWithExternalAuth = false
31 externalAuthError = false
32 externalLogins: string[] = []
34 instanceInformationPanels = {
36 administrators: false,
44 private openedForgotPasswordModal: NgbModalRef
45 private serverConfig: ServerConfig
48 protected formReactiveService: FormReactiveService,
49 private route: ActivatedRoute,
50 private modalService: NgbModal,
51 private authService: AuthService,
52 private userService: UserService,
53 private redirectService: RedirectService,
54 private notifier: Notifier,
55 private hooks: HooksService,
56 private storage: SessionStorageService,
57 private router: Router
62 get signupAllowed () {
63 return this.serverConfig.signup.allowed === true
67 return this.serverConfig.instance.name
70 onTermsClick (event: Event, instanceInformation: HTMLElement) {
71 event.preventDefault()
74 this.accordion.expand('terms')
75 instanceInformation.scrollIntoView({ behavior: 'smooth' })
80 return this.serverConfig.email.enabled === false
84 const snapshot = this.route.snapshot
86 // Avoid undefined errors when accessing form error properties
88 username: LOGIN_USERNAME_VALIDATOR,
89 password: LOGIN_PASSWORD_VALIDATOR,
91 VALIDATORS: [], // Will be set dynamically
92 MESSAGES: USER_OTP_TOKEN_VALIDATOR.MESSAGES
96 this.serverConfig = snapshot.data.serverConfig
98 if (snapshot.queryParams.externalAuthToken) {
99 this.loadExternalAuthToken(snapshot.queryParams.username, snapshot.queryParams.externalAuthToken)
103 if (snapshot.queryParams.externalAuthError) {
104 this.externalAuthError = true
108 const previousUrl = this.redirectService.getPreviousUrl()
109 if (previousUrl && previousUrl !== '/') {
110 this.storage.setItem(LoginComponent.SESSION_STORAGE_REDIRECT_URL_KEY, previousUrl)
115 this.hooks.runAction('action:login.init', 'login')
118 getExternalLogins () {
119 return this.serverConfig.plugin.registeredExternalAuths
122 getAuthHref (auth: RegisteredExternalAuthConfig) {
123 return getExternalAuthHref(environment.apiUrl, auth)
130 username: this.form.value['username'],
131 password: this.form.value['password'],
132 otpToken: this.form.value['otp-token']
135 this.authService.login(options)
138 next: () => this.redirectService.redirectToPreviousRoute(),
141 this.handleError(err)
146 askResetPassword () {
147 this.userService.askResetPassword(this.forgotPasswordEmail)
150 const message = $localize`An email with the reset password instructions will be sent to ${this.forgotPasswordEmail}.
151 The link will expire within 1 hour.`
153 this.notifier.success(message)
154 this.hideForgotPasswordModal()
157 error: err => this.notifier.error(err.message)
161 openForgotPasswordModal () {
162 this.openedForgotPasswordModal = this.modalService.open(this.forgotPasswordModal)
165 hideForgotPasswordModal () {
166 this.openedForgotPasswordModal.close()
169 onInstanceAboutAccordionInit (instanceAboutAccordion: InstanceAboutAccordionComponent) {
170 this.accordion = instanceAboutAccordion.accordion
173 hasUsernameUppercase () {
174 return this.form.value['username'].match(/[A-Z]/)
177 private loadExternalAuthToken (username: string, token: string) {
178 this.isAuthenticatedWithExternalAuth = true
180 this.authService.login({ username, password: null, token })
183 const redirectUrl = this.storage.getItem(LoginComponent.SESSION_STORAGE_REDIRECT_URL_KEY)
185 this.storage.removeItem(LoginComponent.SESSION_STORAGE_REDIRECT_URL_KEY)
186 return this.router.navigateByUrl(redirectUrl)
189 this.redirectService.redirectToLatestSessionRoute()
193 this.handleError(err)
194 this.isAuthenticatedWithExternalAuth = false
199 private handleError (err: any) {
202 if (this.authService.isOTPMissingError(err)) {
206 this.form.get('otp-token').setValidators(USER_OTP_TOKEN_VALIDATOR.VALIDATORS)
207 this.otpTokenInput.focus()
213 if (err.message.includes('credentials are invalid')) {
214 this.error = $localize`Incorrect username or password.`
218 if (err.message.includes('blocked')) {
219 this.error = $localize`Your account is blocked.`
223 if (err.body?.code === ServerErrorCode.ACCOUNT_WAITING_FOR_APPROVAL) {
224 this.error = $localize`This account is awaiting approval by moderators.`
228 if (err.body?.code === ServerErrorCode.ACCOUNT_APPROVAL_REJECTED) {
229 this.error = $localize`Registration approval has been rejected for this account.`
233 this.error = err.message