]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame_incremental - client/src/app/login/login.component.ts
provide specific engine boundaries for nodejs and yarn
[github/Chocobozzz/PeerTube.git] / client / src / app / login / login.component.ts
... / ...
CommitLineData
1import { Component, ElementRef, OnInit, ViewChild, AfterViewInit } from '@angular/core'
2import { Notifier, RedirectService } from '@app/core'
3import { UserService } from '@app/shared'
4import { AuthService } from '../core'
5import { FormReactive } from '../shared'
6import { I18n } from '@ngx-translate/i18n-polyfill'
7import { FormValidatorService } from '@app/shared/forms/form-validators/form-validator.service'
8import { LoginValidatorsService } from '@app/shared/forms/form-validators/login-validators.service'
9import { NgbModal, NgbModalRef } from '@ng-bootstrap/ng-bootstrap'
10import { ActivatedRoute } from '@angular/router'
11import { ServerConfig, RegisteredExternalAuthConfig } from '@shared/models/server/server-config.model'
12import { environment } from 'src/environments/environment'
13import { HooksService } from '@app/core/plugins/hooks.service'
14
15@Component({
16 selector: 'my-login',
17 templateUrl: './login.component.html',
18 styleUrls: [ './login.component.scss' ]
19})
20
21export class LoginComponent extends FormReactive implements OnInit, AfterViewInit {
22 @ViewChild('usernameInput', { static: false }) usernameInput: ElementRef
23 @ViewChild('forgotPasswordModal', { static: true }) forgotPasswordModal: ElementRef
24
25 error: string = null
26 forgotPasswordEmail = ''
27
28 isAuthenticatedWithExternalAuth = false
29 externalAuthError = false
30 externalLogins: string[] = []
31
32 private openedForgotPasswordModal: NgbModalRef
33 private serverConfig: ServerConfig
34
35 constructor (
36 protected formValidatorService: FormValidatorService,
37 private route: ActivatedRoute,
38 private modalService: NgbModal,
39 private loginValidatorsService: LoginValidatorsService,
40 private authService: AuthService,
41 private userService: UserService,
42 private redirectService: RedirectService,
43 private notifier: Notifier,
44 private hooks: HooksService,
45 private i18n: I18n
46 ) {
47 super()
48 }
49
50 get signupAllowed () {
51 return this.serverConfig.signup.allowed === true
52 }
53
54 isEmailDisabled () {
55 return this.serverConfig.email.enabled === false
56 }
57
58 ngOnInit () {
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 }
67
68 if (snapshot.queryParams.externalAuthError) {
69 this.externalAuthError = true
70 return
71 }
72
73 this.buildForm({
74 username: this.loginValidatorsService.LOGIN_USERNAME,
75 password: this.loginValidatorsService.LOGIN_PASSWORD
76 })
77 }
78
79 ngAfterViewInit () {
80 if (this.usernameInput) {
81 this.usernameInput.nativeElement.focus()
82 }
83
84 this.hooks.runAction('action:login.init', 'login')
85 }
86
87 getExternalLogins () {
88 return this.serverConfig.plugin.registeredExternalAuths
89 }
90
91 getAuthHref (auth: RegisteredExternalAuthConfig) {
92 return environment.apiUrl + `/plugins/${auth.name}/${auth.version}/auth/${auth.authName}`
93 }
94
95 login () {
96 this.error = null
97
98 const { username, password } = this.form.value
99
100 this.authService.login(username, password)
101 .subscribe(
102 () => this.redirectService.redirectToPreviousRoute(),
103
104 err => this.handleError(err)
105 )
106 }
107
108 askResetPassword () {
109 this.userService.askResetPassword(this.forgotPasswordEmail)
110 .subscribe(
111 () => {
112 const message = this.i18n(
113 'An email with the reset password instructions will be sent to {{email}}. The link will expire within 1 hour.',
114 { email: this.forgotPasswordEmail }
115 )
116 this.notifier.success(message)
117 this.hideForgotPasswordModal()
118 },
119
120 err => this.notifier.error(err.message)
121 )
122 }
123
124 openForgotPasswordModal () {
125 this.openedForgotPasswordModal = this.modalService.open(this.forgotPasswordModal)
126 }
127
128 hideForgotPasswordModal () {
129 this.openedForgotPasswordModal.close()
130 }
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 }
151}