Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
hoangtien2k3 committed Sep 7, 2023
1 parent 38698e7 commit cf90b04
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 4 deletions.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 0 additions & 2 deletions Unit-Testing-JPA/test-springboot-blog-rest-api/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,6 @@
<version>3.1.2</version>
</dependency>



<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package com.hoangtien2k3.testspringbootblogrestapi.controller;

import com.hoangtien2k3.testspringbootblogrestapi.dto.LoginDto;
import com.hoangtien2k3.testspringbootblogrestapi.dto.SignUpDto;
import com.hoangtien2k3.testspringbootblogrestapi.model.Role;
import com.hoangtien2k3.testspringbootblogrestapi.model.User;
import com.hoangtien2k3.testspringbootblogrestapi.repository.RoleRepository;
import com.hoangtien2k3.testspringbootblogrestapi.repository.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
Expand All @@ -16,6 +19,8 @@
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Collections;

@RestController
@RequestMapping("/api/auth")
public class AuthController {
Expand All @@ -35,12 +40,40 @@ public ResponseEntity<String> authenticateUser(@RequestBody LoginDto loginDto) {
Authentication authentication = authenticationManager
.authenticate(new UsernamePasswordAuthenticationToken(loginDto.getUsernameOrEmail(), loginDto.getPassword()));

// là một bộ nhớ lưu trữ thông tin xác thực hiện tại cho một luồng (thread)
SecurityContextHolder.getContext().setAuthentication(authentication);

return new ResponseEntity<>("User signed-in successfully.", HttpStatus.OK);
}

@PostMapping("/signup")
public ResponseEntity<?> registerUser(@RequestBody SignUpDto signUpDto) {

// check username exists in a DB
if (userRepository.existsByUsername(signUpDto.getUsername())) {
return new ResponseEntity<>("Username is already taken", HttpStatus.OK);
}

// check email exists in a DB
if (userRepository.existsByEmail(signUpDto.getEmail())) {
return new ResponseEntity<>("Username is already taken", HttpStatus.OK);
}

// create user object
User user = User.builder()
.name(signUpDto.getName())
.username(signUpDto.getUsername())
.email(signUpDto.getEmail())
.password(signUpDto.getPassword())
.build();

Role role = roleRepository.findByName("ROLE_ADMIN").get();
user.setRoles(Collections.singleton(role));

userRepository.save(user);

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



}
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package com.hoangtien2k3.testspringbootblogrestapi.dto;

import lombok.Builder;
import lombok.Data;

@Builder
@Data
public class SignUpDto {
private String name;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package com.hoangtien2k3.testspringbootblogrestapi.model;

import jakarta.persistence.*;
import lombok.Builder;
import lombok.Getter;
import lombok.Setter;

@Builder
@Setter
@Getter
@Entity
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package com.hoangtien2k3.testspringbootblogrestapi.model;

import jakarta.persistence.*;
import lombok.Builder;
import lombok.Data;

import java.util.Set;

@Data
@Builder
@Entity
@Table(name = "users", uniqueConstraints = {
@UniqueConstraint(columnNames = {"username"}),
Expand Down

0 comments on commit cf90b04

Please sign in to comment.