]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/login/login.component.ts
Handle external login errors
[github/Chocobozzz/PeerTube.git] / client / src / app / login / login.component.ts
1 import { Component, ElementRef, OnInit, ViewChild, AfterViewInit } 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, RegisteredExternalAuthConfig } from '@shared/models/server/server-config.model'
12 import { environment } from 'src/environments/environment'
13
14 @Component({
15 selector: 'my-login',
16 templateUrl: './login.component.html',
17 styleUrls: [ './login.component.scss' ]
18 })
19
20 export class LoginComponent extends FormReactive implements OnInit, AfterViewInit {
21 @ViewChild('usernameInput', { static: false }) usernameInput: ElementRef
22 @ViewChild('forgotPasswordModal', { static: true }) forgotPasswordModal: ElementRef
23
24 error: string = null
25 forgotPasswordEmail = ''
26
27 isAuthenticatedWithExternalAuth = false
28 externalAuthError = false
29 externalLogins: string[] = []
30
31 private openedForgotPasswordModal: NgbModalRef
32 private serverConfig: ServerConfig
33
34 constructor (
35 protected formValidatorService: FormValidatorService,
36 private route: ActivatedRoute,
37 private modalService: NgbModal,
38 private loginValidatorsService: LoginValidatorsService,
39 private authService: AuthService,
40 private userService: UserService,
41 private redirectService: RedirectService,
42 private notifier: Notifier,
43 private i18n: I18n
44 ) {
45 super()
46 }
47
48 get signupAllowed () {
49 return this.serverConfig.signup.allowed === true
50 }
51
52 isEmailDisabled () {
53 return this.serverConfig.email.enabled === false
54 }
55
56 ngOnInit () {
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 }
65
66 if (snapshot.queryParams.externalAuthError) {
67 this.externalAuthError = true
68 return
69 }
70
71 this.buildForm({
72 username: this.loginValidatorsService.LOGIN_USERNAME,
73 password: this.loginValidatorsService.LOGIN_PASSWORD
74 })
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 }
86
87 getAuthHref (auth: RegisteredExternalAuthConfig) {
88 return environment.apiUrl + `/plugins/${auth.name}/${auth.version}/auth/${auth.authName}`
89 }
90
91 login () {
92 this.error = null
93
94 const { username, password } = this.form.value
95
96 this.authService.login(username, password)
97 .subscribe(
98 () => this.redirectService.redirectToPreviousRoute(),
99
100 err => this.handleError(err)
101 )
102 }
103
104 askResetPassword () {
105 this.userService.askResetPassword(this.forgotPasswordEmail)
106 .subscribe(
107 () => {
108 const message = this.i18n(
109 'An email with the reset password instructions will be sent to {{email}}. The link will expire within 1 hour.',
110 { email: this.forgotPasswordEmail }
111 )
112 this.notifier.success(message)
113 this.hideForgotPasswordModal()
114 },
115
116 err => this.notifier.error(err.message)
117 )
118 }
119
120 openForgotPasswordModal () {
121 this.openedForgotPasswordModal = this.modalService.open(this.forgotPasswordModal)
122 }
123
124 hideForgotPasswordModal () {
125 this.openedForgotPasswordModal.close()
126 }
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 }
147 }