]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/login/login.component.ts
Add external login buttons
[github/Chocobozzz/PeerTube.git] / client / src / app / login / login.component.ts
1 import { Component, ElementRef, OnInit, ViewChild, AfterViewInit } from '@angular/core'
2 import { Notifier, RedirectService } from '@app/core'
3 import { UserService } from '@app/shared'
4 import { AuthService } from '../core'
5 import { FormReactive } from '../shared'
6 import { I18n } from '@ngx-translate/i18n-polyfill'
7 import { FormValidatorService } from '@app/shared/forms/form-validators/form-validator.service'
8 import { LoginValidatorsService } from '@app/shared/forms/form-validators/login-validators.service'
9 import { NgbModal, NgbModalRef } from '@ng-bootstrap/ng-bootstrap'
10 import { ActivatedRoute } from '@angular/router'
11 import { ServerConfig, RegisteredExternalAuthConfig } from '@shared/models/server/server-config.model'
12 import { environment } from 'src/environments/environment'
13
14 @Component({
15 selector: 'my-login',
16 templateUrl: './login.component.html',
17 styleUrls: [ './login.component.scss' ]
18 })
19
20 export class LoginComponent extends FormReactive implements OnInit, AfterViewInit {
21 @ViewChild('usernameInput', { static: false }) usernameInput: ElementRef
22 @ViewChild('forgotPasswordModal', { static: true }) forgotPasswordModal: ElementRef
23
24 error: string = null
25 forgotPasswordEmail = ''
26 isAuthenticatedWithExternalAuth = false
27 externalLogins: string[] = []
28
29 private openedForgotPasswordModal: NgbModalRef
30 private serverConfig: ServerConfig
31
32 constructor (
33 protected formValidatorService: FormValidatorService,
34 private route: ActivatedRoute,
35 private modalService: NgbModal,
36 private loginValidatorsService: LoginValidatorsService,
37 private authService: AuthService,
38 private userService: UserService,
39 private redirectService: RedirectService,
40 private notifier: Notifier,
41 private i18n: I18n
42 ) {
43 super()
44 }
45
46 get signupAllowed () {
47 return this.serverConfig.signup.allowed === true
48 }
49
50 isEmailDisabled () {
51 return this.serverConfig.email.enabled === false
52 }
53
54 ngOnInit () {
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 }
63
64 this.buildForm({
65 username: this.loginValidatorsService.LOGIN_USERNAME,
66 password: this.loginValidatorsService.LOGIN_PASSWORD
67 })
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 }
79
80 getAuthHref (auth: RegisteredExternalAuthConfig) {
81 return environment.apiUrl + `/plugins/${auth.name}/${auth.version}/auth/${auth.authName}`
82 }
83
84 login () {
85 this.error = null
86
87 const { username, password } = this.form.value
88
89 this.authService.login(username, password)
90 .subscribe(
91 () => this.redirectService.redirectToPreviousRoute(),
92
93 err => this.handleError(err)
94 )
95 }
96
97 askResetPassword () {
98 this.userService.askResetPassword(this.forgotPasswordEmail)
99 .subscribe(
100 () => {
101 const message = this.i18n(
102 'An email with the reset password instructions will be sent to {{email}}. The link will expire within 1 hour.',
103 { email: this.forgotPasswordEmail }
104 )
105 this.notifier.success(message)
106 this.hideForgotPasswordModal()
107 },
108
109 err => this.notifier.error(err.message)
110 )
111 }
112
113 openForgotPasswordModal () {
114 this.openedForgotPasswordModal = this.modalService.open(this.forgotPasswordModal)
115 }
116
117 hideForgotPasswordModal () {
118 this.openedForgotPasswordModal.close()
119 }
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 }
140 }