- aggiornamento refreshtoken (parte 1)
- PCB: fix listino
This commit is contained in:
@@ -85,6 +85,38 @@ export const Api = {
|
||||
})
|
||||
},
|
||||
|
||||
async refreshToken() {
|
||||
// Implementa la logica per ottenere un nuovo token utilizzando il refreshToken
|
||||
// Potrebbe essere una chiamata al server simile a sendRequest()
|
||||
// Se il refreshToken è valido, salva il nuovo token e restituiscilo
|
||||
// Altrimenti, gestisci il caso di refreshToken scaduto o invalido, e.g., redirecting to login
|
||||
|
||||
try {
|
||||
console.log('refreshToken')
|
||||
const response = await axios.post('/users/newTok', {
|
||||
refreshToken: localStorage.getItem(toolsext.localStorage.refreshToken)
|
||||
});
|
||||
|
||||
// Save the new access token in local storage
|
||||
localStorage.setItem(toolsext.localStorage.token, response.data.accessToken);
|
||||
|
||||
// Reset the refresh token if it was reset by the server
|
||||
if (response.data.refreshToken) {
|
||||
localStorage.setItem(toolsext.localStorage.refreshToken, response.data.refreshToken);
|
||||
}
|
||||
|
||||
// Return the new access token
|
||||
return response.data.accessToken;
|
||||
} catch (error) {
|
||||
// Handle the error, for example by logging out the user
|
||||
console.error(error);
|
||||
localStorage.removeItem(toolsext.localStorage.token);
|
||||
localStorage.removeItem(toolsext.localStorage.refreshToken);
|
||||
throw error;
|
||||
}
|
||||
|
||||
},
|
||||
|
||||
async SendReq(url: string, method: string, mydata: any, setAuthToken = false): Promise<Types.AxiosSuccess | Types.AxiosError> {
|
||||
const mydataout = {
|
||||
...mydata,
|
||||
@@ -103,7 +135,7 @@ export const Api = {
|
||||
userStore.setResStatus(0)
|
||||
// eslint-disable-next-line @typescript-eslint/no-misused-promises
|
||||
return new Promise((resolve, reject) => sendRequest(url, method, mydataout)
|
||||
.then((res) => {
|
||||
.then(async (res) => {
|
||||
// console.log('status:', res.status)
|
||||
|
||||
setTimeout(() => {
|
||||
@@ -121,9 +153,24 @@ export const Api = {
|
||||
// Forbidden
|
||||
// You probably is connectiong with other page...
|
||||
userStore.setServerCode(toolsext.ERR_AUTHENTICATION)
|
||||
userStore.setAuth('')
|
||||
userStore.setAuth('', '')
|
||||
// $router.push('/signin')
|
||||
return reject({ code: toolsext.ERR_AUTHENTICATION })
|
||||
} else if (res.status === serv_constants.RIS_CODE__HTTP_FORBIDDEN_TOKEN_EXPIRED) {
|
||||
console.log('Token Expired')
|
||||
// Prova ad ottenere un nuovo token di accesso
|
||||
try {
|
||||
// Se il token è scaduto, allora faccio la richiesta di un NUOVO TOKEN, passandogli refreshToken
|
||||
const newAccessToken = await this.refreshToken();
|
||||
userStore.setAuth(newAccessToken, userStore.refreshToken);
|
||||
|
||||
// Riprova l'originale SendReq con il nuovo token.
|
||||
// Assicurati di evitare un loop infinito in caso di errori continui
|
||||
return resolve(this.SendReq(url, method, mydata, setAuthToken));
|
||||
} catch (error) {
|
||||
// Gestisci errore di refresh token (es. redirect a signin)
|
||||
return reject(error);
|
||||
}
|
||||
}
|
||||
}
|
||||
if (tools.isDebug())
|
||||
@@ -162,7 +209,7 @@ export const Api = {
|
||||
let lettoqualcosa = false
|
||||
|
||||
// console.log('A1) INIZIO.............................................................')
|
||||
return globalroutines( 'readall', tablesync, null)
|
||||
return globalroutines('readall', tablesync, null)
|
||||
.then((alldata) => {
|
||||
if (alldata === undefined) {
|
||||
console.log('alldata NON DEFINITA')
|
||||
@@ -184,10 +231,10 @@ export const Api = {
|
||||
.then((ris) => {
|
||||
ReceiveResponsefromServer(tablesync, nametab, method, ris.data)
|
||||
lettoqualcosa = true
|
||||
return globalroutines( 'delete', tablesync, null, rec._id)
|
||||
return globalroutines('delete', tablesync, null, rec._id)
|
||||
})
|
||||
.then(() => {
|
||||
return globalroutines( 'delete', 'swmsg', null, mystrparam)
|
||||
return globalroutines('delete', 'swmsg', null, mystrparam)
|
||||
}).catch((err) => {
|
||||
if (err.msgerr) {
|
||||
if (err.msgerr.message.includes('Failed to fetch') || err.msgerr.message.includes('Network Error')) {
|
||||
@@ -196,7 +243,7 @@ export const Api = {
|
||||
}
|
||||
console.log(' [Alternative] !!!!!!!!!!!!!!! Error while sending data', err, errorfromserver, 'lettoqualcosa', lettoqualcosa)
|
||||
if (!errorfromserver) {
|
||||
return globalroutines( 'delete', 'swmsg', null, mystrparam)
|
||||
return globalroutines('delete', 'swmsg', null, mystrparam)
|
||||
}
|
||||
})
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user