Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package br.com.linktreeclone.exception;

public class InvalidTokenException extends RuntimeException
{
public InvalidTokenException(String message) {
super(message);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,16 @@ public ResponseEntity<ErrorResponse> handleUnauthorizedException(UnauthorizedExc

return new ResponseEntity<>(errorResponse, HttpStatus.FORBIDDEN);
}

@ExceptionHandler(InvalidTokenException.class)
public ResponseEntity<ErrorResponse> handleInvalidTokenException(InvalidTokenException ex, WebRequest request) {
ErrorResponse errorResponse = new ErrorResponse(
LocalDateTime.now(),
HttpStatus.FORBIDDEN.value(),
"Forbidden",
ex.getMessage(),
request.getDescription(false).replace("uri=", "")
);
return new ResponseEntity<>(errorResponse, HttpStatus.FORBIDDEN);
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package br.com.linktreeclone.security;

import br.com.linktreeclone.entity.User;
import br.com.linktreeclone.exception.InvalidTokenException;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import io.jsonwebtoken.security.Keys;
Expand Down Expand Up @@ -53,7 +54,7 @@ public String validateToken(String token)
.getSubject();
}
catch (Exception e) {
throw new RuntimeException("Token JWT expirado ou inválido");
throw new InvalidTokenException("Token JWT expirado ou inválido");
}
}
}
16 changes: 15 additions & 1 deletion linktreeclone-frontend/src/api/axiosConfig.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import axios from 'axios';

const apiClient = axios.create({
baseURL: 'https://linktree-clone-api-238899108893.southamerica-east1.run.app',
baseURL: 'https://linktree-clone-api-faw777jkuq-rj.a.run.app',
});


Expand All @@ -15,4 +15,18 @@ apiClient.interceptors.request.use((config) => {
return Promise.reject(error);
});

apiClient.interceptors.response.use(
(response) => {
return response;
},
(error) => {
if (error.response && (error.response.status === 401 || error.response.status === 403)) {
localStorage.removeItem('authToken');
window.location.href = '/login';
console.log('Sessão expirada. Redirecionando para o login.');
}
return Promise.reject(error);
}
);

export default apiClient;