Skip to content

Commit

Permalink
✨ feat: Implement Login Validations #20 #19, Implement Token and Name…
Browse files Browse the repository at this point in the history
… Local Storage Persistence #18, Home Route Definition #16, Product Backlog Route Definition #17, Implement Spinner in Login Page #21.

Co-authored-by: ajuliamm <[email protected]>
Co-authored-by: CaiquePrado <[email protected]>
Co-authored-by: ChaiCaroline <[email protected]>
  • Loading branch information
4 people committed Nov 17, 2023
1 parent aaeed32 commit d85b518
Show file tree
Hide file tree
Showing 22 changed files with 496 additions and 138 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,28 @@

import javax.naming.AuthenticationException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.BadCredentialsException;
import org.springframework.security.authentication.DisabledException;
import org.springframework.security.authentication.LockedException;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.db.scrumtrackerapi.model.Customer;
import com.db.scrumtrackerapi.model.TokenMessage;
import com.db.scrumtrackerapi.model.dto.LoginDTO;
import com.db.scrumtrackerapi.security.service.TokenService;
import com.db.scrumtrackerapi.services.CustomerService;

@RestController
@CrossOrigin("http://localhost:5173/")
public class AuthController {

@Autowired
Expand All @@ -25,14 +32,20 @@ public class AuthController {
@Autowired
private TokenService tokenService;

@RequestMapping(value = "/login", method = RequestMethod.GET)
public String login(@RequestBody LoginDTO login) throws AuthenticationException {
@Autowired
private CustomerService customerService;

@RequestMapping(value = "/login", method = RequestMethod.POST)
public ResponseEntity<TokenMessage> login(@RequestBody LoginDTO login) throws AuthenticationException {
UsernamePasswordAuthenticationToken usernamePasswordAuthenticationToken =
new UsernamePasswordAuthenticationToken(login.getEmail(), login.getPassword());
try {
Authentication authenticate = this.authenticationManager.authenticate(usernamePasswordAuthenticationToken);
UserDetails user = (UserDetails) authenticate.getPrincipal();
return tokenService.generateToken(user);
Customer customer = customerService.findByEmail(user.getUsername()).get();
String token = tokenService.generateToken(user);
TokenMessage responseBody = new TokenMessage(token, customer.getName(), customer.getLastName());
return ResponseEntity.ok().body(responseBody);
}
catch (DisabledException ex) {
throw new AuthenticationException("User " + login.getEmail() + " is disabled.");
Expand All @@ -44,6 +57,4 @@ public String login(@RequestBody LoginDTO login) throws AuthenticationException
throw new AuthenticationException("Bad credentials.");
}
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,17 @@

import com.db.scrumtrackerapi.model.ErrorMessage;
import jakarta.persistence.EntityExistsException;
import jakarta.validation.ConstraintViolationException;

@ControllerAdvice
public class ExceptionControllerAdvice {

@ExceptionHandler(ConstraintViolationException.class)
@ResponseStatus(HttpStatus.BAD_REQUEST)
public ResponseEntity<ErrorMessage> manipularExcecoesDeValidacao(ConstraintViolationException ex) {
ErrorMessage response = new ErrorMessage("Validation failed.", HttpStatus.BAD_REQUEST.value(), ex.getMessage());
return ResponseEntity.badRequest().body(response);
}

@ExceptionHandler(EntityExistsException.class)
public ResponseEntity<ErrorMessage> handleEntityExistsException(EntityExistsException ex) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package com.db.scrumtrackerapi.model;
import java.util.Objects;

public class TokenMessage {
private String Token;
private String name;
private String lastName;

protected TokenMessage() {
}

public TokenMessage(String Token, String name, String lastName) {
this.Token = Token;
this.name = name;
this.lastName = lastName;
}

public String getToken() {
return this.Token;
}

public void setToken(String Token) {
this.Token = Token;
}

public String getName() {
return this.name;
}

public void setName(String name) {
this.name = name;
}

public String getLastName() {
return this.lastName;
}

public void setLastName(String lastName) {
this.lastName = lastName;
}

@Override
public boolean equals(Object o) {
if (o == this)
return true;
if (!(o instanceof TokenMessage)) {
return false;
}
TokenMessage tokenMessage = (TokenMessage) o;
return Objects.equals(Token, tokenMessage.Token) && Objects.equals(name, tokenMessage.name) && Objects.equals(lastName, tokenMessage.lastName);
}

@Override
public int hashCode() {
return Objects.hash(Token, name, lastName);
}

@Override
public String toString() {
return "{" +
" Token='" + getToken() + "'" +
", name='" + getName() + "'" +
", lastName='" + getLastName() + "'" +
"}";
}

}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ public class SecurityFilters {
@Bean
public SecurityFilterChain defaultSecurityFilterChain(HttpSecurity http) throws Exception {
http
.cors(withDefaults())
//should be enabled in production environment
.csrf(csrf -> csrf.disable())
.sessionManagement(session ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public class TokenService {
public String generateToken(UserDetails userDetails) {
return JWT.create().withIssuer(issuer)
.withSubject(userDetails.getUsername())
.withExpiresAt(LocalDateTime.now().plusMinutes(10).toInstant(ZoneOffset.of("-03:00")))
.withExpiresAt(LocalDateTime.now().plusHours(1L).toInstant(ZoneOffset.of("-03:00")))
.sign(Algorithm.HMAC256(secret));
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/=\ /=\ |
\ /=: /= | | /=\=\ | /= /=| /=: =/ /=\ /=
\=/ \=: | \=/ | | | | | \=| \=: |\ \= |

Scrum Tracker v1.0.0
Powered by Spring Boot v3.1.5
43 changes: 42 additions & 1 deletion scrum-tracker-front-end/scrum-tracker-ui/package-lock.json

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

2 changes: 2 additions & 0 deletions scrum-tracker-front-end/scrum-tracker-ui/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,11 @@
},
"dependencies": {
"axios": "^1.6.1",
"cors": "^2.8.5",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"react-router-dom": "^6.18.0",
"react-toastify": "^9.1.3",
"styled-components": "^6.1.1",
"ts-jest": "^29.1.1"
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ const Header = () => {
{modalOpenSignOut && <ModalSignOut />}
</div>
<span>
Fulano <br /> Souza
{localStorage.getItem("name")} <br /> {localStorage.getItem("lastName")}
</span>
</LoginContent>
</HeaderContent>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ContainerProduct } from './styles';
import { ContainerProduct, Status } from './styles';
import { teste } from './list'

export default function ProductBackLog() {
Expand All @@ -18,11 +18,11 @@ export default function ProductBackLog() {
{teste.map((product) => {
return (
<tr key={product.id}>
<td><h1>{product.nome}</h1></td>
<td><h1>{product.name}</h1></td>
<td><h1 className='history'>{product.history}</h1></td>
<td><h1>{product.status}</h1></td>
<td><h1>{product.prioridade}</h1></td>
<td><h1>{product.estimativa}</h1></td>
<td><Status $statusColor={product.priority}><h1>{product.priority}</h1></Status></td>
<td><h1>{product.estimate}</h1></td>
</tr>
)
})}
Expand Down
Loading

0 comments on commit d85b518

Please sign in to comment.