]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/login/login.component.ts
Handle external login errors
[github/Chocobozzz/PeerTube.git] / client / src / app / login / login.component.ts
CommitLineData
ebefc902 1import { Component, ElementRef, OnInit, ViewChild, AfterViewInit } 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 10import { ActivatedRoute } from '@angular/router'
ebefc902
C
11import { ServerConfig, RegisteredExternalAuthConfig } from '@shared/models/server/server-config.model'
12import { environment } from 'src/environments/environment'
b1794c53
C
13
14@Component({
a840d396 15 selector: 'my-login',
d235f6b0
C
16 templateUrl: './login.component.html',
17 styleUrls: [ './login.component.scss' ]
b1794c53
C
18})
19
ebefc902
C
20export class LoginComponent extends FormReactive implements OnInit, AfterViewInit {
21 @ViewChild('usernameInput', { static: false }) usernameInput: ElementRef
f36da21e 22 @ViewChild('forgotPasswordModal', { static: true }) forgotPasswordModal: ElementRef
ecb4e35f 23
df98563e 24 error: string = null
ecb4e35f 25 forgotPasswordEmail = ''
bc90883f 26
4a8d113b 27 isAuthenticatedWithExternalAuth = false
bc90883f 28 externalAuthError = false
ebefc902 29 externalLogins: string[] = []
192ea60b 30
63347a0f 31 private openedForgotPasswordModal: NgbModalRef
ba430d75 32 private serverConfig: ServerConfig
63347a0f 33
b1d40cff 34 constructor (
d18d6478 35 protected formValidatorService: FormValidatorService,
ba430d75 36 private route: ActivatedRoute,
63347a0f 37 private modalService: NgbModal,
e309822b 38 private loginValidatorsService: LoginValidatorsService,
b1d40cff
C
39 private authService: AuthService,
40 private userService: UserService,
b1d40cff 41 private redirectService: RedirectService,
f8b2c1b4 42 private notifier: Notifier,
b1d40cff
C
43 private i18n: I18n
44 ) {
df98563e 45 super()
4b2f33f3 46 }
b1794c53 47
2b084d70 48 get signupAllowed () {
ba430d75 49 return this.serverConfig.signup.allowed === true
2b084d70
C
50 }
51
3b3b1820 52 isEmailDisabled () {
ba430d75 53 return this.serverConfig.email.enabled === false
3b3b1820
C
54 }
55
df98563e 56 ngOnInit () {
4a8d113b
C
57 const snapshot = this.route.snapshot
58
59 this.serverConfig = snapshot.data.serverConfig
60
61 if (snapshot.queryParams.externalAuthToken) {
62 this.loadExternalAuthToken(snapshot.queryParams.username, snapshot.queryParams.externalAuthToken)
63 return
64 }
ba430d75 65
bc90883f
C
66 if (snapshot.queryParams.externalAuthError) {
67 this.externalAuthError = true
68 return
69 }
70
d18d6478 71 this.buildForm({
e309822b
C
72 username: this.loginValidatorsService.LOGIN_USERNAME,
73 password: this.loginValidatorsService.LOGIN_PASSWORD
d18d6478 74 })
ebefc902
C
75 }
76
77 ngAfterViewInit () {
78 if (this.usernameInput) {
79 this.usernameInput.nativeElement.focus()
80 }
81 }
82
83 getExternalLogins () {
84 return this.serverConfig.plugin.registeredExternalAuths
85 }
9fe44067 86
ebefc902
C
87 getAuthHref (auth: RegisteredExternalAuthConfig) {
88 return environment.apiUrl + `/plugins/${auth.name}/${auth.version}/auth/${auth.authName}`
0f6da32b
C
89 }
90
df98563e
C
91 login () {
92 this.error = null
4b2f33f3 93
df98563e 94 const { username, password } = this.form.value
4b2f33f3 95
2b084d70
C
96 this.authService.login(username, password)
97 .subscribe(
dae5ca24 98 () => this.redirectService.redirectToPreviousRoute(),
192ea60b 99
4a8d113b 100 err => this.handleError(err)
2b084d70 101 )
b1794c53 102 }
ecb4e35f
C
103
104 askResetPassword () {
105 this.userService.askResetPassword(this.forgotPasswordEmail)
106 .subscribe(
141b177d 107 () => {
b1d40cff 108 const message = this.i18n(
f88ee4a9 109 'An email with the reset password instructions will be sent to {{email}}. The link will expire within 1 hour.',
b1d40cff
C
110 { email: this.forgotPasswordEmail }
111 )
f8b2c1b4 112 this.notifier.success(message)
ecb4e35f
C
113 this.hideForgotPasswordModal()
114 },
115
f8b2c1b4 116 err => this.notifier.error(err.message)
ecb4e35f
C
117 )
118 }
119
ecb4e35f 120 openForgotPasswordModal () {
63347a0f 121 this.openedForgotPasswordModal = this.modalService.open(this.forgotPasswordModal)
ecb4e35f
C
122 }
123
124 hideForgotPasswordModal () {
63347a0f 125 this.openedForgotPasswordModal.close()
ecb4e35f 126 }
4a8d113b
C
127
128 private loadExternalAuthToken (username: string, token: string) {
129 this.isAuthenticatedWithExternalAuth = true
130
131 this.authService.login(username, null, token)
132 .subscribe(
133 () => this.redirectService.redirectToPreviousRoute(),
134
135 err => {
136 this.handleError(err)
137 this.isAuthenticatedWithExternalAuth = false
138 }
139 )
140 }
141
142 private handleError (err: any) {
143 if (err.message.indexOf('credentials are invalid') !== -1) this.error = this.i18n('Incorrect username or password.')
144 else if (err.message.indexOf('blocked') !== -1) this.error = this.i18n('You account is blocked.')
145 else this.error = err.message
146 }
b1794c53 147}