]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/+login/login.component.ts
Add unicode emoji to markdown
[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'
67ed6552
C
6import { FormReactive, FormValidatorService, LoginValidatorsService } from '@app/shared/shared-forms'
7import { NgbModal, NgbModalRef } from '@ng-bootstrap/ng-bootstrap'
67ed6552 8import { RegisteredExternalAuthConfig, ServerConfig } from '@shared/models'
b1794c53
C
9
10@Component({
a840d396 11 selector: 'my-login',
d235f6b0
C
12 templateUrl: './login.component.html',
13 styleUrls: [ './login.component.scss' ]
b1794c53
C
14})
15
ebefc902
C
16export class LoginComponent extends FormReactive implements OnInit, AfterViewInit {
17 @ViewChild('usernameInput', { static: false }) usernameInput: ElementRef
f36da21e 18 @ViewChild('forgotPasswordModal', { static: true }) forgotPasswordModal: ElementRef
ecb4e35f 19
df98563e 20 error: string = null
ecb4e35f 21 forgotPasswordEmail = ''
bc90883f 22
4a8d113b 23 isAuthenticatedWithExternalAuth = false
bc90883f 24 externalAuthError = false
ebefc902 25 externalLogins: string[] = []
192ea60b 26
63347a0f 27 private openedForgotPasswordModal: NgbModalRef
ba430d75 28 private serverConfig: ServerConfig
63347a0f 29
b1d40cff 30 constructor (
d18d6478 31 protected formValidatorService: FormValidatorService,
ba430d75 32 private route: ActivatedRoute,
63347a0f 33 private modalService: NgbModal,
e309822b 34 private loginValidatorsService: LoginValidatorsService,
b1d40cff
C
35 private authService: AuthService,
36 private userService: UserService,
b1d40cff 37 private redirectService: RedirectService,
f8b2c1b4 38 private notifier: Notifier,
66357162
C
39 private hooks: HooksService
40 ) {
df98563e 41 super()
4b2f33f3 42 }
b1794c53 43
2b084d70 44 get signupAllowed () {
ba430d75 45 return this.serverConfig.signup.allowed === true
2b084d70
C
46 }
47
3b3b1820 48 isEmailDisabled () {
ba430d75 49 return this.serverConfig.email.enabled === false
3b3b1820
C
50 }
51
df98563e 52 ngOnInit () {
4a8d113b
C
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 }
ba430d75 61
bc90883f
C
62 if (snapshot.queryParams.externalAuthError) {
63 this.externalAuthError = true
64 return
65 }
66
d18d6478 67 this.buildForm({
e309822b
C
68 username: this.loginValidatorsService.LOGIN_USERNAME,
69 password: this.loginValidatorsService.LOGIN_PASSWORD
d18d6478 70 })
ebefc902
C
71 }
72
73 ngAfterViewInit () {
74 if (this.usernameInput) {
75 this.usernameInput.nativeElement.focus()
76 }
f375bb3d
C
77
78 this.hooks.runAction('action:login.init', 'login')
ebefc902
C
79 }
80
81 getExternalLogins () {
82 return this.serverConfig.plugin.registeredExternalAuths
83 }
9fe44067 84
ebefc902
C
85 getAuthHref (auth: RegisteredExternalAuthConfig) {
86 return environment.apiUrl + `/plugins/${auth.name}/${auth.version}/auth/${auth.authName}`
0f6da32b
C
87 }
88
df98563e
C
89 login () {
90 this.error = null
4b2f33f3 91
df98563e 92 const { username, password } = this.form.value
4b2f33f3 93
2b084d70
C
94 this.authService.login(username, password)
95 .subscribe(
dae5ca24 96 () => this.redirectService.redirectToPreviousRoute(),
192ea60b 97
4a8d113b 98 err => this.handleError(err)
2b084d70 99 )
b1794c53 100 }
ecb4e35f
C
101
102 askResetPassword () {
103 this.userService.askResetPassword(this.forgotPasswordEmail)
104 .subscribe(
141b177d 105 () => {
66357162
C
106 const message = $localize`An email with the reset password instructions will be sent to ${this.forgotPasswordEmail}.
107The link will expire within 1 hour.`
108
f8b2c1b4 109 this.notifier.success(message)
ecb4e35f
C
110 this.hideForgotPasswordModal()
111 },
112
f8b2c1b4 113 err => this.notifier.error(err.message)
ecb4e35f
C
114 )
115 }
116
ecb4e35f 117 openForgotPasswordModal () {
63347a0f 118 this.openedForgotPasswordModal = this.modalService.open(this.forgotPasswordModal)
ecb4e35f
C
119 }
120
121 hideForgotPasswordModal () {
63347a0f 122 this.openedForgotPasswordModal.close()
ecb4e35f 123 }
4a8d113b
C
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) {
66357162
C
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.`
4a8d113b
C
142 else this.error = err.message
143 }
b1794c53 144}