]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/+login/login.component.ts
Merge branch '3520-cli_auth_add_trailing_slash' into release/3.0.0
[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 { InstanceAboutAccordionComponent } from '@app/shared/shared-instance'
7 import { LOGIN_PASSWORD_VALIDATOR, LOGIN_USERNAME_VALIDATOR } from '@app/shared/form-validators/login-validators'
8 import { FormReactive, FormValidatorService } from '@app/shared/shared-forms'
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('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 // Avoid undefined errors when accessing form error properties
75 this.buildForm({
76 username: LOGIN_USERNAME_VALIDATOR,
77 password: LOGIN_PASSWORD_VALIDATOR
78 })
79
80 this.serverConfig = snapshot.data.serverConfig
81
82 if (snapshot.queryParams.externalAuthToken) {
83 this.loadExternalAuthToken(snapshot.queryParams.username, snapshot.queryParams.externalAuthToken)
84 return
85 }
86
87 if (snapshot.queryParams.externalAuthError) {
88 this.externalAuthError = true
89 return
90 }
91 }
92
93 ngAfterViewInit () {
94 if (this.usernameInput) {
95 this.usernameInput.nativeElement.focus()
96 }
97
98 this.hooks.runAction('action:login.init', 'login')
99 }
100
101 getExternalLogins () {
102 return this.serverConfig.plugin.registeredExternalAuths
103 }
104
105 getAuthHref (auth: RegisteredExternalAuthConfig) {
106 return environment.apiUrl + `/plugins/${auth.name}/${auth.version}/auth/${auth.authName}`
107 }
108
109 login () {
110 this.error = null
111
112 const { username, password } = this.form.value
113
114 this.authService.login(username, password)
115 .subscribe(
116 () => this.redirectService.redirectToPreviousRoute(),
117
118 err => this.handleError(err)
119 )
120 }
121
122 askResetPassword () {
123 this.userService.askResetPassword(this.forgotPasswordEmail)
124 .subscribe(
125 () => {
126 const message = $localize`An email with the reset password instructions will be sent to ${this.forgotPasswordEmail}.
127 The link will expire within 1 hour.`
128
129 this.notifier.success(message)
130 this.hideForgotPasswordModal()
131 },
132
133 err => this.notifier.error(err.message)
134 )
135 }
136
137 openForgotPasswordModal () {
138 this.openedForgotPasswordModal = this.modalService.open(this.forgotPasswordModal)
139 }
140
141 hideForgotPasswordModal () {
142 this.openedForgotPasswordModal.close()
143 }
144
145 onInstanceAboutAccordionInit (instanceAboutAccordion: InstanceAboutAccordionComponent) {
146 this.accordion = instanceAboutAccordion.accordion
147 }
148
149 private loadExternalAuthToken (username: string, token: string) {
150 this.isAuthenticatedWithExternalAuth = true
151
152 this.authService.login(username, null, token)
153 .subscribe(
154 () => this.redirectService.redirectToPreviousRoute(),
155
156 err => {
157 this.handleError(err)
158 this.isAuthenticatedWithExternalAuth = false
159 }
160 )
161 }
162
163 private handleError (err: any) {
164 if (err.message.indexOf('credentials are invalid') !== -1) this.error = $localize`Incorrect username or password.`
165 else if (err.message.indexOf('blocked') !== -1) this.error = $localize`Your account is blocked.`
166 else this.error = err.message
167 }
168 }