Skip to content

Commit

Permalink
Throw Error Message At Invalid Credentials
Browse files Browse the repository at this point in the history
Add new alert that is shown when user gives invalid credentials. Also,
reformat profile service and remove unnecessary prints
  • Loading branch information
iliasmentz committed Sep 30, 2018
1 parent bf578ab commit 86218b2
Show file tree
Hide file tree
Showing 4 changed files with 109 additions and 105 deletions.
8 changes: 7 additions & 1 deletion src/main/client/src/app/shared/user/user.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ export class UserService {
}

loginUser(username: string, password: string) {
let success = true;
this.authService.loginUser(username, password)
.then(loginResponse => {

Expand All @@ -39,8 +40,13 @@ export class UserService {
})
.catch( err => {
console.log("can't get the user: " + err);
})
});
success = true;
})
.catch(error => {
success = false;
});
return success;
}

updateUser(userRequest: UserDto) {
Expand Down
91 changes: 44 additions & 47 deletions src/main/client/src/app/welcome/login/login.component.html
Original file line number Diff line number Diff line change
@@ -1,56 +1,53 @@
<!--<div class="limiter">-->
<div class="container-login100">
<div class="wrap-login100">
<div class="login100-pic js-tilt" data-tilt>
<img src="assets/img/img-01.png" alt="IMG">
</div>

<form class="login100-form validate-form" [formGroup]="loginForm" (ngSubmit)="onSubmit()">
<span class="login100-form-title">
Welcome to Worklife!
</span>

<div class="wrap-input100 validate-input" [ngClass]="{'ng-error': !loginForm.get('username').valid && loginForm.get('username').touched}" data-validate="Username is required">
<input class="input100" type="text" name="username" placeholder="Username" formControlName="username">
<span class="focus-input100"></span>
<span class="symbol-input100">
<i class="fa fa-user-secret" aria-hidden="true"></i>
</span>
</div>
<div class="container-login100">
<div class="wrap-login100">
<div class="login100-pic js-tilt" data-tilt>
<img src="assets/img/img-01.png" alt="IMG">
</div>

<div class="wrap-input100 validate-input" [ngClass]="{'ng-error': false}" data-validate="Password is required">
<input class="input100" type="password" name="password" placeholder="Password" formControlName="password">
<span class="focus-input100"></span>
<span class="symbol-input100">
<i class="fa fa-lock" aria-hidden="true"></i>
</span>
</div>
<form class="login100-form validate-form" [formGroup]="loginForm" (ngSubmit)="onSubmit()">
<div *ngIf="showError">
<alert type="danger">
<strong>Error:</strong> Invalid Credentials.
</alert>
</div>
<span class="login100-form-title">
Welcome to Worklife!
</span>

<div class="container-login100-form-btn">
<button class="login100-form-btn"
type="submit">
Login
</button>
</div>
<div class="wrap-input100 validate-input"
[ngClass]="{'ng-error': !loginForm.get('username').valid && loginForm.get('username').touched}"
data-validate="Username is required">
<input class="input100" type="text" name="username" placeholder="Username" formControlName="username">
<span class="focus-input100"></span>
<span class="symbol-input100">
<i class="fa fa-user-secret" aria-hidden="true"></i>
</span>
</div>

<!--<div class="text-center p-t-12">-->
<!--<span class="txt1">-->
<!--Forgot-->
<!--</span>-->
<!--<a class="txt2" href="#">-->
<!--Username / Password?-->
<!--</a>-->
<!--</div>-->
<div class="wrap-input100 validate-input" [ngClass]="{'ng-error': false}" data-validate="Password is required">
<input class="input100" type="password" name="password" placeholder="Password" formControlName="password">
<span class="focus-input100"></span>
<span class="symbol-input100">
<i class="fa fa-lock" aria-hidden="true"></i>
</span>
</div>

<div class="text-center p-t-136">
<a class="txt2" routerLink="/welcome/register">
Create your Account
<i class="fa fa-long-arrow-alt-right m-l-5" aria-hidden="true"></i>
</a>
</div>
</form>
</div>
<div class="container-login100-form-btn">
<button class="login100-form-btn"
type="submit">
Login
</button>
</div>
<div class="text-center p-t-136">
<a class="txt2" routerLink="/welcome/register">
Create your Account
<i class="fa fa-long-arrow-alt-right m-l-5" aria-hidden="true"></i>
</a>
</div>
</form>
</div>
</div>
<!--</div>-->

<script>
Expand Down
10 changes: 8 additions & 2 deletions src/main/client/src/app/welcome/login/login.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,14 @@ import {UserService} from "../../shared/user/user.service";
})
export class LoginComponent implements OnInit {
loginForm: FormGroup;

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

ngOnInit() {
this.showError = false;

this.loginForm = this._fb.group({
username: [null, Validators.required],
password: [null, Validators.required]
Expand All @@ -23,10 +25,14 @@ export class LoginComponent implements OnInit {
}

onSubmit() {
let invalid = false;
if (this.loginForm.valid) {
const username = this.loginForm.get('username').value;
const password = this.loginForm.get('password').value;
this.userService.loginUser(username, password);
invalid = this.userService.loginUser(username, password);
}
if (!this.loginForm.valid || invalid) {
this.showError = true
}
}

Expand Down
105 changes: 50 additions & 55 deletions src/main/java/com/linkedin/service/ProfileService.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,72 +17,67 @@

@Service
public class ProfileService {
private final LoginRepository loginRepository;
private final UserRepository userRepository;
private final UserService userService;
private final UserConverter userConverter;
private final PasswordEncoder passwordEncoder;
private final LoginRepository loginRepository;
private final UserRepository userRepository;
private final UserService userService;
private final UserConverter userConverter;
private final PasswordEncoder passwordEncoder;


@Autowired
public ProfileService(LoginRepository loginRepository, UserRepository userRepository, UserService userService, UserConverter userConverter, PasswordEncoder passwordEncoder) {
this.loginRepository = loginRepository;
this.userRepository = userRepository;
this.userService = userService;
this.userConverter = userConverter;
this.passwordEncoder = passwordEncoder;
}


@Autowired
public ProfileService(LoginRepository loginRepository, UserRepository userRepository, UserService userService, UserConverter userConverter, PasswordEncoder passwordEncoder) {
this.loginRepository = loginRepository;
this.userRepository = userRepository;
this.userService = userService;
this.userConverter = userConverter;
this.passwordEncoder = passwordEncoder;
}
//Returns UserDto
public UserDto getUserDto(String username) {
User user = userService.getUser(username);
Login login = loginRepository.getOne(user.getId());
UserDto userDto = userConverter.toUserDto(user, login.getRole().ordinal());
return userDto;
}


//Returns UserDto
public UserDto getUserDto(String username) {
User user = userService.getUser(username);
Login login = loginRepository.getOne(user.getId());
UserDto userDto = userConverter.toUserDto(user, login.getRole().ordinal());
return userDto;
}
public UserDto updateProfile(UserRequestDto userRequestDto) {
Login login = AuthenticationFacade.authenticatedUser();
User user = userService.getUser(login.getUserId()); //pairnoume ton User


public UserDto updateProfile(UserRequestDto userRequestDto) {
Login login = AuthenticationFacade.authenticatedUser();
User user = userService.getUser(login.getUserId()); //pairnoume ton User
user.setName(userRequestDto.getName());
user.setEmail(userRequestDto.getEmail());
user.setSurname(userRequestDto.getSurname());
user.setBirthdate(userRequestDto.getBirthdate());
user.setAddress(userRequestDto.getAddress());
user.setPhoneNumber(userRequestDto.getPhoneNumber());
user.setImgPath(userRequestDto.getImgPath());

userRepository.save(user);
return userConverter.toUserDto(user, login.getRole().ordinal());
}

user.setName(userRequestDto.getName());
user.setEmail(userRequestDto.getEmail());
user.setSurname(userRequestDto.getSurname());
user.setBirthdate(userRequestDto.getBirthdate());
user.setAddress(userRequestDto.getAddress());
user.setPhoneNumber(userRequestDto.getPhoneNumber());
user.setImgPath(userRequestDto.getImgPath());
public void changePassword(ChangePasswordRequestDto changePasswordRequestDto) throws Exception {
Login login = AuthenticationFacade.authenticatedUser();

userRepository.save(user);
return userConverter.toUserDto(user, login.getRole().ordinal());
}
if (!passwordEncoder.matches(changePasswordRequestDto.getOldPassword(), login.getPassword())) {
throw new WrongPasswordException();
}

public void changePassword(ChangePasswordRequestDto changePasswordRequestDto) throws Exception {
Login login = AuthenticationFacade.authenticatedUser();
System.out.println(login.getPassword());
login.setPassword(passwordEncoder.encode(changePasswordRequestDto.getNewPassword()));
loginRepository.save(login);
}

public void changeEmail(ChangeEmailRequestDto changeEmailRequestDto) throws Exception {
User user = userRepository.findById(AuthenticationFacade.authenticatedUser().getUserId()).orElse(null);
if (userRepository.existsByEmailIgnoreCase(changeEmailRequestDto.getNewEmail())) {
throw new Exception("Email already exists");
}
user.setEmail(changeEmailRequestDto.getNewEmail());
userRepository.save(user);

if(!passwordEncoder.matches(changePasswordRequestDto.getOldPassword(),login.getPassword() )) {
throw new WrongPasswordException();
}
// if(changePasswordRequestDto.getNewPassword() != changePasswordRequestDto.getNewPasswordRepeat()){
//
// throw new Exception("Diaforetikoi Kwdikoi");
// }
login.setPassword(passwordEncoder.encode(changePasswordRequestDto.getNewPassword()));
loginRepository.save(login);
}

public void changeEmail(ChangeEmailRequestDto changeEmailRequestDto) throws Exception {
User user = userRepository.findById(AuthenticationFacade.authenticatedUser().getUserId()).orElse(null);
if(userRepository.existsByEmailIgnoreCase(changeEmailRequestDto.getNewEmail())){
throw new Exception("Email already exists");
}
user.setEmail(changeEmailRequestDto.getNewEmail());
userRepository.save(user);

}
}

0 comments on commit 86218b2

Please sign in to comment.