]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/+login/login.component.ts
Merge branch 'next' into develop
[github/Chocobozzz/PeerTube.git] / client / src / app / +login / login.component.ts
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'
11
12 @Component({
13 selector: 'my-login',
14 templateUrl: './login.component.html',
15 styleUrls: [ './login.component.scss' ]
16 })
17
18 export class LoginComponent extends FormReactive implements OnInit, AfterViewInit {
19 @ViewChild('forgotPasswordModal', { static: true }) forgotPasswordModal: ElementRef
20
21 accordion: NgbAccordion
22 error: string = null
23 forgotPasswordEmail = ''
24
25 isAuthenticatedWithExternalAuth = false
26 externalAuthError = false
27 externalLogins: string[] = []
28
29 instanceInformationPanels = {
30 terms: true,
31 administrators: false,
32 features: false,
33 moderation: false,
34 codeOfConduct: false
35 }
36
37 private openedForgotPasswordModal: NgbModalRef
38 private serverConfig: ServerConfig
39
40 constructor (
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
49 ) {
50 super()
51 }
52
53 get signupAllowed () {
54 return this.serverConfig.signup.allowed === true
55 }
56
57 onTermsClick (event: Event, instanceInformation: HTMLElement) {
58 event.preventDefault()
59
60 if (this.accordion) {
61 this.accordion.expand('terms')
62 instanceInformation.scrollIntoView({ behavior: 'smooth' })
63 }
64 }
65
66 isEmailDisabled () {
67 return this.serverConfig.email.enabled === false
68 }
69
70 ngOnInit () {
71 const snapshot = this.route.snapshot
72
73 // Avoid undefined errors when accessing form error properties
74 this.buildForm({
75 username: LOGIN_USERNAME_VALIDATOR,
76 password: LOGIN_PASSWORD_VALIDATOR
77 })
78
79 this.serverConfig = snapshot.data.serverConfig
80
81 if (snapshot.queryParams.externalAuthToken) {
82 this.loadExternalAuthToken(snapshot.queryParams.username, snapshot.queryParams.externalAuthToken)
83 return
84 }
85
86 if (snapshot.queryParams.externalAuthError) {
87 this.externalAuthError = true
88 return
89 }
90 }
91
92 ngAfterViewInit () {
93 this.hooks.runAction('action:login.init', 'login')
94 }
95
96 getExternalLogins () {
97 return this.serverConfig.plugin.registeredExternalAuths
98 }
99
100 getAuthHref (auth: RegisteredExternalAuthConfig) {
101 return environment.apiUrl + `/plugins/${auth.name}/${auth.version}/auth/${auth.authName}`
102 }
103
104 login () {
105 this.error = null
106
107 const { username, password } = this.form.value
108
109 this.authService.login(username, password)
110 .subscribe(
111 () => this.redirectService.redirectToPreviousRoute(),
112
113 err => this.handleError(err)
114 )
115 }
116
117 askResetPassword () {
118 this.userService.askResetPassword(this.forgotPasswordEmail)
119 .subscribe(
120 () => {
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.`
123
124 this.notifier.success(message)
125 this.hideForgotPasswordModal()
126 },
127
128 err => this.notifier.error(err.message)
129 )
130 }
131
132 openForgotPasswordModal () {
133 this.openedForgotPasswordModal = this.modalService.open(this.forgotPasswordModal)
134 }
135
136 hideForgotPasswordModal () {
137 this.openedForgotPasswordModal.close()
138 }
139
140 onInstanceAboutAccordionInit (instanceAboutAccordion: InstanceAboutAccordionComponent) {
141 this.accordion = instanceAboutAccordion.accordion
142 }
143
144 hasUsernameUppercase () {
145 return this.form.value['username'].match(/[A-Z]/)
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 }