feat: add api prefix option

This commit is contained in:
Dragory 2024-04-06 11:54:31 +00:00
parent 899f743ed6
commit b40ed5ce49
No known key found for this signature in database
9 changed files with 27 additions and 22 deletions

View file

@ -51,8 +51,8 @@ function simpleDiscordAPIRequest(bearerToken, path): Promise<any> {
});
}
export function initAuth(app: express.Express) {
app.use(passport.initialize());
export function initAuth(router: express.Router) {
router.use(passport.initialize());
passport.serializeUser((user, done) => done(null, user));
passport.deserializeUser((user, done) => done(null, user as IPassportApiUser));
@ -110,8 +110,8 @@ export function initAuth(app: express.Express) {
),
);
app.get("/auth/login", passport.authenticate("oauth2"));
app.get(
router.get("/auth/login", passport.authenticate("oauth2"));
router.get(
"/auth/oauth-callback",
passport.authenticate("oauth2", { failureRedirect: "/", session: false }),
(req: Request, res: Response) => {
@ -122,7 +122,7 @@ export function initAuth(app: express.Express) {
}
},
);
app.post("/auth/validate-key", async (req: Request, res: Response) => {
router.post("/auth/validate-key", async (req: Request, res: Response) => {
const key = req.body.key;
if (!key) {
return res.status(400).json({ error: "No key supplied" });
@ -135,14 +135,14 @@ export function initAuth(app: express.Express) {
res.json({ valid: true, userId });
});
app.post("/auth/logout", ...apiTokenAuthHandlers(), async (req: Request, res: Response) => {
router.post("/auth/logout", ...apiTokenAuthHandlers(), async (req: Request, res: Response) => {
await apiLogins.expireApiKey(req.user!.apiKey);
return ok(res);
});
// API route to refresh the given API token's expiry time
// The actual refreshing happens in the api-token passport strategy above, so we just return 200 OK here
app.post("/auth/refresh", ...apiTokenAuthHandlers(), (req, res) => {
router.post("/auth/refresh", ...apiTokenAuthHandlers(), (req, res) => {
return ok(res);
});
}