Update frontend's ability to deal with expired JWT
This commit is contained in:
parent
e2f2edbf59
commit
57b52b6b50
@ -1,4 +1,3 @@
|
|||||||
require("dotenv").config();
|
|
||||||
const express = require("express");
|
const express = require("express");
|
||||||
const cors = require("cors");
|
const cors = require("cors");
|
||||||
const User = require("./models/user.model");
|
const User = require("./models/user.model");
|
||||||
@ -7,7 +6,7 @@ const app = express();
|
|||||||
app.use(express.json());
|
app.use(express.json());
|
||||||
|
|
||||||
const allowedOrigins = process.env.ALLOWED_ORIGINS.split(",").map(origin => origin.trim());
|
const allowedOrigins = process.env.ALLOWED_ORIGINS.split(",").map(origin => origin.trim());
|
||||||
console.log("Allowed Origins: ", allowedOrigins);
|
console.log("Allowed Origins:", allowedOrigins);
|
||||||
app.use(
|
app.use(
|
||||||
cors({
|
cors({
|
||||||
origin: function (origin, callback) {
|
origin: function (origin, callback) {
|
||||||
|
|||||||
@ -17,7 +17,7 @@ exports.createUser = async (username, hashedPassword, name) => {
|
|||||||
const result = await pool.query(
|
const result = await pool.query(
|
||||||
`INSERT INTO users (username, password, name, role)
|
`INSERT INTO users (username, password, name, role)
|
||||||
VALUES ($1, $2, $3, $4)`,
|
VALUES ($1, $2, $3, $4)`,
|
||||||
[username, hashedPassword, name, this.ROLES.EDITOR]
|
[username, hashedPassword, name, this.ROLES.VIEWER]
|
||||||
);
|
);
|
||||||
return result.rows[0];
|
return result.rows[0];
|
||||||
};
|
};
|
||||||
|
|||||||
@ -16,4 +16,17 @@ api.interceptors.request.use((config => {
|
|||||||
return config;
|
return config;
|
||||||
}));
|
}));
|
||||||
|
|
||||||
|
api.interceptors.response.use(
|
||||||
|
response => response,
|
||||||
|
error => {
|
||||||
|
if (error.response?.status === 401 &&
|
||||||
|
error.response?.data?.message === "Invalid or expired token") {
|
||||||
|
localStorage.removeItem("token");
|
||||||
|
window.location.href = "/login";
|
||||||
|
alert("Your session has expired. Please log in again.");
|
||||||
|
}
|
||||||
|
return Promise.reject(error);
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
export default api;
|
export default api;
|
||||||
Loading…
Reference in New Issue
Block a user