1
1
import { Component } from '@angular/core' ;
2
2
import { FormControl , FormGroup , FormsModule , ReactiveFormsModule , Validators } from '@angular/forms' ;
3
- import { RouterLink } from '@angular/router' ;
3
+ import { RouterLink , Router , ActivatedRoute } from '@angular/router' ;
4
4
import { SelectButtonModule } from 'primeng/selectbutton' ;
5
5
import { InputTextModule } from 'primeng/inputtext' ;
6
6
import { PasswordModule } from 'primeng/password' ;
@@ -12,6 +12,7 @@ import { PASSWORD_WEAK, STRONG_PASSWORD_REGEX, weakPasswordValidator } from './_
12
12
import { mismatchPasswordValidator , PASSWORD_MISMATCH } from './_validators/mismatch-password.validator' ;
13
13
import { invalidUsernameValidator , USERNAME_INVALID } from './_validators/invalid-username.validator' ;
14
14
import { invalidPasswordValidator , PASSWORD_INVALID } from './_validators/invalid-password.validator' ;
15
+ import { AuthenticationService } from '../services/authentication.service' ;
15
16
16
17
@Component ( {
17
18
selector : 'app-register' ,
@@ -27,12 +28,22 @@ import { invalidPasswordValidator, PASSWORD_INVALID } from './_validators/invali
27
28
ToastModule ,
28
29
ReactiveFormsModule ,
29
30
] ,
30
- providers : [ MessageService ] ,
31
+ providers : [ MessageService , AuthenticationService ] ,
31
32
templateUrl : './register.component.html' ,
32
33
styleUrl : './account.component.css' ,
33
34
} )
34
35
export class RegisterComponent {
35
- constructor ( private messageService : MessageService ) { }
36
+ constructor (
37
+ private messageService : MessageService ,
38
+ private authenticationService : AuthenticationService ,
39
+ private router : Router ,
40
+ private route : ActivatedRoute
41
+ ) {
42
+ // redirect to home if already logged in
43
+ if ( this . authenticationService . userValue ) {
44
+ this . router . navigate ( [ '/' ] ) ;
45
+ }
46
+ }
36
47
37
48
userForm : FormGroup = new FormGroup (
38
49
{
@@ -82,11 +93,36 @@ export class RegisterComponent {
82
93
onSubmit ( ) {
83
94
if ( this . userForm . valid ) {
84
95
this . isProcessingRegistration = true ;
85
- this . showError ( ) ;
86
- setTimeout ( ( ) => {
87
- this . isProcessingRegistration = false ;
88
- console . log ( 'Form Submitted' , this . userForm . value ) ;
89
- } , 3000 ) ;
96
+
97
+ this . authenticationService . createAccount (
98
+ this . userForm . value . username ,
99
+ this . userForm . value . email ,
100
+ this . userForm . value . password )
101
+ . pipe ( )
102
+ . subscribe ( {
103
+ next : ( ) => {
104
+ // get return url from route parameters or default to '/'
105
+ const returnUrl = this . route . snapshot . queryParams [ 'returnUrl' ] || '/' ;
106
+ this . router . navigate ( [ returnUrl ] ) ;
107
+ } ,
108
+ // error handling for registration because we assume there will be no errors with auto login
109
+ error : error => {
110
+ console . error ( error ) ;
111
+ this . isProcessingRegistration = false ;
112
+ let errorMessage = 'An unknown error occurred' ;
113
+ if ( error . status === 400 ) {
114
+ errorMessage = 'Missing Fields' ;
115
+ }
116
+ else if ( error . status === 401 ) {
117
+ errorMessage = 'There is already an account with that username or email' ;
118
+ }
119
+ else if ( error . status === 500 ) {
120
+ errorMessage = 'Database Server Error' ;
121
+ }
122
+ this . messageService . add ( { severity : 'error' , summary : 'Log In Error' , detail : errorMessage } ) ;
123
+ }
124
+ } )
125
+
90
126
} else {
91
127
console . log ( 'Invalid form' ) ;
92
128
}
0 commit comments