Skip to content

Commit

Permalink
Register Error Messages
Browse files Browse the repository at this point in the history
Add register error messages in the form. Register Controller now returns
ResponseEntity with the error message that we want to be displayed.
  • Loading branch information
iliasmentz committed Sep 30, 2018
1 parent 6b335b5 commit 14c9df0
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 5 deletions.
10 changes: 10 additions & 0 deletions src/main/client/src/app/welcome/register/register.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,16 @@
<img src="assets/img/img-01.png" alt="IMG">
</div>
<form class="register100-form validate-form" (ngSubmit)="onSubmit()" [formGroup]="registerForm">
<div *ngIf="showError">
<alert type="danger">
<strong>Error:</strong> {{ errorMessage }}
</alert>
</div>
<div *ngIf="showPasswordMismatch">
<alert type="danger">
<strong>Error:</strong> Passwords does not match!
</alert>
</div>
<span class="register100-form-title">
Hello, Welcome to Worklife!
</span>
Expand Down
14 changes: 13 additions & 1 deletion src/main/client/src/app/welcome/register/register.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,16 @@ import {UserService} from "../../shared/user/user.service";
})
export class RegisterComponent implements OnInit {
registerForm: FormGroup;
showError: boolean;
showPasswordMismatch: boolean;
errorMessage: string;

constructor(private _fb: FormBuilder,
private userService: UserService) { }

ngOnInit() {
this.showError = false;
this.showPasswordMismatch = false;
this.registerForm = this._fb.group({
email: [null, [Validators.required, Validators.email]],
name: [null, [Validators.required, Validators.minLength(3)]],
Expand All @@ -30,12 +35,19 @@ export class RegisterComponent implements OnInit {

onSubmit() {
if (this.registerForm.valid) {
this.showPasswordMismatch = false;
let registerRequest = new Register(this.registerForm);
this.userService.register(registerRequest)
.then(() => {
this.userService.loginUser(registerRequest.username, registerRequest.password);
})
.catch(err => console.log(err));
.catch(err => {
console.log(err);
this.showError = true;
this.errorMessage = err.error;
});
} else {
this.showPasswordMismatch = true;
}
}
}
10 changes: 6 additions & 4 deletions src/main/java/com/linkedin/controller/AuthController.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
Expand Down Expand Up @@ -42,19 +44,19 @@ public AuthController(UserService userService, UserConverter userConverter, User

@ApiOperation(value = "Register", notes = "Creates a new user", response = String.class)
@PostMapping(UrlConst.REGISTER)
public RegisterDto registerUser(@Valid @RequestBody RegisterRequestDto registerRequestDto) {
public ResponseEntity<String> registerUser(@Valid @RequestBody RegisterRequestDto registerRequestDto) {
if (userService.usernameTaken(registerRequestDto.getUsername())) {
return new RegisterDto("Username is already taken!");
return new ResponseEntity<>("Username is already taken!", HttpStatus.NOT_ACCEPTABLE);
}

if (userService.emailExists(registerRequestDto.getEmail())) {
return new RegisterDto("Email Address already in use!");
return new ResponseEntity<>("Email Address already in use!", HttpStatus.NOT_ACCEPTABLE);
}

// Creating user's account
userService.register(registerRequestDto);

return new RegisterDto("User registered successfully");
return new ResponseEntity<>("User registered successfully", HttpStatus.OK);
}

@ApiOperation(value = "Returns Users", notes = "Returns all Users", response = String.class)
Expand Down

0 comments on commit 14c9df0

Please sign in to comment.