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