Allow iframes to open links
[oweals/peertube.git] / client / src / app / login / login.component.ts
1 import { Component, ElementRef, OnInit, ViewChild } from '@angular/core'
2 import { Notifier, RedirectService, ServerService } from '@app/core'
3 import { UserService } from '@app/shared'
4 import { AuthService } from '../core'
5 import { FormReactive } from '../shared'
6 import { I18n } from '@ngx-translate/i18n-polyfill'
7 import { FormValidatorService } from '@app/shared/forms/form-validators/form-validator.service'
8 import { LoginValidatorsService } from '@app/shared/forms/form-validators/login-validators.service'
9 import { NgbModal, NgbModalRef } from '@ng-bootstrap/ng-bootstrap'
10 import { ActivatedRoute, Router } from '@angular/router'
11 import { 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 {
20   @ViewChild('emailInput', { static: true }) input: ElementRef
21   @ViewChild('forgotPasswordModal', { static: true }) forgotPasswordModal: ElementRef
22
23   error: string = null
24   forgotPasswordEmail = ''
25
26   private openedForgotPasswordModal: NgbModalRef
27   private serverConfig: ServerConfig
28
29   constructor (
30     protected formValidatorService: FormValidatorService,
31     private router: Router,
32     private route: ActivatedRoute,
33     private modalService: NgbModal,
34     private loginValidatorsService: LoginValidatorsService,
35     private authService: AuthService,
36     private userService: UserService,
37     private serverService: ServerService,
38     private redirectService: RedirectService,
39     private notifier: Notifier,
40     private i18n: I18n
41   ) {
42     super()
43   }
44
45   get signupAllowed () {
46     return this.serverConfig.signup.allowed === true
47   }
48
49   isEmailDisabled () {
50     return this.serverConfig.email.enabled === false
51   }
52
53   ngOnInit () {
54     this.serverConfig = this.route.snapshot.data.serverConfig
55
56     this.buildForm({
57       username: this.loginValidatorsService.LOGIN_USERNAME,
58       password: this.loginValidatorsService.LOGIN_PASSWORD
59     })
60
61     this.input.nativeElement.focus()
62   }
63
64   login () {
65     this.error = null
66
67     const { username, password } = this.form.value
68
69     this.authService.login(username, password)
70       .subscribe(
71         () => this.redirectService.redirectToPreviousRoute(),
72
73         err => {
74           if (err.message.indexOf('credentials are invalid') !== -1) this.error = this.i18n('Incorrect username or password.')
75           else if (err.message.indexOf('blocked') !== -1) this.error = this.i18n('You account is blocked.')
76           else this.error = err.message
77         }
78       )
79   }
80
81   askResetPassword () {
82     this.userService.askResetPassword(this.forgotPasswordEmail)
83       .subscribe(
84         () => {
85           const message = this.i18n(
86             'An email with the reset password instructions will be sent to {{email}}. The link will expire within 1 hour.',
87             { email: this.forgotPasswordEmail }
88           )
89           this.notifier.success(message)
90           this.hideForgotPasswordModal()
91         },
92
93         err => this.notifier.error(err.message)
94       )
95   }
96
97   openForgotPasswordModal () {
98     this.openedForgotPasswordModal = this.modalService.open(this.forgotPasswordModal)
99   }
100
101   hideForgotPasswordModal () {
102     this.openedForgotPasswordModal.close()
103   }
104 }