]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/+login/login.component.ts
e9336172e8b8e5f904d80479999ed745d3b27bd4
[github/Chocobozzz/PeerTube.git] / client / src / app / +login / login.component.ts
1 import { environment } from 'src/environments/environment'
2 import { AfterViewInit, Component, ElementRef, OnInit, ViewChild } from '@angular/core'
3 import { ActivatedRoute } from '@angular/router'
4 import { AuthService, Notifier, RedirectService, UserService } from '@app/core'
5 import { HooksService } from '@app/core/plugins/hooks.service'
6 import { FormReactive, FormValidatorService, LoginValidatorsService } from '@app/shared/shared-forms'
7 import { NgbModal, NgbModalRef } from '@ng-bootstrap/ng-bootstrap'
8 import { RegisteredExternalAuthConfig, ServerConfig } from '@shared/models'
9
10 @Component({
11 selector: 'my-login',
12 templateUrl: './login.component.html',
13 styleUrls: [ './login.component.scss' ]
14 })
15
16 export class LoginComponent extends FormReactive implements OnInit, AfterViewInit {
17 @ViewChild('usernameInput', { static: false }) usernameInput: ElementRef
18 @ViewChild('forgotPasswordModal', { static: true }) forgotPasswordModal: ElementRef
19
20 error: string = null
21 forgotPasswordEmail = ''
22
23 isAuthenticatedWithExternalAuth = false
24 externalAuthError = false
25 externalLogins: string[] = []
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 hooks: HooksService
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 if (snapshot.queryParams.externalAuthError) {
63 this.externalAuthError = true
64 return
65 }
66
67 this.buildForm({
68 username: this.loginValidatorsService.LOGIN_USERNAME,
69 password: this.loginValidatorsService.LOGIN_PASSWORD
70 })
71 }
72
73 ngAfterViewInit () {
74 if (this.usernameInput) {
75 this.usernameInput.nativeElement.focus()
76 }
77
78 this.hooks.runAction('action:login.init', 'login')
79 }
80
81 getExternalLogins () {
82 return this.serverConfig.plugin.registeredExternalAuths
83 }
84
85 getAuthHref (auth: RegisteredExternalAuthConfig) {
86 return environment.apiUrl + `/plugins/${auth.name}/${auth.version}/auth/${auth.authName}`
87 }
88
89 login () {
90 this.error = null
91
92 const { username, password } = this.form.value
93
94 this.authService.login(username, password)
95 .subscribe(
96 () => this.redirectService.redirectToPreviousRoute(),
97
98 err => this.handleError(err)
99 )
100 }
101
102 askResetPassword () {
103 this.userService.askResetPassword(this.forgotPasswordEmail)
104 .subscribe(
105 () => {
106 const message = $localize`An email with the reset password instructions will be sent to ${this.forgotPasswordEmail}.
107 The link will expire within 1 hour.`
108
109 this.notifier.success(message)
110 this.hideForgotPasswordModal()
111 },
112
113 err => this.notifier.error(err.message)
114 )
115 }
116
117 openForgotPasswordModal () {
118 this.openedForgotPasswordModal = this.modalService.open(this.forgotPasswordModal)
119 }
120
121 hideForgotPasswordModal () {
122 this.openedForgotPasswordModal.close()
123 }
124
125 private loadExternalAuthToken (username: string, token: string) {
126 this.isAuthenticatedWithExternalAuth = true
127
128 this.authService.login(username, null, token)
129 .subscribe(
130 () => this.redirectService.redirectToPreviousRoute(),
131
132 err => {
133 this.handleError(err)
134 this.isAuthenticatedWithExternalAuth = false
135 }
136 )
137 }
138
139 private handleError (err: any) {
140 if (err.message.indexOf('credentials are invalid') !== -1) this.error = $localize`Incorrect username or password.`
141 else if (err.message.indexOf('blocked') !== -1) this.error = $localize`Your account is blocked.`
142 else this.error = err.message
143 }
144 }