Merge branch 'release/v1.3.0' into develop
[oweals/peertube.git] / client / src / app / +signup / +register / register.component.ts
1 import { Component } from '@angular/core'
2 import { AuthService, Notifier, RedirectService, ServerService } from '@app/core'
3 import { UserService, UserValidatorsService } from '@app/shared'
4 import { I18n } from '@ngx-translate/i18n-polyfill'
5 import { UserRegister } from '@shared/models/users/user-register.model'
6 import { FormGroup } from '@angular/forms'
7
8 @Component({
9   selector: 'my-register',
10   templateUrl: './register.component.html',
11   styleUrls: [ './register.component.scss' ]
12 })
13 export class RegisterComponent {
14   info: string = null
15   error: string = null
16   success: string = null
17   signupDone = false
18
19   formStepUser: FormGroup
20   formStepChannel: FormGroup
21
22   constructor (
23     private authService: AuthService,
24     private userValidatorsService: UserValidatorsService,
25     private notifier: Notifier,
26     private userService: UserService,
27     private serverService: ServerService,
28     private redirectService: RedirectService,
29     private i18n: I18n
30   ) {
31   }
32
33   get requiresEmailVerification () {
34     return this.serverService.getConfig().signup.requiresEmailVerification
35   }
36
37   hasSameChannelAndAccountNames () {
38     return this.getUsername() === this.getChannelName()
39   }
40
41   getUsername () {
42     if (!this.formStepUser) return undefined
43
44     return this.formStepUser.value['username']
45   }
46
47   getChannelName () {
48     if (!this.formStepChannel) return undefined
49
50     return this.formStepChannel.value['name']
51   }
52
53   onUserFormBuilt (form: FormGroup) {
54     this.formStepUser = form
55   }
56
57   onChannelFormBuilt (form: FormGroup) {
58     this.formStepChannel = form
59   }
60
61   signup () {
62     this.error = null
63
64     const body: UserRegister = Object.assign(this.formStepUser.value, { channel: this.formStepChannel.value })
65
66     this.userService.signup(body).subscribe(
67       () => {
68         this.signupDone = true
69
70         if (this.requiresEmailVerification) {
71           this.info = this.i18n('Now please check your emails to verify your account and complete signup.')
72           return
73         }
74
75         // Auto login
76         this.authService.login(body.username, body.password)
77             .subscribe(
78               () => {
79                 this.success = this.i18n('You are now logged in as {{username}}!', { username: body.username })
80               },
81
82               err => this.error = err.message
83             )
84       },
85
86       err => this.error = err.message
87     )
88   }
89 }