Skip to content

Commit

Permalink
feat: user auth service init
Browse files Browse the repository at this point in the history
  • Loading branch information
thutasann committed Jan 14, 2025
1 parent 4ba57c2 commit bf33608
Show file tree
Hide file tree
Showing 12 changed files with 178 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public class TradingBackendApplication {

private static final Logger logger = LoggerFactory.getLogger(TradingBackendApplication.class);

private static final String host = "http://localhost:8080/api/";
private static final String host = "http://localhost:8080/api/v1";
private static final String swagger = "http://localhost:8080/swagger-ui/index.html";

public static void main(String[] args) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ public class AppConfig {
SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
http.sessionManagement(session -> session.sessionCreationPolicy(SessionCreationPolicy.STATELESS))
.authorizeHttpRequests(authorize -> authorize
.requestMatchers("/api/v1/**").permitAll()
.requestMatchers("/api/**").authenticated().anyRequest().permitAll())
.addFilterBefore(new JwtTokenValidator(), BasicAuthenticationFilter.class)
.csrf(csrf -> csrf.disable())
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.thuta.trading_backend.controller;

import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import static org.springframework.http.HttpStatus.INTERNAL_SERVER_ERROR;

import org.springframework.beans.factory.annotation.Autowired;

import com.thuta.trading_backend.entity.User;
import com.thuta.trading_backend.response.DataResponse;
import com.thuta.trading_backend.service.auth.IAuthService;

@RestController
@RequestMapping("${api.prefix}/auth")
public class AuthController {

@Autowired
private IAuthService authService;

@PostMapping("/signup")
public ResponseEntity<DataResponse> register(
@RequestBody User user) throws Exception {
try {
User savedUser = authService.register(user);
return ResponseEntity.ok(new DataResponse("signup success", savedUser));
} catch (Exception e) {
return ResponseEntity.status(INTERNAL_SERVER_ERROR).body(new DataResponse(e.getMessage(), null));
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.thuta.trading_backend.controller;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("${api.prefix}")
public class HomeController {
@GetMapping
public String home() {
return "Welcome to trading platform";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.thuta.trading_backend.entity;

import com.thuta.trading_backend.enums.VERIFICATION_TYPE;

import lombok.Data;

/**
* Two Factor Authentication
*/
@Data
public class TwoFactorAuth {
private boolean isEnabled = false;
private VERIFICATION_TYPE sendTo;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.thuta.trading_backend.entity;

import com.fasterxml.jackson.annotation.JsonProperty;
import com.thuta.trading_backend.enums.USER_ROLE;

import jakarta.persistence.Embedded;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import lombok.Data;

/**
* User Entity
*/
@Entity
@Data
public class User {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;

private String fullName;

private String email;

@JsonProperty(access = JsonProperty.Access.WRITE_ONLY)
private String password;

@Embedded
private TwoFactorAuth twoFactorAuth = new TwoFactorAuth();

private USER_ROLE role = USER_ROLE.ROLE_CUSTOMER;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.thuta.trading_backend.enums;

/**
* User Role Enum
*/
public enum USER_ROLE {
ROLE_ADMIN,
ROLE_CUSTOMER
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.thuta.trading_backend.enums;

/**
* Verification Type Enum
*/
public enum VERIFICATION_TYPE {
MOBILE,
EMAIL
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.thuta.trading_backend.repository;

import org.springframework.data.jpa.repository.JpaRepository;

import com.thuta.trading_backend.entity.User;

public interface UserRepository extends JpaRepository<User, Long> {

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.thuta.trading_backend.response;

import lombok.AllArgsConstructor;
import lombok.Data;

/**
* Data Response Object
*/
@Data
@AllArgsConstructor
public class DataResponse {
private String message;
private Object data;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.thuta.trading_backend.service.auth;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

import com.thuta.trading_backend.entity.User;
import com.thuta.trading_backend.repository.UserRepository;

@Service
public class AuthService implements IAuthService {
@Autowired
private UserRepository userRepo;

@Autowired
private PasswordEncoder passwordEncoder;

@Override
public User register(User user) throws Exception {
try {
User newUser = new User();
newUser.setFullName(user.getFullName());
newUser.setEmail(user.getEmail());
newUser.setPassword(passwordEncoder.encode(user.getPassword()));

User savedUser = userRepo.save(newUser);
return savedUser;
} catch (Exception e) {
throw new Exception("Something went wrong at signup");
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.thuta.trading_backend.service.auth;

import com.thuta.trading_backend.entity.User;

public interface IAuthService {
User register(User user) throws Exception;
}

0 comments on commit bf33608

Please sign in to comment.