]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/+signup/+register/register.component.ts
Split user service
[github/Chocobozzz/PeerTube.git] / client / src / app / +signup / +register / register.component.ts
1 import { Component, OnInit } from '@angular/core'
2 import { FormGroup } from '@angular/forms'
3 import { ActivatedRoute } from '@angular/router'
4 import { AuthService } from '@app/core'
5 import { HooksService } from '@app/core/plugins/hooks.service'
6 import { InstanceAboutAccordionComponent } from '@app/shared/shared-instance'
7 import { UserSignupService } from '@app/shared/shared-users'
8 import { NgbAccordion } from '@ng-bootstrap/ng-bootstrap'
9 import { UserRegister } from '@shared/models'
10 import { ServerConfig } from '@shared/models/server'
11
12 @Component({
13 selector: 'my-register',
14 templateUrl: './register.component.html',
15 styleUrls: [ './register.component.scss' ]
16 })
17 export class RegisterComponent implements OnInit {
18 accordion: NgbAccordion
19 info: string = null
20 error: string = null
21 success: string = null
22 signupDone = false
23
24 videoUploadDisabled: boolean
25
26 formStepTerms: FormGroup
27 formStepUser: FormGroup
28 formStepChannel: FormGroup
29
30 aboutHtml = {
31 codeOfConduct: ''
32 }
33
34 instanceInformationPanels = {
35 codeOfConduct: true,
36 terms: true,
37 administrators: false,
38 features: false,
39 moderation: false
40 }
41
42 defaultPreviousStepButtonLabel = $localize`:Button on the registration form to go to the previous step:Back`
43 defaultNextStepButtonLabel = $localize`:Button on the registration form to go to the previous step:Next`
44 stepUserButtonLabel = this.defaultNextStepButtonLabel
45
46 signupDisabled = false
47
48 private serverConfig: ServerConfig
49
50 constructor (
51 private route: ActivatedRoute,
52 private authService: AuthService,
53 private userSignupService: UserSignupService,
54 private hooks: HooksService
55 ) { }
56
57 get requiresEmailVerification () {
58 return this.serverConfig.signup.requiresEmailVerification
59 }
60
61 get minimumAge () {
62 return this.serverConfig.signup.minimumAge
63 }
64
65 ngOnInit (): void {
66 this.serverConfig = this.route.snapshot.data.serverConfig
67
68 if (this.serverConfig.signup.allowed === false || this.serverConfig.signup.allowedForCurrentIP === false) {
69 this.signupDisabled = true
70 return
71 }
72
73 this.videoUploadDisabled = this.serverConfig.user.videoQuota === 0
74 this.stepUserButtonLabel = this.videoUploadDisabled
75 ? $localize`:Button on the registration form to finalize the account and channel creation:Signup`
76 : this.defaultNextStepButtonLabel
77
78 this.hooks.runAction('action:signup.register.init', 'signup')
79
80 }
81
82 hasSameChannelAndAccountNames () {
83 return this.getUsername() === this.getChannelName()
84 }
85
86 getUsername () {
87 if (!this.formStepUser) return undefined
88
89 return this.formStepUser.value['username']
90 }
91
92 getChannelName () {
93 if (!this.formStepChannel) return undefined
94
95 return this.formStepChannel.value['name']
96 }
97
98 onTermsFormBuilt (form: FormGroup) {
99 this.formStepTerms = form
100 }
101
102 onUserFormBuilt (form: FormGroup) {
103 this.formStepUser = form
104 }
105
106 onChannelFormBuilt (form: FormGroup) {
107 this.formStepChannel = form
108 }
109
110 onTermsClick () {
111 if (this.accordion) this.accordion.toggle('terms')
112 }
113
114 onCodeOfConductClick () {
115 if (this.accordion) this.accordion.toggle('code-of-conduct')
116 }
117
118 onInstanceAboutAccordionInit (instanceAboutAccordion: InstanceAboutAccordionComponent) {
119 this.accordion = instanceAboutAccordion.accordion
120 this.aboutHtml = instanceAboutAccordion.aboutHtml
121 }
122
123 async signup () {
124 this.error = null
125
126 const body: UserRegister = await this.hooks.wrapObject(
127 Object.assign(this.formStepUser.value, { channel: this.videoUploadDisabled ? undefined : this.formStepChannel.value }),
128 'signup',
129 'filter:api.signup.registration.create.params'
130 )
131
132 this.userSignupService.signup(body).subscribe({
133 next: () => {
134 this.signupDone = true
135
136 if (this.requiresEmailVerification) {
137 this.info = $localize`Now please check your emails to verify your account and complete signup.`
138 return
139 }
140
141 // Auto login
142 this.authService.login(body.username, body.password)
143 .subscribe({
144 next: () => {
145 this.success = $localize`You are now logged in as ${body.username}!`
146 },
147
148 error: err => {
149 this.error = err.message
150 }
151 })
152 },
153
154 error: err => {
155 this.error = err.message
156 }
157 })
158 }
159 }