]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/login/login.component.ts
Translated using Weblate (Hungarian)
[github/Chocobozzz/PeerTube.git] / client / src / app / login / login.component.ts
CommitLineData
ebefc902 1import { Component, ElementRef, OnInit, ViewChild, AfterViewInit } from '@angular/core'
000eb0e4 2import { Notifier, RedirectService } from '@app/core'
ecb4e35f 3import { UserService } from '@app/shared'
df98563e
C
4import { AuthService } from '../core'
5import { FormReactive } from '../shared'
b1d40cff 6import { I18n } from '@ngx-translate/i18n-polyfill'
d18d6478 7import { FormValidatorService } from '@app/shared/forms/form-validators/form-validator.service'
e309822b 8import { LoginValidatorsService } from '@app/shared/forms/form-validators/login-validators.service'
63347a0f 9import { NgbModal, NgbModalRef } from '@ng-bootstrap/ng-bootstrap'
000eb0e4 10import { ActivatedRoute } from '@angular/router'
ebefc902
C
11import { ServerConfig, RegisteredExternalAuthConfig } from '@shared/models/server/server-config.model'
12import { environment } from 'src/environments/environment'
f375bb3d 13import { HooksService } from '@app/core/plugins/hooks.service'
b1794c53
C
14
15@Component({
a840d396 16 selector: 'my-login',
d235f6b0
C
17 templateUrl: './login.component.html',
18 styleUrls: [ './login.component.scss' ]
b1794c53
C
19})
20
ebefc902
C
21export class LoginComponent extends FormReactive implements OnInit, AfterViewInit {
22 @ViewChild('usernameInput', { static: false }) usernameInput: ElementRef
f36da21e 23 @ViewChild('forgotPasswordModal', { static: true }) forgotPasswordModal: ElementRef
ecb4e35f 24
df98563e 25 error: string = null
ecb4e35f 26 forgotPasswordEmail = ''
bc90883f 27
4a8d113b 28 isAuthenticatedWithExternalAuth = false
bc90883f 29 externalAuthError = false
ebefc902 30 externalLogins: string[] = []
192ea60b 31
63347a0f 32 private openedForgotPasswordModal: NgbModalRef
ba430d75 33 private serverConfig: ServerConfig
63347a0f 34
b1d40cff 35 constructor (
d18d6478 36 protected formValidatorService: FormValidatorService,
ba430d75 37 private route: ActivatedRoute,
63347a0f 38 private modalService: NgbModal,
e309822b 39 private loginValidatorsService: LoginValidatorsService,
b1d40cff
C
40 private authService: AuthService,
41 private userService: UserService,
b1d40cff 42 private redirectService: RedirectService,
f8b2c1b4 43 private notifier: Notifier,
f375bb3d 44 private hooks: HooksService,
b1d40cff
C
45 private i18n: I18n
46 ) {
df98563e 47 super()
4b2f33f3 48 }
b1794c53 49
2b084d70 50 get signupAllowed () {
ba430d75 51 return this.serverConfig.signup.allowed === true
2b084d70
C
52 }
53
3b3b1820 54 isEmailDisabled () {
ba430d75 55 return this.serverConfig.email.enabled === false
3b3b1820
C
56 }
57
df98563e 58 ngOnInit () {
4a8d113b
C
59 const snapshot = this.route.snapshot
60
61 this.serverConfig = snapshot.data.serverConfig
62
63 if (snapshot.queryParams.externalAuthToken) {
64 this.loadExternalAuthToken(snapshot.queryParams.username, snapshot.queryParams.externalAuthToken)
65 return
66 }
ba430d75 67
bc90883f
C
68 if (snapshot.queryParams.externalAuthError) {
69 this.externalAuthError = true
70 return
71 }
72
d18d6478 73 this.buildForm({
e309822b
C
74 username: this.loginValidatorsService.LOGIN_USERNAME,
75 password: this.loginValidatorsService.LOGIN_PASSWORD
d18d6478 76 })
ebefc902
C
77 }
78
79 ngAfterViewInit () {
80 if (this.usernameInput) {
81 this.usernameInput.nativeElement.focus()
82 }
f375bb3d
C
83
84 this.hooks.runAction('action:login.init', 'login')
ebefc902
C
85 }
86
87 getExternalLogins () {
88 return this.serverConfig.plugin.registeredExternalAuths
89 }
9fe44067 90
ebefc902
C
91 getAuthHref (auth: RegisteredExternalAuthConfig) {
92 return environment.apiUrl + `/plugins/${auth.name}/${auth.version}/auth/${auth.authName}`
0f6da32b
C
93 }
94
df98563e
C
95 login () {
96 this.error = null
4b2f33f3 97
df98563e 98 const { username, password } = this.form.value
4b2f33f3 99
2b084d70
C
100 this.authService.login(username, password)
101 .subscribe(
dae5ca24 102 () => this.redirectService.redirectToPreviousRoute(),
192ea60b 103
4a8d113b 104 err => this.handleError(err)
2b084d70 105 )
b1794c53 106 }
ecb4e35f
C
107
108 askResetPassword () {
109 this.userService.askResetPassword(this.forgotPasswordEmail)
110 .subscribe(
141b177d 111 () => {
b1d40cff 112 const message = this.i18n(
f88ee4a9 113 'An email with the reset password instructions will be sent to {{email}}. The link will expire within 1 hour.',
b1d40cff
C
114 { email: this.forgotPasswordEmail }
115 )
f8b2c1b4 116 this.notifier.success(message)
ecb4e35f
C
117 this.hideForgotPasswordModal()
118 },
119
f8b2c1b4 120 err => this.notifier.error(err.message)
ecb4e35f
C
121 )
122 }
123
ecb4e35f 124 openForgotPasswordModal () {
63347a0f 125 this.openedForgotPasswordModal = this.modalService.open(this.forgotPasswordModal)
ecb4e35f
C
126 }
127
128 hideForgotPasswordModal () {
63347a0f 129 this.openedForgotPasswordModal.close()
ecb4e35f 130 }
4a8d113b
C
131
132 private loadExternalAuthToken (username: string, token: string) {
133 this.isAuthenticatedWithExternalAuth = true
134
135 this.authService.login(username, null, token)
136 .subscribe(
137 () => this.redirectService.redirectToPreviousRoute(),
138
139 err => {
140 this.handleError(err)
141 this.isAuthenticatedWithExternalAuth = false
142 }
143 )
144 }
145
146 private handleError (err: any) {
147 if (err.message.indexOf('credentials are invalid') !== -1) this.error = this.i18n('Incorrect username or password.')
148 else if (err.message.indexOf('blocked') !== -1) this.error = this.i18n('You account is blocked.')
149 else this.error = err.message
150 }
b1794c53 151}