[b101b69] | 1 | package mk.ukim.finki.busngo.service.impl;
|
---|
| 2 |
|
---|
| 3 | import mk.ukim.finki.busngo.model.entities.Korisnik;
|
---|
[bde8b13] | 4 | import mk.ukim.finki.busngo.model.entities.Patnik;
|
---|
[b101b69] | 5 | import mk.ukim.finki.busngo.model.exceptions.InvalidCredentialsException;
|
---|
| 6 | import mk.ukim.finki.busngo.model.exceptions.UserAlreadyExistsException;
|
---|
| 7 | import mk.ukim.finki.busngo.repository.KorisnikRepository;
|
---|
[bde8b13] | 8 | import mk.ukim.finki.busngo.repository.PatnikRepository;
|
---|
[b101b69] | 9 | import mk.ukim.finki.busngo.service.AuthService;
|
---|
| 10 | import org.springframework.security.crypto.password.PasswordEncoder;
|
---|
| 11 | import org.springframework.stereotype.Service;
|
---|
| 12 |
|
---|
| 13 | @Service
|
---|
| 14 | public class AuthServiceImpl implements AuthService {
|
---|
| 15 | private final KorisnikRepository korisnikRepository;
|
---|
[bde8b13] | 16 | private final PatnikRepository patnikRepository;
|
---|
[b101b69] | 17 | private final PasswordEncoder passwordEncoder;
|
---|
| 18 |
|
---|
[bde8b13] | 19 | public AuthServiceImpl(KorisnikRepository korisnikRepository, PatnikRepository patnikRepository, PasswordEncoder passwordEncoder) {
|
---|
[b101b69] | 20 | this.korisnikRepository = korisnikRepository;
|
---|
[bde8b13] | 21 | this.patnikRepository = patnikRepository;
|
---|
[b101b69] | 22 | this.passwordEncoder = passwordEncoder;
|
---|
| 23 | }
|
---|
| 24 |
|
---|
| 25 | @Override
|
---|
| 26 | public Korisnik login(String email, String password) {
|
---|
| 27 | if(email == null || password == null || email.isEmpty() || password.isEmpty())
|
---|
| 28 | throw new InvalidCredentialsException();
|
---|
| 29 | return korisnikRepository.findByKEmailAndKLozinka(email, password).orElseThrow(InvalidCredentialsException::new);
|
---|
| 30 | }
|
---|
| 31 |
|
---|
| 32 | @Override
|
---|
[bde8b13] | 33 | public Korisnik registerPatnik(String ime, String email, String password, String confirmPassword, String address, String telefon) {
|
---|
[b101b69] | 34 | if (email == null || password == null || email.isEmpty() || password.isEmpty()) {
|
---|
| 35 | throw new InvalidCredentialsException();
|
---|
| 36 | }
|
---|
| 37 |
|
---|
| 38 | if (!password.equals(confirmPassword)) {
|
---|
| 39 | throw new InvalidCredentialsException();
|
---|
| 40 | }
|
---|
| 41 |
|
---|
| 42 | if(this.korisnikRepository.findByKEmail(email).isPresent()) {
|
---|
| 43 | throw new UserAlreadyExistsException(email);
|
---|
| 44 | }
|
---|
| 45 |
|
---|
[bde8b13] | 46 | Patnik korisnik = new Patnik();
|
---|
[b101b69] | 47 | korisnik.setKIme(ime);
|
---|
| 48 | korisnik.setKAdresa(address);
|
---|
| 49 | korisnik.setKLozinka(passwordEncoder.encode(password));
|
---|
| 50 | korisnik.setKEmail(email);
|
---|
| 51 | korisnik.setKTelefon(telefon);
|
---|
| 52 | korisnik.setKIsAdmin(false);
|
---|
| 53 |
|
---|
[bde8b13] | 54 |
|
---|
| 55 | return patnikRepository.save(korisnik);
|
---|
[b101b69] | 56 | }
|
---|
| 57 |
|
---|
| 58 |
|
---|
| 59 | }
|
---|