Skip to content

Commit e9d33c2

Browse files
committed
Sign-In and Sign-Up is successfully done.
1 parent 0e4cd59 commit e9d33c2

File tree

6 files changed

+155
-0
lines changed

6 files changed

+155
-0
lines changed

src/main/java/com/manir/springbootecommercerestapi/config/SecurityConfig.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import org.springframework.context.annotation.Bean;
66
import org.springframework.context.annotation.Configuration;
77
import org.springframework.http.HttpMethod;
8+
import org.springframework.security.authentication.AuthenticationManager;
89
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
910
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
1011
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
@@ -38,6 +39,8 @@ protected void configure(HttpSecurity http) throws Exception {
3839
.authorizeRequests()
3940
//to permit all get request and secure post put and delete methods
4041
.antMatchers(HttpMethod.GET, "/api/**").permitAll()
42+
//authorize singIn and signUp
43+
.antMatchers("/api/v1/auth/**").permitAll()
4144
.anyRequest()
4245
.authenticated()
4346
.and()
@@ -67,4 +70,11 @@ protected void configure(AuthenticationManagerBuilder auth) throws Exception {
6770
PasswordEncoder passwordEncoder(){
6871
return new BCryptPasswordEncoder();
6972
}
73+
74+
//User authentication manager bean
75+
@Override
76+
@Bean
77+
public AuthenticationManager authenticationManagerBean() throws Exception {
78+
return super.authenticationManagerBean();
79+
}
7080
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package com.manir.springbootecommercerestapi.controller;
2+
3+
import com.manir.springbootecommercerestapi.dto.LoginDto;
4+
import com.manir.springbootecommercerestapi.dto.SignUpDto;
5+
import com.manir.springbootecommercerestapi.repository.UserRepository;
6+
import com.manir.springbootecommercerestapi.service.UserRegisterService;
7+
import org.springframework.beans.factory.annotation.Autowired;
8+
import org.springframework.http.HttpStatus;
9+
import org.springframework.http.ResponseEntity;
10+
import org.springframework.security.authentication.AuthenticationManager;
11+
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
12+
import org.springframework.security.core.Authentication;
13+
import org.springframework.security.core.context.SecurityContextHolder;
14+
import org.springframework.web.bind.annotation.PostMapping;
15+
import org.springframework.web.bind.annotation.RequestBody;
16+
import org.springframework.web.bind.annotation.RequestMapping;
17+
import org.springframework.web.bind.annotation.RestController;
18+
19+
20+
21+
@RestController
22+
@RequestMapping(value = "api/v1/auth")
23+
public class AuthController {
24+
25+
@Autowired
26+
private AuthenticationManager authenticationManager;
27+
@Autowired
28+
private UserRepository userRepository;
29+
@Autowired
30+
private UserRegisterService userRegisterService;
31+
32+
//login api
33+
@PostMapping("/login")
34+
public ResponseEntity<String> authenticateUser(@RequestBody LoginDto loginDto){
35+
36+
Authentication authentication = authenticationManager.authenticate(
37+
new UsernamePasswordAuthenticationToken(
38+
loginDto.getUserNameOrEmail(),
39+
loginDto.getPassword()
40+
)
41+
);
42+
SecurityContextHolder.getContext().setAuthentication(authentication);
43+
return new ResponseEntity<>("User sign-In successfully", HttpStatus.OK);
44+
}
45+
46+
//register api
47+
@PostMapping("/register")
48+
public ResponseEntity<?> registerUser(@RequestBody SignUpDto signUpDto){
49+
50+
//check for username exists in DB
51+
if (userRepository.existsByUserName(signUpDto.getUsername())){
52+
return new ResponseEntity<>("Username already exists", HttpStatus.BAD_REQUEST);
53+
}
54+
if (userRepository.existsByEmail(signUpDto.getEmail())){
55+
return new ResponseEntity<>("Email already exists", HttpStatus.BAD_REQUEST);
56+
}
57+
SignUpDto registeredUser = userRegisterService.registerUser(signUpDto);
58+
return new ResponseEntity<>("User is successfully registered", HttpStatus.OK);
59+
}
60+
61+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package com.manir.springbootecommercerestapi.dto;
2+
3+
import lombok.Data;
4+
5+
@Data
6+
public class LoginDto {
7+
private String userNameOrEmail;
8+
private String password;
9+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package com.manir.springbootecommercerestapi.dto;
2+
3+
import lombok.Data;
4+
5+
@Data
6+
public class SignUpDto {
7+
private String name;
8+
private String username;
9+
private String email;
10+
private String password;
11+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package com.manir.springbootecommercerestapi.service.Impl;
2+
3+
import com.manir.springbootecommercerestapi.dto.SignUpDto;
4+
import com.manir.springbootecommercerestapi.model.Role;
5+
import com.manir.springbootecommercerestapi.model.User;
6+
import com.manir.springbootecommercerestapi.repository.RoleRepository;
7+
import com.manir.springbootecommercerestapi.repository.UserRepository;
8+
import com.manir.springbootecommercerestapi.service.UserRegisterService;
9+
import org.modelmapper.ModelMapper;
10+
import org.springframework.beans.factory.annotation.Autowired;
11+
import org.springframework.security.crypto.password.PasswordEncoder;
12+
import org.springframework.stereotype.Service;
13+
14+
import java.util.Collections;
15+
16+
@Service
17+
public class UserRegisterServiceImpl implements UserRegisterService {
18+
19+
@Autowired
20+
private UserRepository userRepository;
21+
@Autowired
22+
private RoleRepository roleRepository;
23+
@Autowired
24+
private PasswordEncoder passwordEncoder;
25+
@Autowired
26+
private ModelMapper modelMapper;
27+
28+
@Override
29+
public SignUpDto registerUser(SignUpDto signUpDto) {
30+
31+
//convert dto to entity
32+
User user = mapToEntity(signUpDto);
33+
//save user to db
34+
User registeredUser = userRepository.save(user);
35+
return mapToDto(registeredUser);
36+
}
37+
38+
//map to dto
39+
private SignUpDto mapToDto(User user){
40+
SignUpDto signUpDto = modelMapper.map(user, SignUpDto.class);
41+
return signUpDto;
42+
}
43+
44+
//map to entity
45+
private User mapToEntity(SignUpDto signUpDto){
46+
User user = new User();
47+
user.setName(signUpDto.getName());
48+
user.setUserName(signUpDto.getUsername());
49+
user.setEmail(signUpDto.getEmail());
50+
user.setPassword(passwordEncoder.encode(signUpDto.getPassword()));
51+
52+
//add role to the user
53+
Role role = roleRepository.findByName("ROLE_USER").get();
54+
user.setRoles(Collections.singleton(role));
55+
return user;
56+
}
57+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package com.manir.springbootecommercerestapi.service;
2+
3+
import com.manir.springbootecommercerestapi.dto.SignUpDto;
4+
5+
public interface UserRegisterService {
6+
SignUpDto registerUser(SignUpDto signUpDto);
7+
}

0 commit comments

Comments
 (0)