]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/+login/login.component.ts
Add server config helper in plugin guide
[github/Chocobozzz/PeerTube.git] / client / src / app / +login / login.component.ts
CommitLineData
ebefc902 1import { environment } from 'src/environments/environment'
67ed6552
C
2import { AfterViewInit, Component, ElementRef, OnInit, ViewChild } from '@angular/core'
3import { ActivatedRoute } from '@angular/router'
4import { AuthService, Notifier, RedirectService, UserService } from '@app/core'
f375bb3d 5import { HooksService } from '@app/core/plugins/hooks.service'
40360c17 6import { InstanceAboutAccordionComponent } from '@app/shared/shared-instance'
7ed1edbb
C
7import { LOGIN_PASSWORD_VALIDATOR, LOGIN_USERNAME_VALIDATOR } from '@app/shared/form-validators/login-validators'
8import { FormReactive, FormValidatorService } from '@app/shared/shared-forms'
40360c17 9import { NgbAccordion, NgbModal, NgbModalRef } from '@ng-bootstrap/ng-bootstrap'
67ed6552 10import { RegisteredExternalAuthConfig, ServerConfig } from '@shared/models'
b1794c53
C
11
12@Component({
a840d396 13 selector: 'my-login',
d235f6b0
C
14 templateUrl: './login.component.html',
15 styleUrls: [ './login.component.scss' ]
b1794c53
C
16})
17
ebefc902
C
18export class LoginComponent extends FormReactive implements OnInit, AfterViewInit {
19 @ViewChild('usernameInput', { static: false }) usernameInput: ElementRef
f36da21e 20 @ViewChild('forgotPasswordModal', { static: true }) forgotPasswordModal: ElementRef
ecb4e35f 21
40360c17 22 accordion: NgbAccordion
df98563e 23 error: string = null
ecb4e35f 24 forgotPasswordEmail = ''
bc90883f 25
4a8d113b 26 isAuthenticatedWithExternalAuth = false
bc90883f 27 externalAuthError = false
ebefc902 28 externalLogins: string[] = []
192ea60b 29
40360c17
K
30 instanceInformationPanels = {
31 terms: true,
32 administrators: false,
33 features: false,
34 moderation: false,
35 codeOfConduct: false
36 }
37
63347a0f 38 private openedForgotPasswordModal: NgbModalRef
ba430d75 39 private serverConfig: ServerConfig
63347a0f 40
b1d40cff 41 constructor (
d18d6478 42 protected formValidatorService: FormValidatorService,
ba430d75 43 private route: ActivatedRoute,
63347a0f 44 private modalService: NgbModal,
b1d40cff
C
45 private authService: AuthService,
46 private userService: UserService,
b1d40cff 47 private redirectService: RedirectService,
f8b2c1b4 48 private notifier: Notifier,
66357162
C
49 private hooks: HooksService
50 ) {
df98563e 51 super()
4b2f33f3 52 }
b1794c53 53
2b084d70 54 get signupAllowed () {
ba430d75 55 return this.serverConfig.signup.allowed === true
2b084d70
C
56 }
57
40360c17
K
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
3b3b1820 67 isEmailDisabled () {
ba430d75 68 return this.serverConfig.email.enabled === false
3b3b1820
C
69 }
70
df98563e 71 ngOnInit () {
4a8d113b
C
72 const snapshot = this.route.snapshot
73
2345e138
C
74 // Avoid undefined errors when accessing form error properties
75 this.buildForm({
76 username: LOGIN_USERNAME_VALIDATOR,
77 password: LOGIN_PASSWORD_VALIDATOR
78 })
79
4a8d113b
C
80 this.serverConfig = snapshot.data.serverConfig
81
82 if (snapshot.queryParams.externalAuthToken) {
83 this.loadExternalAuthToken(snapshot.queryParams.username, snapshot.queryParams.externalAuthToken)
84 return
85 }
ba430d75 86
bc90883f
C
87 if (snapshot.queryParams.externalAuthError) {
88 this.externalAuthError = true
89 return
90 }
ebefc902
C
91 }
92
93 ngAfterViewInit () {
94 if (this.usernameInput) {
95 this.usernameInput.nativeElement.focus()
96 }
f375bb3d
C
97
98 this.hooks.runAction('action:login.init', 'login')
ebefc902
C
99 }
100
101 getExternalLogins () {
102 return this.serverConfig.plugin.registeredExternalAuths
103 }
9fe44067 104
ebefc902
C
105 getAuthHref (auth: RegisteredExternalAuthConfig) {
106 return environment.apiUrl + `/plugins/${auth.name}/${auth.version}/auth/${auth.authName}`
0f6da32b
C
107 }
108
df98563e
C
109 login () {
110 this.error = null
4b2f33f3 111
df98563e 112 const { username, password } = this.form.value
4b2f33f3 113
2b084d70
C
114 this.authService.login(username, password)
115 .subscribe(
dae5ca24 116 () => this.redirectService.redirectToPreviousRoute(),
192ea60b 117
4a8d113b 118 err => this.handleError(err)
2b084d70 119 )
b1794c53 120 }
ecb4e35f
C
121
122 askResetPassword () {
123 this.userService.askResetPassword(this.forgotPasswordEmail)
124 .subscribe(
141b177d 125 () => {
66357162
C
126 const message = $localize`An email with the reset password instructions will be sent to ${this.forgotPasswordEmail}.
127The link will expire within 1 hour.`
128
f8b2c1b4 129 this.notifier.success(message)
ecb4e35f
C
130 this.hideForgotPasswordModal()
131 },
132
f8b2c1b4 133 err => this.notifier.error(err.message)
ecb4e35f
C
134 )
135 }
136
ecb4e35f 137 openForgotPasswordModal () {
63347a0f 138 this.openedForgotPasswordModal = this.modalService.open(this.forgotPasswordModal)
ecb4e35f
C
139 }
140
141 hideForgotPasswordModal () {
63347a0f 142 this.openedForgotPasswordModal.close()
ecb4e35f 143 }
4a8d113b 144
40360c17
K
145 onInstanceAboutAccordionInit (instanceAboutAccordion: InstanceAboutAccordionComponent) {
146 this.accordion = instanceAboutAccordion.accordion
147 }
148
4a8d113b
C
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) {
66357162
C
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.`
4a8d113b
C
166 else this.error = err.message
167 }
b1794c53 168}