mirror of
https://github.com/20kaushik02/spotify-manager.git
synced 2026-01-25 06:04:05 +00:00
MASSIVE commit
- moved to typescript - axios rate limitmodule is busted, removed for now, do something else for that - sequelize-typescript - dotenv, not dotenv-flow - removed playlist details route types for API ton of minor fixes and improvements
This commit is contained in:
@@ -1,33 +0,0 @@
|
||||
import { sessionName } from "../constants.js";
|
||||
import * as typedefs from "../typedefs.js";
|
||||
import curriedLogger from "../utils/logger.js";
|
||||
const logger = curriedLogger(import.meta);
|
||||
|
||||
/**
|
||||
* middleware to check if access token is present
|
||||
* @param {typedefs.Req} req
|
||||
* @param {typedefs.Res} res
|
||||
* @param {typedefs.Next} next
|
||||
*/
|
||||
export const isAuthenticated = (req, res, next) => {
|
||||
if (req.session.accessToken) {
|
||||
req.sessHeaders = {
|
||||
"Authorization": `Bearer ${req.session.accessToken}`,
|
||||
// "X-RateLimit-SessID": `${req.sessionID}_${req.session.user.username}`
|
||||
};
|
||||
next();
|
||||
} else {
|
||||
const delSession = req.session.destroy((error) => {
|
||||
if (Object.keys(error).length) {
|
||||
res.status(500).send({ message: "Internal Server Error" });
|
||||
logger.error("session.destroy", { error });
|
||||
return;
|
||||
} else {
|
||||
res.clearCookie(sessionName);
|
||||
res.status(401).send({ message: "Unauthorized" });
|
||||
logger.debug("Session invalid, destroyed.", { sessionID: delSession.id });
|
||||
return;
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
31
middleware/authCheck.ts
Normal file
31
middleware/authCheck.ts
Normal file
@@ -0,0 +1,31 @@
|
||||
import type { AxiosRequestHeaders } from "axios";
|
||||
import type { RequestHandler } from "express";
|
||||
|
||||
import { sessionName } from "../constants.ts";
|
||||
|
||||
import curriedLogger from "../utils/logger.ts";
|
||||
const logger = curriedLogger(import.meta.filename);
|
||||
|
||||
export const isAuthenticated: RequestHandler = (req, res, next) => {
|
||||
if (req.session.accessToken) {
|
||||
req.session.authHeaders = {
|
||||
Authorization: `Bearer ${req.session.accessToken}`,
|
||||
} as AxiosRequestHeaders;
|
||||
next();
|
||||
} else {
|
||||
const delSession = req.session.destroy((error) => {
|
||||
if (Object.keys(error).length) {
|
||||
res.status(500).send({ message: "Internal Server Error" });
|
||||
logger.error("session.destroy", { error });
|
||||
return null;
|
||||
} else {
|
||||
res.clearCookie(sessionName);
|
||||
res.status(401).send({ message: "Unauthorized" });
|
||||
logger.debug("Session invalid, destroyed.", {
|
||||
sessionID: delSession.id,
|
||||
});
|
||||
return null;
|
||||
}
|
||||
});
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user