]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/login/login.component.ts
Add external login tests
[github/Chocobozzz/PeerTube.git] / client / src / app / login / login.component.ts
1 import { Component, ElementRef, OnInit, ViewChild } from '@angular/core'
2 import { Notifier, RedirectService } 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 } from '@angular/router'
11 import { ServerConfig } from '@shared/models/server/server-config.model'
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 isAuthenticatedWithExternalAuth = false
26
27 private openedForgotPasswordModal: NgbModalRef
28 private serverConfig: ServerConfig
29
30 constructor (
31 protected formValidatorService: FormValidatorService,
32 private route: ActivatedRoute,
33 private modalService: NgbModal,
34 private loginValidatorsService: LoginValidatorsService,
35 private authService: AuthService,
36 private userService: UserService,
37 private redirectService: RedirectService,
38 private notifier: Notifier,
39 private i18n: I18n
40 ) {
41 super()
42 }
43
44 get signupAllowed () {
45 return this.serverConfig.signup.allowed === true
46 }
47
48 isEmailDisabled () {
49 return this.serverConfig.email.enabled === false
50 }
51
52 ngOnInit () {
53 const snapshot = this.route.snapshot
54
55 this.serverConfig = snapshot.data.serverConfig
56
57 if (snapshot.queryParams.externalAuthToken) {
58 this.loadExternalAuthToken(snapshot.queryParams.username, snapshot.queryParams.externalAuthToken)
59 return
60 }
61
62 this.buildForm({
63 username: this.loginValidatorsService.LOGIN_USERNAME,
64 password: this.loginValidatorsService.LOGIN_PASSWORD
65 })
66
67 this.input.nativeElement.focus()
68 }
69
70 login () {
71 this.error = null
72
73 const { username, password } = this.form.value
74
75 this.authService.login(username, password)
76 .subscribe(
77 () => this.redirectService.redirectToPreviousRoute(),
78
79 err => this.handleError(err)
80 )
81 }
82
83 askResetPassword () {
84 this.userService.askResetPassword(this.forgotPasswordEmail)
85 .subscribe(
86 () => {
87 const message = this.i18n(
88 'An email with the reset password instructions will be sent to {{email}}. The link will expire within 1 hour.',
89 { email: this.forgotPasswordEmail }
90 )
91 this.notifier.success(message)
92 this.hideForgotPasswordModal()
93 },
94
95 err => this.notifier.error(err.message)
96 )
97 }
98
99 openForgotPasswordModal () {
100 this.openedForgotPasswordModal = this.modalService.open(this.forgotPasswordModal)
101 }
102
103 hideForgotPasswordModal () {
104 this.openedForgotPasswordModal.close()
105 }
106
107 private loadExternalAuthToken (username: string, token: string) {
108 this.isAuthenticatedWithExternalAuth = true
109
110 this.authService.login(username, null, token)
111 .subscribe(
112 () => this.redirectService.redirectToPreviousRoute(),
113
114 err => {
115 this.handleError(err)
116 this.isAuthenticatedWithExternalAuth = false
117 }
118 )
119 }
120
121 private handleError (err: any) {
122 if (err.message.indexOf('credentials are invalid') !== -1) this.error = this.i18n('Incorrect username or password.')
123 else if (err.message.indexOf('blocked') !== -1) this.error = this.i18n('You account is blocked.')
124 else this.error = err.message
125 }
126 }