Skip to content
Snippets Groups Projects
Commit ddd38246 authored by meskio's avatar meskio :tent:
Browse files

Ask for login if can't renew token

parent 6b18c215
No related branches found
No related tags found
No related merge requests found
......@@ -2,6 +2,7 @@ import React, { useEffect, useState } from "react";
import { useStorageItem } from "@capacitor-community/react-hooks/storage";
import Panel from "./Panel";
import AuthContext from "./AuthContext";
import { ResponseError } from "./errors";
import { url } from "./util";
function App() {
......@@ -60,7 +61,7 @@ function renewToken(oldToken, setToken) {
})
.then((response) => {
if (!response.ok) {
throw new Error(response.status.toString() + " " + response.statusText);
throw new ResponseError(response);
}
return response.json();
})
......@@ -69,7 +70,11 @@ function renewToken(oldToken, setToken) {
setToken(token);
})
.catch((error) => {
console.log("Error renewing token: " + error.message);
if (error instanceof ResponseError && error.response.status === 401) {
setToken(null);
} else {
console.log("Error renewing token: " + error.message);
}
});
}
......
import React from "react";
import { Row, Form, Spinner, Alert } from "react-bootstrap";
import AuthContext from "./AuthContext";
import { ResponseError } from "./errors";
import { url } from "./util";
class ResponseError extends Error {
constructor(response) {
super(response.status.toString() + " " + response.statusText);
this.response = response;
}
}
class Sender extends React.Component {
static contextType = AuthContext;
......
class ResponseError extends Error {
constructor(response) {
super(response.status.toString() + " " + response.statusText);
this.response = response;
}
}
export { ResponseError };
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment