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