Skip to content

Commit

Permalink
Working on login from frontend
Browse files Browse the repository at this point in the history
  • Loading branch information
stp94 committed Mar 12, 2021
1 parent 7c89d88 commit 9fcd68a
Show file tree
Hide file tree
Showing 13 changed files with 92 additions and 97 deletions.
Binary file removed bazaDanych.mv.db
Binary file not shown.
6 changes: 6 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,12 @@
</dependency>


<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>





Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,8 @@ public UserDetails loadUserByUsername(String username) {
}
return new MyUserPrincipal(user);
}




}
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ protected void configure(HttpSecurity httpSecurity)
throws Exception {
httpSecurity.csrf().disable();
httpSecurity.authorizeRequests()
.antMatchers("/trucks/all","/session/player/**","/order/all").permitAll()
.antMatchers("/trucks/all","/session/player/**","/order/all","/register","/login").permitAll()
.anyRequest().authenticated()
.and()
.formLogin().permitAll()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
public class TruckforwarderBackendApplication {



public static void main(String[] args) {
SpringApplication.run(TruckforwarderBackendApplication.class, args);
}
Expand All @@ -42,4 +41,5 @@ public FilterRegistrationBean simpleCorsFilter() {
return bean;
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,9 @@ public void postPurchaseTruck(@RequestBody String SelectedTruck){
String selectedTruckByUser;
stringBuilder.append(SelectedTruck);
stringBuilder.delete(0,18);
stringBuilder.delete(stringBuilder.length()-2,stringBuilder.length()); // Format String from Post to correctly form
stringBuilder.delete(stringBuilder.length()-2,stringBuilder.length());
selectedTruckByUser = stringBuilder.toString();

BoughtTrucks boughtTrucks = new BoughtTrucks();

boughtTrucks.setIdbought_trucks(Math.toIntExact(boughtTrucksRepository.count() + 1));
boughtTrucks.setIdtrucks(truckRepository.findByName(selectedTruckByUser).getID());
boughtTrucks.setIdplayers(playerRepository.findByOwner(SecurityContextHolder.getContext().getAuthentication().getName()).getIdplayers());
Expand All @@ -77,12 +75,11 @@ public void postPurchaseTruck(@RequestBody String SelectedTruck){

boughtTrucksRepository.save(boughtTrucks); // Add Bought Truck


playerRepository.findByOwner(SecurityContextHolder.getContext().getAuthentication().getName()).
setCash(playerRepository.findByOwner(SecurityContextHolder.getContext().getAuthentication().getName()).getCash() -
truckRepository.findByName(selectedTruckByUser).getPrice()); // Set Cash Status
truckRepository.findByName(selectedTruckByUser).getPrice());

playerRepository.save(playerRepository.findByOwner(SecurityContextHolder.getContext().getAuthentication().getName())); // Update Cash Status
playerRepository.save(playerRepository.findByOwner(SecurityContextHolder.getContext().getAuthentication().getName()));



Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,60 +2,73 @@


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.web.bind.annotation.*;
import pl.staszczykpiotr.truckforwarder_backend.dto.Player;
import pl.staszczykpiotr.truckforwarder_backend.dto.User;
import pl.staszczykpiotr.truckforwarder_backend.repository.BoughtTrucksRepository;
import pl.staszczykpiotr.truckforwarder_backend.repository.PlayerRepository;

import java.security.Principal;
import pl.staszczykpiotr.truckforwarder_backend.repository.UserRepository;

@CrossOrigin
@RestController
@CrossOrigin
@RequestMapping("/principal")
public class UserController {
private PlayerRepository playerRepository;
private UserRepository userRepository;

@Autowired
public UserController(PlayerRepository playerRepository) {
public UserController(PlayerRepository playerRepository, UserRepository userRepository) {
this.playerRepository = playerRepository;
this.userRepository = userRepository;

}


@GetMapping
public User transferPrincipal(@RequestParam String username, @RequestParam String password){
User httpEntryUser = new User();
@PostMapping("/login")
public String loginUser(@RequestBody String username, String password) {

httpEntryUser.setUsername(username);
httpEntryUser.setPassword(password);
return "login_success";

return httpEntryUser;

}

@GetMapping("info")
public Principal retrievePrincipal(Principal principal) {

return principal;
@PostMapping("/register")
@ResponseStatus(code = HttpStatus.OK)
public String processRegister(String username, String password) {
User user = new User();
user.setUsername(username);
user.setPassword(password);
user.setEnabled(true);
user.setId((long) Math.toIntExact(playerRepository.count() + 1));
BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
String encodedPassword = passwordEncoder.encode(user.getPassword());
user.setPassword(encodedPassword);

userRepository.save(user);

Player player = new Player();
player.setIdplayers(Math.toIntExact(playerRepository.count() + 1));
player.setName(username);
player.setSpeed(100F);
player.setResponsibility(100F);
player.setRespect(100F);
player.setCash(15000F);
player.setFinished_courses(0);
player.setFailed_courses(0);
player.setOwner(username);

playerRepository.save(player);


return "login success";
}



@PostMapping()
public Boolean loginUser(@RequestBody String username, String password){

//return playerRepository.findByOwner(SecurityContextHolder.getContext().getAuthentication().getName());
return SecurityContextHolder.getContext().getAuthentication().isAuthenticated();

}
}




}
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
package pl.staszczykpiotr.truckforwarder_backend.dto;

import pl.staszczykpiotr.truckforwarder_backend.interfaces.ITruck;

import javax.persistence.*;


@Entity
@Table(name = "trucks")

public class Truck implements ITruck{
public class Truck {

@Id
@Column(name = "id")
Expand All @@ -31,50 +29,46 @@ public class Truck implements ITruck{
protected Float capacity;
@Column(name = "price")
protected Float price;
@Column(name = "status")
protected Byte status;


public Truck(){

};

@Override

public Integer getID() {
return idtrucks;
}
@Override

public String getName() {
return name;
}
@Override

public String getLoading() {
return loadingtype;
}
@Override

public Float getLength() {
return length;
}
@Override

public Float getWidth() {
return width;
}
@Override

public Float getHeight() {
return height;
}
@Override

public Float getWeight() {
return weight;
}
@Override

public Float getCapacity() { return capacity;
}
@Override

public Float getPrice() {
return price;
}
@Override
public Byte getStatus() {
return status;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,16 @@ public class User {

private String password;

public Boolean getEnabled() {
return enabled;
}

public void setEnabled(Boolean enabled) {
this.enabled = enabled;
}

private Boolean enabled;

public Long getId() {
return id;
}
Expand All @@ -40,4 +50,15 @@ public String getPassword() {
public void setPassword(String password) {
this.password = password;
}

// public void register(String username, String password){
// this.username = username;
// this.password = password;
// }



public User(){

}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,6 @@
public interface UserRepository extends JpaRepository<User, Long> {

User findByUsername(String username);
User readAllByUsernameAndPassword(String username, String password);

}
2 changes: 2 additions & 0 deletions src/main/resources/application.properties
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,6 @@ spring.datasource.username=root
spring.datasource.password=Zaq12wsx
spring.datasource.initialization-mode=always
logging.level.org.springframework.security=DEBUG
debug=true


28 changes: 0 additions & 28 deletions src/main/resources/hibernate.cfg.xml

This file was deleted.

0 comments on commit 9fcd68a

Please sign in to comment.