mirror of
https://github.com/bubblecup-12/VogelSocialMedia.git
synced 2025-07-06 15:18:48 +00:00
added refreshtoken logic to backend
This commit is contained in:
parent
a9bda19891
commit
ae318b61bf
2 changed files with 29 additions and 0 deletions
|
@ -31,6 +31,7 @@ export const minioClient = new Client({
|
||||||
//swagger configuration
|
//swagger configuration
|
||||||
import swaggerJSDoc from "swagger-jsdoc";
|
import swaggerJSDoc from "swagger-jsdoc";
|
||||||
import swaggerUi from "swagger-ui-express";
|
import swaggerUi from "swagger-ui-express";
|
||||||
|
import { deleteExpiredTokens } from "./tasks/deleteTokens";
|
||||||
|
|
||||||
const options = {
|
const options = {
|
||||||
definition: {
|
definition: {
|
||||||
|
@ -65,6 +66,15 @@ const options = {
|
||||||
};
|
};
|
||||||
const specs = swaggerJSDoc(options);
|
const specs = swaggerJSDoc(options);
|
||||||
app.use("/api-docs", swaggerUi.serve, swaggerUi.setup(specs));
|
app.use("/api-docs", swaggerUi.serve, swaggerUi.setup(specs));
|
||||||
|
|
||||||
|
// remove old tokens every ten minutes
|
||||||
|
setInterval(
|
||||||
|
() => {
|
||||||
|
console.log("Deleting old tokens");
|
||||||
|
deleteExpiredTokens();
|
||||||
|
},
|
||||||
|
10 * 60 * 1000
|
||||||
|
);
|
||||||
app.use(bodyParser.json());
|
app.use(bodyParser.json());
|
||||||
app.use("/api/user", userRouter);
|
app.use("/api/user", userRouter);
|
||||||
app.use("/api/posts", authenticateToken(), postRouter);
|
app.use("/api/posts", authenticateToken(), postRouter);
|
||||||
|
|
19
code/backend/src/tasks/deleteTokens.ts
Normal file
19
code/backend/src/tasks/deleteTokens.ts
Normal file
|
@ -0,0 +1,19 @@
|
||||||
|
import { PrismaClient } from "../../prisma/app/generated/prisma/client";
|
||||||
|
|
||||||
|
const prisma = new PrismaClient();
|
||||||
|
|
||||||
|
export const deleteExpiredTokens = async () => {
|
||||||
|
const now = new Date();
|
||||||
|
try {
|
||||||
|
const result = await prisma.refreshToken.deleteMany({
|
||||||
|
where: {
|
||||||
|
expiresAt: {
|
||||||
|
lt: now, // delete expired tokens
|
||||||
|
},
|
||||||
|
},
|
||||||
|
});
|
||||||
|
console.log(`deleted ${result.count} tokens`);
|
||||||
|
} catch (error) {
|
||||||
|
console.error("Error while deleting tokens:", error);
|
||||||
|
}
|
||||||
|
};
|
Loading…
Add table
Add a link
Reference in a new issue