]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/login/login.component.ts
Add external login buttons
[github/Chocobozzz/PeerTube.git] / client / src / app / login / login.component.ts
CommitLineData
ebefc902 1import { Component, ElementRef, OnInit, ViewChild, AfterViewInit } from '@angular/core'
000eb0e4 2import { Notifier, RedirectService } from '@app/core'
ecb4e35f 3import { UserService } from '@app/shared'
df98563e
C
4import { AuthService } from '../core'
5import { FormReactive } from '../shared'
b1d40cff 6import { I18n } from '@ngx-translate/i18n-polyfill'
d18d6478 7import { FormValidatorService } from '@app/shared/forms/form-validators/form-validator.service'
e309822b 8import { LoginValidatorsService } from '@app/shared/forms/form-validators/login-validators.service'
63347a0f 9import { NgbModal, NgbModalRef } from '@ng-bootstrap/ng-bootstrap'
000eb0e4 10import { ActivatedRoute } from '@angular/router'
ebefc902
C
11import { ServerConfig, RegisteredExternalAuthConfig } from '@shared/models/server/server-config.model'
12import { environment } from 'src/environments/environment'
b1794c53
C
13
14@Component({
a840d396 15 selector: 'my-login',
d235f6b0
C
16 templateUrl: './login.component.html',
17 styleUrls: [ './login.component.scss' ]
b1794c53
C
18})
19
ebefc902
C
20export class LoginComponent extends FormReactive implements OnInit, AfterViewInit {
21 @ViewChild('usernameInput', { static: false }) usernameInput: ElementRef
f36da21e 22 @ViewChild('forgotPasswordModal', { static: true }) forgotPasswordModal: ElementRef
ecb4e35f 23
df98563e 24 error: string = null
ecb4e35f 25 forgotPasswordEmail = ''
4a8d113b 26 isAuthenticatedWithExternalAuth = false
ebefc902 27 externalLogins: string[] = []
192ea60b 28
63347a0f 29 private openedForgotPasswordModal: NgbModalRef
ba430d75 30 private serverConfig: ServerConfig
63347a0f 31
b1d40cff 32 constructor (
d18d6478 33 protected formValidatorService: FormValidatorService,
ba430d75 34 private route: ActivatedRoute,
63347a0f 35 private modalService: NgbModal,
e309822b 36 private loginValidatorsService: LoginValidatorsService,
b1d40cff
C
37 private authService: AuthService,
38 private userService: UserService,
b1d40cff 39 private redirectService: RedirectService,
f8b2c1b4 40 private notifier: Notifier,
b1d40cff
C
41 private i18n: I18n
42 ) {
df98563e 43 super()
4b2f33f3 44 }
b1794c53 45
2b084d70 46 get signupAllowed () {
ba430d75 47 return this.serverConfig.signup.allowed === true
2b084d70
C
48 }
49
3b3b1820 50 isEmailDisabled () {
ba430d75 51 return this.serverConfig.email.enabled === false
3b3b1820
C
52 }
53
df98563e 54 ngOnInit () {
4a8d113b
C
55 const snapshot = this.route.snapshot
56
57 this.serverConfig = snapshot.data.serverConfig
58
59 if (snapshot.queryParams.externalAuthToken) {
60 this.loadExternalAuthToken(snapshot.queryParams.username, snapshot.queryParams.externalAuthToken)
61 return
62 }
ba430d75 63
d18d6478 64 this.buildForm({
e309822b
C
65 username: this.loginValidatorsService.LOGIN_USERNAME,
66 password: this.loginValidatorsService.LOGIN_PASSWORD
d18d6478 67 })
ebefc902
C
68 }
69
70 ngAfterViewInit () {
71 if (this.usernameInput) {
72 this.usernameInput.nativeElement.focus()
73 }
74 }
75
76 getExternalLogins () {
77 return this.serverConfig.plugin.registeredExternalAuths
78 }
9fe44067 79
ebefc902
C
80 getAuthHref (auth: RegisteredExternalAuthConfig) {
81 return environment.apiUrl + `/plugins/${auth.name}/${auth.version}/auth/${auth.authName}`
0f6da32b
C
82 }
83
df98563e
C
84 login () {
85 this.error = null
4b2f33f3 86
df98563e 87 const { username, password } = this.form.value
4b2f33f3 88
2b084d70
C
89 this.authService.login(username, password)
90 .subscribe(
dae5ca24 91 () => this.redirectService.redirectToPreviousRoute(),
192ea60b 92
4a8d113b 93 err => this.handleError(err)
2b084d70 94 )
b1794c53 95 }
ecb4e35f
C
96
97 askResetPassword () {
98 this.userService.askResetPassword(this.forgotPasswordEmail)
99 .subscribe(
141b177d 100 () => {
b1d40cff 101 const message = this.i18n(
f88ee4a9 102 'An email with the reset password instructions will be sent to {{email}}. The link will expire within 1 hour.',
b1d40cff
C
103 { email: this.forgotPasswordEmail }
104 )
f8b2c1b4 105 this.notifier.success(message)
ecb4e35f
C
106 this.hideForgotPasswordModal()
107 },
108
f8b2c1b4 109 err => this.notifier.error(err.message)
ecb4e35f
C
110 )
111 }
112
ecb4e35f 113 openForgotPasswordModal () {
63347a0f 114 this.openedForgotPasswordModal = this.modalService.open(this.forgotPasswordModal)
ecb4e35f
C
115 }
116
117 hideForgotPasswordModal () {
63347a0f 118 this.openedForgotPasswordModal.close()
ecb4e35f 119 }
4a8d113b
C
120
121 private loadExternalAuthToken (username: string, token: string) {
122 this.isAuthenticatedWithExternalAuth = true
123
124 this.authService.login(username, null, token)
125 .subscribe(
126 () => this.redirectService.redirectToPreviousRoute(),
127
128 err => {
129 this.handleError(err)
130 this.isAuthenticatedWithExternalAuth = false
131 }
132 )
133 }
134
135 private handleError (err: any) {
136 if (err.message.indexOf('credentials are invalid') !== -1) this.error = this.i18n('Incorrect username or password.')
137 else if (err.message.indexOf('blocked') !== -1) this.error = this.i18n('You account is blocked.')
138 else this.error = err.message
139 }
b1794c53 140}