]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/+login/login.component.ts
improvements to login and sign-up pages (#3357)
[github/Chocobozzz/PeerTube.git] / client / src / app / +login / login.component.ts
CommitLineData
ebefc902 1import { environment } from 'src/environments/environment'
67ed6552
C
2import { AfterViewInit, Component, ElementRef, OnInit, ViewChild } from '@angular/core'
3import { ActivatedRoute } from '@angular/router'
4import { AuthService, Notifier, RedirectService, UserService } from '@app/core'
f375bb3d 5import { HooksService } from '@app/core/plugins/hooks.service'
40360c17 6import { InstanceAboutAccordionComponent } from '@app/shared/shared-instance'
7ed1edbb
C
7import { LOGIN_PASSWORD_VALIDATOR, LOGIN_USERNAME_VALIDATOR } from '@app/shared/form-validators/login-validators'
8import { FormReactive, FormValidatorService } from '@app/shared/shared-forms'
40360c17 9import { NgbAccordion, NgbModal, NgbModalRef } from '@ng-bootstrap/ng-bootstrap'
67ed6552 10import { RegisteredExternalAuthConfig, ServerConfig } from '@shared/models'
b1794c53
C
11
12@Component({
a840d396 13 selector: 'my-login',
d235f6b0
C
14 templateUrl: './login.component.html',
15 styleUrls: [ './login.component.scss' ]
b1794c53
C
16})
17
ebefc902
C
18export class LoginComponent extends FormReactive implements OnInit, AfterViewInit {
19 @ViewChild('usernameInput', { static: false }) usernameInput: ElementRef
f36da21e 20 @ViewChild('forgotPasswordModal', { static: true }) forgotPasswordModal: ElementRef
ecb4e35f 21
40360c17 22 accordion: NgbAccordion
df98563e 23 error: string = null
ecb4e35f 24 forgotPasswordEmail = ''
bc90883f 25
4a8d113b 26 isAuthenticatedWithExternalAuth = false
bc90883f 27 externalAuthError = false
ebefc902 28 externalLogins: string[] = []
192ea60b 29
40360c17
K
30 instanceInformationPanels = {
31 terms: true,
32 administrators: false,
33 features: false,
34 moderation: false,
35 codeOfConduct: false
36 }
37
63347a0f 38 private openedForgotPasswordModal: NgbModalRef
ba430d75 39 private serverConfig: ServerConfig
63347a0f 40
b1d40cff 41 constructor (
d18d6478 42 protected formValidatorService: FormValidatorService,
ba430d75 43 private route: ActivatedRoute,
63347a0f 44 private modalService: NgbModal,
b1d40cff
C
45 private authService: AuthService,
46 private userService: UserService,
b1d40cff 47 private redirectService: RedirectService,
f8b2c1b4 48 private notifier: Notifier,
66357162
C
49 private hooks: HooksService
50 ) {
df98563e 51 super()
4b2f33f3 52 }
b1794c53 53
2b084d70 54 get signupAllowed () {
ba430d75 55 return this.serverConfig.signup.allowed === true
2b084d70
C
56 }
57
40360c17
K
58 onTermsClick (event: Event, instanceInformation: HTMLElement) {
59 event.preventDefault()
60
61 if (this.accordion) {
62 this.accordion.expand('terms')
63 instanceInformation.scrollIntoView({ behavior: 'smooth' })
64 }
65 }
66
3b3b1820 67 isEmailDisabled () {
ba430d75 68 return this.serverConfig.email.enabled === false
3b3b1820
C
69 }
70
df98563e 71 ngOnInit () {
4a8d113b
C
72 const snapshot = this.route.snapshot
73
74 this.serverConfig = snapshot.data.serverConfig
75
76 if (snapshot.queryParams.externalAuthToken) {
77 this.loadExternalAuthToken(snapshot.queryParams.username, snapshot.queryParams.externalAuthToken)
78 return
79 }
ba430d75 80
bc90883f
C
81 if (snapshot.queryParams.externalAuthError) {
82 this.externalAuthError = true
83 return
84 }
85
d18d6478 86 this.buildForm({
7ed1edbb
C
87 username: LOGIN_USERNAME_VALIDATOR,
88 password: LOGIN_PASSWORD_VALIDATOR
d18d6478 89 })
ebefc902
C
90 }
91
92 ngAfterViewInit () {
93 if (this.usernameInput) {
94 this.usernameInput.nativeElement.focus()
95 }
f375bb3d
C
96
97 this.hooks.runAction('action:login.init', 'login')
ebefc902
C
98 }
99
100 getExternalLogins () {
101 return this.serverConfig.plugin.registeredExternalAuths
102 }
9fe44067 103
ebefc902
C
104 getAuthHref (auth: RegisteredExternalAuthConfig) {
105 return environment.apiUrl + `/plugins/${auth.name}/${auth.version}/auth/${auth.authName}`
0f6da32b
C
106 }
107
df98563e
C
108 login () {
109 this.error = null
4b2f33f3 110
df98563e 111 const { username, password } = this.form.value
4b2f33f3 112
2b084d70
C
113 this.authService.login(username, password)
114 .subscribe(
dae5ca24 115 () => this.redirectService.redirectToPreviousRoute(),
192ea60b 116
4a8d113b 117 err => this.handleError(err)
2b084d70 118 )
b1794c53 119 }
ecb4e35f
C
120
121 askResetPassword () {
122 this.userService.askResetPassword(this.forgotPasswordEmail)
123 .subscribe(
141b177d 124 () => {
66357162
C
125 const message = $localize`An email with the reset password instructions will be sent to ${this.forgotPasswordEmail}.
126The link will expire within 1 hour.`
127
f8b2c1b4 128 this.notifier.success(message)
ecb4e35f
C
129 this.hideForgotPasswordModal()
130 },
131
f8b2c1b4 132 err => this.notifier.error(err.message)
ecb4e35f
C
133 )
134 }
135
ecb4e35f 136 openForgotPasswordModal () {
63347a0f 137 this.openedForgotPasswordModal = this.modalService.open(this.forgotPasswordModal)
ecb4e35f
C
138 }
139
140 hideForgotPasswordModal () {
63347a0f 141 this.openedForgotPasswordModal.close()
ecb4e35f 142 }
4a8d113b 143
40360c17
K
144 onInstanceAboutAccordionInit (instanceAboutAccordion: InstanceAboutAccordionComponent) {
145 this.accordion = instanceAboutAccordion.accordion
146 }
147
4a8d113b
C
148 private loadExternalAuthToken (username: string, token: string) {
149 this.isAuthenticatedWithExternalAuth = true
150
151 this.authService.login(username, null, token)
152 .subscribe(
153 () => this.redirectService.redirectToPreviousRoute(),
154
155 err => {
156 this.handleError(err)
157 this.isAuthenticatedWithExternalAuth = false
158 }
159 )
160 }
161
162 private handleError (err: any) {
66357162
C
163 if (err.message.indexOf('credentials are invalid') !== -1) this.error = $localize`Incorrect username or password.`
164 else if (err.message.indexOf('blocked') !== -1) this.error = $localize`Your account is blocked.`
4a8d113b
C
165 else this.error = err.message
166 }
b1794c53 167}