]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame_incremental - client/src/app/+login/login.component.ts
Fix build
[github/Chocobozzz/PeerTube.git] / client / src / app / +login / login.component.ts
... / ...
CommitLineData
1import { environment } from 'src/environments/environment'
2import { AfterViewInit, Component, ElementRef, OnInit, ViewChild } from '@angular/core'
3import { ActivatedRoute } from '@angular/router'
4import { AuthService, Notifier, RedirectService, UserService } from '@app/core'
5import { HooksService } from '@app/core/plugins/hooks.service'
6import { InstanceAboutAccordionComponent } from '@app/shared/shared-instance'
7import { LOGIN_PASSWORD_VALIDATOR, LOGIN_USERNAME_VALIDATOR } from '@app/shared/form-validators/login-validators'
8import { FormReactive, FormValidatorService } from '@app/shared/shared-forms'
9import { NgbAccordion, NgbModal, NgbModalRef } from '@ng-bootstrap/ng-bootstrap'
10import { RegisteredExternalAuthConfig, ServerConfig } from '@shared/models'
11
12@Component({
13 selector: 'my-login',
14 templateUrl: './login.component.html',
15 styleUrls: [ './login.component.scss' ]
16})
17
18export class LoginComponent extends FormReactive implements OnInit, AfterViewInit {
19 @ViewChild('usernameInput', { static: false }) usernameInput: ElementRef
20 @ViewChild('forgotPasswordModal', { static: true }) forgotPasswordModal: ElementRef
21
22 accordion: NgbAccordion
23 error: string = null
24 forgotPasswordEmail = ''
25
26 isAuthenticatedWithExternalAuth = false
27 externalAuthError = false
28 externalLogins: string[] = []
29
30 instanceInformationPanels = {
31 terms: true,
32 administrators: false,
33 features: false,
34 moderation: false,
35 codeOfConduct: false
36 }
37
38 private openedForgotPasswordModal: NgbModalRef
39 private serverConfig: ServerConfig
40
41 constructor (
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
50 ) {
51 super()
52 }
53
54 get signupAllowed () {
55 return this.serverConfig.signup.allowed === true
56 }
57
58 onTermsClick (event: Event, instanceInformation: HTMLElement) {
59 event.preventDefault()
60
61 if (this.accordion) {
62 this.accordion.expand('terms')
63 instanceInformation.scrollIntoView({ behavior: 'smooth' })
64 }
65 }
66
67 isEmailDisabled () {
68 return this.serverConfig.email.enabled === false
69 }
70
71 ngOnInit () {
72 const snapshot = this.route.snapshot
73
74 this.serverConfig = snapshot.data.serverConfig
75
76 if (snapshot.queryParams.externalAuthToken) {
77 this.loadExternalAuthToken(snapshot.queryParams.username, snapshot.queryParams.externalAuthToken)
78 return
79 }
80
81 if (snapshot.queryParams.externalAuthError) {
82 this.externalAuthError = true
83 return
84 }
85
86 this.buildForm({
87 username: LOGIN_USERNAME_VALIDATOR,
88 password: LOGIN_PASSWORD_VALIDATOR
89 })
90 }
91
92 ngAfterViewInit () {
93 if (this.usernameInput) {
94 this.usernameInput.nativeElement.focus()
95 }
96
97 this.hooks.runAction('action:login.init', 'login')
98 }
99
100 getExternalLogins () {
101 return this.serverConfig.plugin.registeredExternalAuths
102 }
103
104 getAuthHref (auth: RegisteredExternalAuthConfig) {
105 return environment.apiUrl + `/plugins/${auth.name}/${auth.version}/auth/${auth.authName}`
106 }
107
108 login () {
109 this.error = null
110
111 const { username, password } = this.form.value
112
113 this.authService.login(username, password)
114 .subscribe(
115 () => this.redirectService.redirectToPreviousRoute(),
116
117 err => this.handleError(err)
118 )
119 }
120
121 askResetPassword () {
122 this.userService.askResetPassword(this.forgotPasswordEmail)
123 .subscribe(
124 () => {
125 const message = $localize`An email with the reset password instructions will be sent to ${this.forgotPasswordEmail}.
126The link will expire within 1 hour.`
127
128 this.notifier.success(message)
129 this.hideForgotPasswordModal()
130 },
131
132 err => this.notifier.error(err.message)
133 )
134 }
135
136 openForgotPasswordModal () {
137 this.openedForgotPasswordModal = this.modalService.open(this.forgotPasswordModal)
138 }
139
140 hideForgotPasswordModal () {
141 this.openedForgotPasswordModal.close()
142 }
143
144 onInstanceAboutAccordionInit (instanceAboutAccordion: InstanceAboutAccordionComponent) {
145 this.accordion = instanceAboutAccordion.accordion
146 }
147
148 private loadExternalAuthToken (username: string, token: string) {
149 this.isAuthenticatedWithExternalAuth = true
150
151 this.authService.login(username, null, token)
152 .subscribe(
153 () => this.redirectService.redirectToPreviousRoute(),
154
155 err => {
156 this.handleError(err)
157 this.isAuthenticatedWithExternalAuth = false
158 }
159 )
160 }
161
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
166 }
167}