Skip to content

Commit

Permalink
Validando quando usuário perde a sessão.
Browse files Browse the repository at this point in the history
  • Loading branch information
rodrigoborgesmachado committed Dec 20, 2023
1 parent f56832f commit 0510580
Showing 1 changed file with 22 additions and 2 deletions.
24 changes: 22 additions & 2 deletions src/services/api.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import axios from "axios";
import Config from './../config.json';
import { toast } from 'react-toastify';

const api = axios.create({
//baseURL: 'https://localhost:7119/api',
baseURL: 'https://apisunsale.azurewebsites.net/api',
baseURL: 'https://localhost:7119/api',
//baseURL: 'https://apisunsale.azurewebsites.net/api',
headers: {
'Content-Type': 'application/json',
},
Expand All @@ -15,4 +16,23 @@ if (token) {
api.defaults.headers.common['Authorization'] = `Bearer ${token}`;
}

// Add an interceptor to handle unauthorized responses
api.interceptors.response.use(
(response) => response,
(error) => {
if (error.response && error.response.status === 401) {
localStorage.setItem(Config.LOGADO, 0);
localStorage.setItem(Config.USUARIO, '');
localStorage.setItem(Config.TOKEN, '');
localStorage.setItem(Config.ADMIN, '');
localStorage.setItem(Config.TEMPO_PARAM, 0);
localStorage.removeItem(Config.LOGADO);
localStorage.removeItem(Config.USUARIO);
localStorage.removeItem(Config.TOKEN);
localStorage.removeItem(Config.ADMIN);
}
return Promise.reject(error);
}
);

export default api;

0 comments on commit 0510580

Please sign in to comment.