]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/login/login.component.ts
Add external login tests
[github/Chocobozzz/PeerTube.git] / client / src / app / login / login.component.ts
CommitLineData
ecb4e35f 1import { Component, ElementRef, OnInit, ViewChild } 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
RK
10import { ActivatedRoute } from '@angular/router'
11import { ServerConfig } from '@shared/models/server/server-config.model'
b1794c53
C
12
13@Component({
a840d396 14 selector: 'my-login',
d235f6b0
C
15 templateUrl: './login.component.html',
16 styleUrls: [ './login.component.scss' ]
b1794c53
C
17})
18
4b2f33f3 19export class LoginComponent extends FormReactive implements OnInit {
f36da21e
C
20 @ViewChild('emailInput', { static: true }) input: ElementRef
21 @ViewChild('forgotPasswordModal', { static: true }) forgotPasswordModal: ElementRef
ecb4e35f 22
df98563e 23 error: string = null
ecb4e35f 24 forgotPasswordEmail = ''
4a8d113b 25 isAuthenticatedWithExternalAuth = false
192ea60b 26
63347a0f 27 private openedForgotPasswordModal: NgbModalRef
ba430d75 28 private serverConfig: ServerConfig
63347a0f 29
b1d40cff 30 constructor (
d18d6478 31 protected formValidatorService: FormValidatorService,
ba430d75 32 private route: ActivatedRoute,
63347a0f 33 private modalService: NgbModal,
e309822b 34 private loginValidatorsService: LoginValidatorsService,
b1d40cff
C
35 private authService: AuthService,
36 private userService: UserService,
b1d40cff 37 private redirectService: RedirectService,
f8b2c1b4 38 private notifier: Notifier,
b1d40cff
C
39 private i18n: I18n
40 ) {
df98563e 41 super()
4b2f33f3 42 }
b1794c53 43
2b084d70 44 get signupAllowed () {
ba430d75 45 return this.serverConfig.signup.allowed === true
2b084d70
C
46 }
47
3b3b1820 48 isEmailDisabled () {
ba430d75 49 return this.serverConfig.email.enabled === false
3b3b1820
C
50 }
51
df98563e 52 ngOnInit () {
4a8d113b
C
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 }
ba430d75 61
d18d6478 62 this.buildForm({
e309822b
C
63 username: this.loginValidatorsService.LOGIN_USERNAME,
64 password: this.loginValidatorsService.LOGIN_PASSWORD
d18d6478 65 })
9fe44067
RK
66
67 this.input.nativeElement.focus()
0f6da32b
C
68 }
69
df98563e
C
70 login () {
71 this.error = null
4b2f33f3 72
df98563e 73 const { username, password } = this.form.value
4b2f33f3 74
2b084d70
C
75 this.authService.login(username, password)
76 .subscribe(
dae5ca24 77 () => this.redirectService.redirectToPreviousRoute(),
192ea60b 78
4a8d113b 79 err => this.handleError(err)
2b084d70 80 )
b1794c53 81 }
ecb4e35f
C
82
83 askResetPassword () {
84 this.userService.askResetPassword(this.forgotPasswordEmail)
85 .subscribe(
141b177d 86 () => {
b1d40cff 87 const message = this.i18n(
f88ee4a9 88 'An email with the reset password instructions will be sent to {{email}}. The link will expire within 1 hour.',
b1d40cff
C
89 { email: this.forgotPasswordEmail }
90 )
f8b2c1b4 91 this.notifier.success(message)
ecb4e35f
C
92 this.hideForgotPasswordModal()
93 },
94
f8b2c1b4 95 err => this.notifier.error(err.message)
ecb4e35f
C
96 )
97 }
98
ecb4e35f 99 openForgotPasswordModal () {
63347a0f 100 this.openedForgotPasswordModal = this.modalService.open(this.forgotPasswordModal)
ecb4e35f
C
101 }
102
103 hideForgotPasswordModal () {
63347a0f 104 this.openedForgotPasswordModal.close()
ecb4e35f 105 }
4a8d113b
C
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 }
b1794c53 126}