]>
Commit | Line | Data |
---|---|---|
1d5342ab | 1 | import { Component } from '@angular/core' |
f8b2c1b4 | 2 | import { AuthService, Notifier, RedirectService, ServerService } from '@app/core' |
b247a132 | 3 | import { UserService, UserValidatorsService } from '@app/shared' |
b1d40cff | 4 | import { I18n } from '@ngx-translate/i18n-polyfill' |
1d5342ab C |
5 | import { UserRegister } from '@shared/models/users/user-register.model' |
6 | import { FormGroup } from '@angular/forms' | |
a184c71b C |
7 | |
8 | @Component({ | |
b247a132 C |
9 | selector: 'my-register', |
10 | templateUrl: './register.component.html', | |
11 | styleUrls: [ './register.component.scss' ] | |
a184c71b | 12 | }) |
b247a132 | 13 | export class RegisterComponent { |
d8c9996c | 14 | info: string = null |
df98563e | 15 | error: string = null |
1d5342ab | 16 | success: string = null |
d8c9996c | 17 | signupDone = false |
a184c71b | 18 | |
1d5342ab C |
19 | formStepUser: FormGroup |
20 | formStepChannel: FormGroup | |
21 | ||
df98563e | 22 | constructor ( |
43e9d2af | 23 | private authService: AuthService, |
e309822b | 24 | private userValidatorsService: UserValidatorsService, |
f8b2c1b4 | 25 | private notifier: Notifier, |
5afdd0a5 | 26 | private userService: UserService, |
d9eaee39 | 27 | private serverService: ServerService, |
b1d40cff | 28 | private redirectService: RedirectService, |
b1d40cff | 29 | private i18n: I18n |
a184c71b | 30 | ) { |
8a19bee1 C |
31 | } |
32 | ||
d9eaee39 JM |
33 | get requiresEmailVerification () { |
34 | return this.serverService.getConfig().signup.requiresEmailVerification | |
35 | } | |
36 | ||
1d5342ab C |
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 | |
a184c71b C |
59 | } |
60 | ||
df98563e C |
61 | signup () { |
62 | this.error = null | |
a184c71b | 63 | |
b247a132 | 64 | const body: UserRegister = Object.assign(this.formStepUser.value, { channel: this.formStepChannel.value }) |
a184c71b | 65 | |
1d5342ab | 66 | this.userService.signup(body).subscribe( |
a184c71b | 67 | () => { |
d8c9996c C |
68 | this.signupDone = true |
69 | ||
d9eaee39 | 70 | if (this.requiresEmailVerification) { |
1d5342ab | 71 | this.info = this.i18n('Now please check your emails to verify your account and complete signup.') |
d8c9996c | 72 | return |
d9eaee39 | 73 | } |
d8c9996c | 74 | |
43e9d2af | 75 | // Auto login |
1d5342ab | 76 | this.authService.login(body.username, body.password) |
43e9d2af C |
77 | .subscribe( |
78 | () => { | |
1d5342ab | 79 | this.success = this.i18n('You are now logged in as {{username}}!', { username: body.username }) |
43e9d2af C |
80 | }, |
81 | ||
82 | err => this.error = err.message | |
83 | ) | |
a184c71b C |
84 | }, |
85 | ||
f7354483 | 86 | err => this.error = err.message |
df98563e | 87 | ) |
a184c71b C |
88 | } |
89 | } |