]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/+login/login.component.ts
Merge branch 'release/4.2.0' into develop
[github/Chocobozzz/PeerTube.git] / client / src / app / +login / login.component.ts
1
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'
12
13 @Component({
14 selector: 'my-login',
15 templateUrl: './login.component.html',
16 styleUrls: [ './login.component.scss' ]
17 })
18
19 export class LoginComponent extends FormReactive implements OnInit, AfterViewInit {
20 private static SESSION_STORAGE_REDIRECT_URL_KEY = 'login-previous-url'
21
22 @ViewChild('forgotPasswordModal', { static: true }) forgotPasswordModal: ElementRef
23
24 accordion: NgbAccordion
25 error: string = null
26 forgotPasswordEmail = ''
27
28 isAuthenticatedWithExternalAuth = false
29 externalAuthError = false
30 externalLogins: string[] = []
31
32 instanceInformationPanels = {
33 terms: true,
34 administrators: false,
35 features: false,
36 moderation: false,
37 codeOfConduct: false
38 }
39
40 private openedForgotPasswordModal: NgbModalRef
41 private serverConfig: ServerConfig
42
43 constructor (
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
54 ) {
55 super()
56 }
57
58 get signupAllowed () {
59 return this.serverConfig.signup.allowed === true
60 }
61
62 get instanceName () {
63 return this.serverConfig.instance.name
64 }
65
66 onTermsClick (event: Event, instanceInformation: HTMLElement) {
67 event.preventDefault()
68
69 if (this.accordion) {
70 this.accordion.expand('terms')
71 instanceInformation.scrollIntoView({ behavior: 'smooth' })
72 }
73 }
74
75 isEmailDisabled () {
76 return this.serverConfig.email.enabled === false
77 }
78
79 ngOnInit () {
80 const snapshot = this.route.snapshot
81
82 // Avoid undefined errors when accessing form error properties
83 this.buildForm({
84 username: LOGIN_USERNAME_VALIDATOR,
85 password: LOGIN_PASSWORD_VALIDATOR
86 })
87
88 this.serverConfig = snapshot.data.serverConfig
89
90 if (snapshot.queryParams.externalAuthToken) {
91 this.loadExternalAuthToken(snapshot.queryParams.username, snapshot.queryParams.externalAuthToken)
92 return
93 }
94
95 if (snapshot.queryParams.externalAuthError) {
96 this.externalAuthError = true
97 return
98 }
99
100 const previousUrl = this.redirectService.getPreviousUrl()
101 if (previousUrl && previousUrl !== '/') {
102 this.storage.setItem(LoginComponent.SESSION_STORAGE_REDIRECT_URL_KEY, previousUrl)
103 }
104 }
105
106 ngAfterViewInit () {
107 this.hooks.runAction('action:login.init', 'login')
108 }
109
110 getExternalLogins () {
111 return this.serverConfig.plugin.registeredExternalAuths
112 }
113
114 getAuthHref (auth: RegisteredExternalAuthConfig) {
115 return PluginsManager.getExternalAuthHref(auth)
116 }
117
118 login () {
119 this.error = null
120
121 const { username, password } = this.form.value
122
123 this.authService.login(username, password)
124 .subscribe({
125 next: () => this.redirectService.redirectToPreviousRoute(),
126
127 error: err => this.handleError(err)
128 })
129 }
130
131 askResetPassword () {
132 this.userService.askResetPassword(this.forgotPasswordEmail)
133 .subscribe({
134 next: () => {
135 const message = $localize`An email with the reset password instructions will be sent to ${this.forgotPasswordEmail}.
136 The link will expire within 1 hour.`
137
138 this.notifier.success(message)
139 this.hideForgotPasswordModal()
140 },
141
142 error: err => this.notifier.error(err.message)
143 })
144 }
145
146 openForgotPasswordModal () {
147 this.openedForgotPasswordModal = this.modalService.open(this.forgotPasswordModal)
148 }
149
150 hideForgotPasswordModal () {
151 this.openedForgotPasswordModal.close()
152 }
153
154 onInstanceAboutAccordionInit (instanceAboutAccordion: InstanceAboutAccordionComponent) {
155 this.accordion = instanceAboutAccordion.accordion
156 }
157
158 hasUsernameUppercase () {
159 return this.form.value['username'].match(/[A-Z]/)
160 }
161
162 private loadExternalAuthToken (username: string, token: string) {
163 this.isAuthenticatedWithExternalAuth = true
164
165 this.authService.login(username, null, token)
166 .subscribe({
167 next: () => {
168 const redirectUrl = this.storage.getItem(LoginComponent.SESSION_STORAGE_REDIRECT_URL_KEY)
169 if (redirectUrl) {
170 this.storage.removeItem(LoginComponent.SESSION_STORAGE_REDIRECT_URL_KEY)
171 return this.router.navigateByUrl(redirectUrl)
172 }
173
174 this.redirectService.redirectToLatestSessionRoute()
175 },
176
177 error: err => {
178 this.handleError(err)
179 this.isAuthenticatedWithExternalAuth = false
180 }
181 })
182 }
183
184 private handleError (err: any) {
185 if (err.message.indexOf('credentials are invalid') !== -1) this.error = $localize`Incorrect username or password.`
186 else if (err.message.indexOf('blocked') !== -1) this.error = $localize`Your account is blocked.`
187 else this.error = err.message
188 }
189 }